2016-10-12 3 views
-1

Итак, в нашем университете мы сейчас работаем с обработкой, я решил сделать небольшую игру. Однако, если я начну игру, он сработает после «отсутствия ответа». У меня были подобные проблемы, когда я шел в бесконечных циклах на C++ в школе, но я не могу найти ошибку, которую я сделал. Надеюсь, кто-то может мне помочь :)Обработка: Бесконечная для цикла (?)

/* 

Project: Circle Munch 

Project explanation: The player has to collect the green circles with the moveable circle via W, A, S, D or arrow keys and avoid the red ones -- Project for "Medieninformatik" WS 2016/2017      

Coder: Jannik Indorf 

Date of last edit: 12.10.2016 

Ideas of interest: diff. big circles, different points 


*/ 


float m_x = width/2;      // X-coordinate starting point (center) for: player 
float m_y = height/2;      // Y-coordinate "           " 
float [] o_x;        // X-c. for : object 
float [] o_y;        // Y-c. "    " 
float background_r = random(100);   //Background rgb values. 0-100 are dark, player and objects are bright 
float background_g = random(100); 
float background_b = random(100); 
int Score = 0;         // Player-Score 
int m_radius = 40;       // player radius 
int o_radius= 30;       // Object radia 
color g = color(255,0,0);     // color green 
color r = color(0,255,0);     // color red 
int[] o_color = {g,r};      // Array for color red && green → Function: color detection 
int Counter_o = 1;       // Obstacle Counter 
int o_c_v = o_color.length;     // length of Array: o_color for randomizing the outcome 

void setup()         
{ 
    size(640,480); 
    frameRate(60*4); 
    ellipseMode(CENTER); 
} 

void draw() 
{ 

// ********** Background color declaration ************************* 
    background(background_r,background_g,background_b); 

// ***********Drawing the Player ************************ 
fill(255,255,200); 
    ellipse(m_x,m_y, m_radius, m_radius);      // Moving Object 

// ********** TextOut f. Score ************************* 
    fill(0); 
    textSize(16); 
    textAlign(CENTER,CENTER); 
    text(Score,m_x,m_y); 

// ********** User-Input ************************* 
UserInput(); 
//****** Staying inside the window.. ********* 
InsideWindow(); 

    // 
    if(Score % 10 == 0 || Score == 0) 
    { 
     Counter_o = Score/10 +1; 

      /*for every Counter_o : draw 1 Ellipse. → for every Counter_o, make 2 new variables for every new object x and object y */ 


      for (int i=0; i<= Counter_o; i++) 
      { 

       for (int j=0; j <= i; j++) 
       { 
        o_x[j] = random(15,625); 
        o_y[j] = random(15,465);   

        o_c_v= (int) random(1,2); 
        fill(o_color[o_c_v]); 

        ellipse(o_x[j],o_y[j], o_radius, o_radius); 

       } 


      } 
    } 

// *********** Collision detection ************************ 

    for(int k =0 ; k< o_x.length ; k++) 
    { 
    if (dist(m_x , m_y, o_x[k], o_y[k]) <= m_radius/2 + o_radius/2) 
    { 
     Score = color_check(o_color[k]); 
     o_x[k] = random(15,625); 
     o_y[k] = random(15,465); 
    } 
    } 

} // *************End of loop************************************************************** 
/************************************************************************* 


Collision detection between circles 
Jannik Indorf 
12.10.2016 

**************************************************************************/ 

int color_check(int check_o_color) 
{ 
    int Score_difference = 0; 


    if (check_o_color == o_color[0]) // Green Obstacles increase the score 
    { 
    Score_difference = 5; 

    background_r = random(100); 
    background_g = random(100); 
    background_b = random(100); 
    } 
    else       // Red Obstacles lower the score 
    { 
    Score_difference = -10; 
    background_r = random(100); 
    background_g = random(100); 
    background_b = random(100); 

    } 

    m_radius = m_radius + Score_difference; 

return Score_difference; 
} 
/************************************************************************/ 
/************************************************************************* 


User Input via W,A,S,D or ↑ ↓ → ← 
Jannik Indorf 
12.10.2016 

**************************************************************************/ 

void UserInput() 
{ 
    if (keyPressed){           
    if(key =='w' || key == 'W'||keyCode == UP) 
    { 
     m_y = m_y - 1; 
    } 
    if(key =='a' || key == 'A'||keyCode == LEFT) 
    { 
     m_x = m_x - 1; 
    } 
    if(key =='d' || key == 'D'||keyCode == RIGHT) 
    { 
     m_x = m_x + 1; 
    } 
    if(key =='s' || key == 'S'||keyCode == DOWN) 
    { 
     m_y = m_y + 1; 
    } 

    } 
} 

/************************************************************************* 
************************************************************************* 

Staying Inside the Window, get out right, come out left 
Jannik Indorf 
12.10.2016 

**************************************************************************/ 

void InsideWindow() 
{ 
    if (m_x > width) 
    { 
    m_x = 0; 
    } 
    if (m_x < 0) 
    { 
    m_x = width; 
    } 
    if (m_y > height) 
    { 
    m_y = 0; 
    } 
    if (m_y < 0) 
    { 
    m_y = height; 
    } 


} 
/************************************************************************* 
*************************************************************************/ 

ответ

0

Ваш код не вводит бесконечный цикл. Он поражает NullPointerException на этой линии:

o_x[j] = random(15, 625); 

Это удар этого NullPointerException, потому что вы никогда не давали o_x значения. Вы можете исправить это, придав ему значение.

У вас будут похожие проблемы со многими другими вашими переменными. Похоже, вы пытаетесь написать весь свой эскиз за один раз, что только вызовет у вас головные боли. Вместо этого я бы рекомендовал сделать шаг назад и начать с чистого эскиза. Добавляйте только одну маленькую вещь за раз и проверяйте свой код каждый раз, когда вы добавляете новую строку!

+0

благодарит за ответ! –

+0

Редактировать: я снова пойду с нуля. я сделал гораздо более легкую версию, поэтому его не полностью написал за один раз, но я хотел использовать больше кругов и массивов, поэтому мне пришлось изменить почти все. –