在这个程序中,雨水在循环中落下。当雨滴撞击地面时,它会从顶部开始。每次初始化程序时,都会创建随机颜色和随机雨滴位置。但是当我们回到画布顶部时,我无法弄清楚如何使每个雨滴位置和颜色随机。
var xPositions = [];
var yPositions = [];
for (var count = 0; count < 16; count++) {
fill(random(0, 255), random(0, 255), random(0, 255));
xPositions.push(random(0, 400));
yPositions.push(random(0, 400));
}
draw = function() {
background(204, 247, 255);
for (var i = 0; i < xPositions.length; i++) {
noStroke();
ellipse(xPositions[i], yPositions[i], 10, 10);
yPositions[i] += 4;
if (yPositions[i] > 400) {
yPositions[i] = 0;
}
}
};
var mouseClicked = function() {
xPositions.push(mouseX);
yPositions.push(mouseY);
};
&#13;
答案 0 :(得分:0)
你已经得到了答案,因为你得到了
if (yPositions[i] > 400) { yPositions[i] = 0; }
条件。只需添加类似
的内容 xPositions[i] = random(0,400);
答案 1 :(得分:0)
var xPositions = [];
var yPositions = [];
var color = [];
for (var count = 0; count < 16; count++) {
color.push(random(0, 255), random(0, 255), random(0, 255));
xPositions.push(random(0, 400));
yPositions.push(random(0, 400));
}
draw = function() {
background(204, 247, 255);
for (var i = 0; i < xPositions.length; i++) {
noStroke();
fill(var[i]);
ellipse(xPositions[i], yPositions[i], 10, 10);
yPositions[i] += 4;
if (yPositions[i] > 400) {
yPositions[i] = 0;
xPositions[i] = random(0, 400);
var[i] = (random(0, 255), random(0, 255), random(0, 255));
}
}
};
var mouseClicked = function() {
xPositions.push(mouseX);
yPositions.push(mouseY);
};
我建议将x,y和color变量放在一个对象中,这样你就不必使用3个数组