xxxxxxxxxx
57
var balls = [];
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 100; i++) {
balls.push({
x: random(width),
y: random(height),
v: {
x: 0.0,
y: 0.0,
},
life: random(255),
});
}
}
function draw() {
background(0);
for (const ball of balls) {
let a;
if (abs(ball.v.y) > 0.2) {
a = 1.0;
} else if (abs(ball.v.y) <= 0.2) {
a = 1.0;
}
ball.x += ball.v.x;
ball.y += ball.v.y;
if (ball.y > 400 - 5) {
ball.v.y = random(0.1,1.0) * -ball.v.y;
ball.y = 400 - 5;
}
ball.v.y += a;
noStroke();
fill(255, ball.life);
circle(ball.x, ball.y, 10);
ball.life--;
if (ball.life < 0) {
ball.v.x = ball.v.y = 0.0;
ball.x = random(width);
ball.y = 0;
ball.life = random(255);
}
}
}
function keyPressed() {
for (const ball of balls) {
ball.x = random(width);
ball.y = random(height);
}
}