【问题标题】:Using an if statement within loops? - Processing在循环中使用 if 语句? - 加工
【发布时间】:2018-11-04 09:00:48
【问题描述】:

假设我必须在 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 < 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 语句仅在第一行的球越过宽度时触发,但是整个游戏只是停止而不是触发该行,这似乎是什么问题?

【问题讨论】:

  • 请不要破坏你的帖子——现在完全没有意义了。
  • 你有没有想过这个问题?如果是,请将答案标记为已接受。

标签: java loops if-statement conditional processing


【解决方案1】:

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

您的问题略高于基本内容。

所以一个可能的答案:

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

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

希望这可以帮助您继续学习处理和编程。

和平。

【讨论】:

    【解决方案2】:

    第一个建议:学习正确的 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
    • 这是您最初的“计数器”,将其称为“分数”似乎更有意义。而这段代码是供你阅读理解的,不是供你盲目复制的
    • 实际上,我更喜欢使用“Point”之类的东西来表示坐标,而不是将 x 和 y 保留为单独的变量。进一步更新示例给你一个想法
    • 阿德里安,这个“点”是函数吗?没见过,不是处理软件里面的功能……
    • 这是一个类。可以由您编写来表示一个点的 x-y 坐标。或者您可以简单地在 JDK awt 等中使用 Point 类。这里的要点是提取适当的类和函数将使您的代码更具可读性。
    【解决方案3】:

    您每次都将i 设置为0 ScoreX + i * 80 &gt; width,对吗? ScoreXwidth 没有任何变化,这意味着循环将简单地倒数到 i 的任何值使该条件为真,使您进入无限循环。

    【讨论】:

    • 但 i 是决定圆圈间距的那个.. 如果 i 不设置回 0,圆圈将出现在屏幕大小之外
    【解决方案4】:

    此声明

    i = 0; //reset i to be 0
    

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

    【讨论】:

    • 但如果我不重置 i,那么圆圈将被绘制到屏幕大小之外..
    • 那你必须以其他方式解决这个问题(我没有完全理解你想要达到的目标)。也许在其中放一个具有不同循环索引的第二个循环?
    • 但是你所说的这个“第二个循环”只有在第一个循环达到某个阶段时才必须触发..有没有办法以这种方式连接两个循环?
    • 我试图基本上用圆圈填充屏幕,每次球越过屏幕时,圆圈就会出现并填充第一行,然后是第二行,最后是整个屏幕
    • “填满整个屏幕”听起来像是两个嵌套循环。一个计算行数,另一个计算列数。
    猜你喜欢
    • 2019-05-06
    • 2015-07-06
    • 2014-03-13
    • 1970-01-01
    • 2019-12-20
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多