在循环中使用if语句? – 处理

假设我必须在for循环中使用if语句,并且for循环在某个条件下触发,而if语句仅在for循环达到某个阶段时触发。

例如,条件是一个计数器,当某个事件发生时,例如球从屏幕上掉下来,它会计数。 每当球越过屏幕时,逐个绘制圆圈。 当第一行中的圆圈到达屏幕的末尾时,圆圈开始出现在第一行下方的第二行。 但是第二行对我来说不起作用,我用if语句实现了它。

float BallY = 50; // y value of the ball float BallX = 260; // x value of the ball float ScoreX = 52; float ScoreY = 40; int counter; void setup() { size(512, 348); //width and height of screen counter = 0; } void draw() { frameRate(600); background(255); fill(0); ellipse(BallX, BallY, 15, 15); //ball that will fall BallY++; //ball's y value increases each frame if (BallY > height) //if ball's y value is greater than the screen { BallY = 0; //reset the y value of the ball back to 0 counter++; } for (int i = 0; i  width) // if the circles cross the width { i = 0; //reset i to be 0 ellipse(ScoreX + i * 80, 80, 40, 40); // draw circles in the second row } }} 

if语句只是在第一行的球越过宽度时触发,但整个游戏只是停止而不是触发那一行,这似乎是什么问题?

第一个建议:学习正确的Java编码约定,学习如何缩进代码,并学习命名变量。

对代码进行轻微重写应该是一个可读的修复:

 int scoreStartX = 52; int scoreStartY = 40; int scoreBallSize = 40; // scorePosX/Y means the position the score-ball should be drawn scorePosX = scoreStartX; // scoreStartX/Y = starting position of score balls scorePosY = scoreStartY; for (int i = 0; i < score; i++) { ellipse(scorePosX , scorePosY , scoreBallSize , scoreBallSize); // increment the positions, and wrap to next col if over screen width scorePosX += scoreBallSize ; if( scorePosX > screenWidth) { // next score ball position is beyond the screen scorePosX = scoreStartX; scorePosY += scoreBallSize; } } 

进一步重构代码以使用像Point这样的东西来表示坐标

 Point scoreStartPos = new Point(52, 40); int scoreBallSize = 40; Point scorePos = new Point(scoreStartPos ); for (int i = 0; i < score; i++) { drawCircle(scorePos, scoreBallSize); // a little helper method makes your code easier to read // increment the positions, and wrap to next col if over screen width scorePos.translate( +scoreBallSize, 0); if( scorePos.getX() > screenWidth) { // next score ball position is beyond the screen scorePos.setLocation(scoreStartPoint.getX(), scorePos.getY() + scoreBallSize); } } 

这个说法

 i = 0; //reset i to be 0 

重置循环索引,因此可能导致无限循环。

你每次在ScoreX + i * 80 > width时将i设置为0 ,对吗? ScoreXwidth没有任何变化,这意味着循环将简单地重新计算为i使该条件为真的任何值,使您处于无限循环中。

希望你找到了问题的答案。 我给你一个可能的答案,涉及面向对象的编程。

你的问题略高于基本的东西。

所以可能的答案是:

 float BallY = 50; // y value of the ball float BallX = 260; // x value of the ball int counter = 0; score[] scores; // object oriented programming int n = 50; // number of objects void setup() { size(512, 348); //width and height of screen frameRate(600); scores = new score[n]; // object oriented float myx = 40; float myy = 40; for (int i = 0; i < n; i++) { // here we create our n score objects // here n = 50 so 50 objects are created. scores[i] = new score(myx, myy); // here we increase the x coordinate myx += 40; // here we check the boundaries and // if we go past we reset myx to 40 // and we go one line down if (myx > width) { myx = 40; myy += 40; } } } void draw() { background(255); fill(0); ellipse(BallX, BallY, 15, 15); //ball that will fall BallY++; //ball's y value increases each frame if (BallY > height) //if ball's y value is greater than the screen { BallY = 0; //reset the y value of the ball back to 0 counter++; } // we set the color fill(255/1, 255/1, 255/2); for (int i = 0; i < counter; i++) { if (counter < n) { // we draw the object scores[i].score_draw(); } } } // OBJECT ORIENTED : THE CLASS class score { float myx, myy; score(float x, float y) { myx = x; myy = y; } void score_draw() { ellipse(myx, myy, 40, 40); } } 

这有效但随着时间的推移会变慢。 你必须找出原因。

希望这有助于您继续学习处理和编程。

和平。