在循环中使用if语句? - 处理 [英] Using an if statement within loops? - Processing

查看:126
本文介绍了在循环中使用if语句? - 处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Lets say I have to use an if statement within a for loop and the for loop fires at a certain condition and the if statement only fires when the for loop has reached a certain stage.

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

For example, the condition is a counter that counts up when a certain thing happens such as a ball falling down the screen. circles are drawn one by one everytime the ball crosses the screen. When the circles in the first row have reached the end of the screen, the circles start appearing on the second row below the first one. However the second row isnt working for me, which I have implemented with the if statement.

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 < counter; i++) { 
    ellipse(ScoreX + i * 80, 40, 40, 40); // draw circles in the first row one by one

     if( ScoreX + i * 80 > 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语句只是在第一行的球越过宽度时触发,但整个游戏只是停止而不是触发该行,这似乎是什么问题?

The if statement is meant to fire only when the balls on the first row are crossing the width, however the whole game just stops instead of firing that row, what seems to be the issue?

推荐答案

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

First advice: learn the proper Java coding convention, learn how to indent your code, and learn to name your variable.

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

A slight rewrite on your code should make a readable fix:

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来表示坐标


Further refactoring the code to make use of something like Point to represent coordinate

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);
   }
}

这篇关于在循环中使用if语句? - 处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆