在数组中更改颜色
我试图使小行星的颜色通过红色,绿色,蓝色循环,当小行星脱离屏幕或撞到地面时。这是我当前拥有的代码:
float[] circleX;
float[] circleY;
float[] circledia;
float[] speedY;
float[] speedX;
int asteroid = 10;
void setup()
{
size(600, 700);
background(188, 234, 255);
circleX = new float[asteroid];
circleY = new float[asteroid];
circledia = new float[asteroid];
speedX = new float[asteroid];
speedY = new float[asteroid];
for (int i = 0; i < asteroid; i++)
{
circleX[i] = random(50, 500);
circleY[i] = 0;
circledia[i] = random(10, 50);
speedX[i] = random(-2, 2);
speedY[i] = random(2, 3);
}
}
void draw()
{
background(188, 234, 255);
noStroke();
fill(113, 69, 75);
rect(0, 580, width, height);
asteroidArr();
}
我认为这与我的无效小行星函数有关,但是我无法弄清楚在哪里放置if语句以骑每种颜色。
void asteroidArr()
{
for (int i = 0; i < asteroid; i++)
{
noStroke();
fill(255, 0, 0);
ellipse(circleX[i], circleY[i], circledia[i], circledia[i]);
circleY[i] = circleY[i] + speedY[i];
circleX[i] = circleX[i] + speedX[i];
if (circleY[i] > 560 || circleX[i] < -10 || circleX[i] > 610)
{
circleY[i] = 0;
circleX[i] = random(50, 500);
circledia[i] = random(10, 50);
speedY[i] = random(2, 2);
speedX[i] = random(-2, 2);
}
}
}
I'm trying to make the colour of the asteroids cycle through red, green, blue when either the asteroids goes off screen or hit the ground. Here's the code I currently have:
float[] circleX;
float[] circleY;
float[] circledia;
float[] speedY;
float[] speedX;
int asteroid = 10;
void setup()
{
size(600, 700);
background(188, 234, 255);
circleX = new float[asteroid];
circleY = new float[asteroid];
circledia = new float[asteroid];
speedX = new float[asteroid];
speedY = new float[asteroid];
for (int i = 0; i < asteroid; i++)
{
circleX[i] = random(50, 500);
circleY[i] = 0;
circledia[i] = random(10, 50);
speedX[i] = random(-2, 2);
speedY[i] = random(2, 3);
}
}
void draw()
{
background(188, 234, 255);
noStroke();
fill(113, 69, 75);
rect(0, 580, width, height);
asteroidArr();
}
I think it has something to do with my void asteroidArr function, but I cannot figure out where to put a if statement for cycling each colour.
void asteroidArr()
{
for (int i = 0; i < asteroid; i++)
{
noStroke();
fill(255, 0, 0);
ellipse(circleX[i], circleY[i], circledia[i], circledia[i]);
circleY[i] = circleY[i] + speedY[i];
circleX[i] = circleX[i] + speedX[i];
if (circleY[i] > 560 || circleX[i] < -10 || circleX[i] > 610)
{
circleY[i] = 0;
circleX[i] = random(50, 500);
circledia[i] = random(10, 50);
speedY[i] = random(2, 2);
speedX[i] = random(-2, 2);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用整数数组选择颜色(0 =红色,1 =绿色,2 =蓝色)。
然后,您可以在绘制每个圆圈之前使用此“选择器”选择正确的填充颜色。
(有多种方法可以做到这一点 - 您也可以使用开关语句。)
现在,您只需要确保在圆圈离开屏幕时增加选择器变量。
我建议您尝试先扩展代码而不查看下面的代码;)
这是您的建议插入:
希望这有所帮助。
You can use an array of integers to select the color (0 = red, 1 = green, 2 = blue).
Then, you can use this "selector" to choose the right fill color before drawing each circle.
(There are multiple ways to do this - you can also use a switch statement.)
Now, you only have to make sure to increase the selector variable when a circle leaves the screen.
I suggest you try to extend your code first without looking at the code below ;)
Here's your code with my suggestion inserted:
Hope this helped.