xxxxxxxxxx
41
let xes = [];
let yes = [];
let xspeed = [];
let yspeed = [];
function setup() {
createCanvas(400, 400);
// Initialize positions and speeds for 50 balls
for (let b = 0; b < 50; b++) {
xes.push(random(width)); // Random horizontal position
yes.push(random(height)); // Random vertical position
xspeed.push(random(-5, 5)); // Random horizontal speed
yspeed.push(random(-5, 5)); // Random vertical speed
}
}
function draw() {
background(220);
// Loop through all the balls
for (let b = 0; b < xes.length; b++) {
// Update positions
xes[b] += xspeed[b];
yes[b] += yspeed[b];
// Check for collisions with borders
if (xes[b] <= 0 || xes[b] >= width) {
xspeed[b] *= -1; // Reverse horizontal speed
}
if (yes[b] <= 0 || yes[b] >= height) {
yspeed[b] *= -1; // Reverse vertical speed
}
// Draw the ball
ellipse(xes[b], yes[b], 50, 50); // Corrected missing comma here
}
}