Using an if statement within loops? - Processing

ぐ巨炮叔叔 提交于 2019-12-02 12:56:30

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

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

This statement

i = 0; //reset i to be 0

resets the loop index and is therefore likely to cause an infinite loop.

You are setting i to 0 every time ScoreX + i * 80 > width, right? There are no changes to ScoreX or width, meaning that the loop will simply count back up to whatever value of i makes that condition true, putting you in an infinite loop.

Hope you have found the answer to your question. I give you a possible answer which involves object oriented programming.

Your question is a little bit above the basic stuff.

So a possible answer:

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

This works but it gets slower over time. You will have to find out why.

Hope this helps you carry on your learning of processing and programming in general.

PEACE.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!