在循环中使用 if 语句? - 加工
Using an if statement within loops? - Processing
假设我必须在 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 语句只在第一行的球越过宽度时才触发,但整个游戏只是停止而不是触发该行,似乎是什么问题?
此声明
i = 0; //reset i to be 0
重置循环索引,因此可能会导致无限循环。
你每次 ScoreX + i * 80 > width
都将 i
设置为 0
,对吗? ScoreX
或 width
没有变化,这意味着循环将简单地返回到 i
使该条件为真的任何值,使您进入无限循环。
第一个建议:学习正确的 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);
}
}
希望您已经找到问题的答案。
我给你一个可能的答案,涉及面向对象编程。
你的问题有点超出了基本的东西。
所以一个可能的答案:
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);
}
}
这可行,但随着时间的推移会变慢。你将不得不找出原因。
希望这有助于您继续学习处理和编程。
和平。
假设我必须在 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 语句只在第一行的球越过宽度时才触发,但整个游戏只是停止而不是触发该行,似乎是什么问题?
此声明
i = 0; //reset i to be 0
重置循环索引,因此可能会导致无限循环。
你每次 ScoreX + i * 80 > width
都将 i
设置为 0
,对吗? ScoreX
或 width
没有变化,这意味着循环将简单地返回到 i
使该条件为真的任何值,使您进入无限循环。
第一个建议:学习正确的 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);
}
}
希望您已经找到问题的答案。 我给你一个可能的答案,涉及面向对象编程。
你的问题有点超出了基本的东西。
所以一个可能的答案:
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);
}
}
这可行,但随着时间的推移会变慢。你将不得不找出原因。
希望这有助于您继续学习处理和编程。
和平。