所以在我们的大学,我们现在正在处理,我决定做一个小游戏。但是,如果我开始游戏,它会在#34;没有响应"之后崩溃。在学校里用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 :(得分:0)
您的代码不会进入无限循环。它在此行上点击NullPointerException
:
o_x[j] = random(15, 625);
它正在点击此NullPointerException
,因为您从未给过o_x
一个值。你可以通过给它一个值来解决这个问题。
但是你会遇到许多其他变量的类似问题。看起来你正试图一次写下你的整个草图,这只会让你感到头痛。相反,我建议退一步,重新开始草图。每次只添加一个小东西,并在每次添加新行时测试代码!