xxxxxxxxxx
55
// Declare a variable for the horizontal position of the ball
let xspeed=[1,2,3,4];
for (let xs = 0; xs <10, xs++){
}
// Declare a variable for the horizontal speed of the ball
let xspeed1 = 1;
let yspeed1 = 1.2;
let xspeed2= 2
let yspeed2=2
function setup() {
createCanvas(400, 400);
// Initialize the position in the center
x1 = width / 2;
y1 = height/2;
x2 = width / 2;
y2 = height/2
}
function draw() {
background(220);
// Move the ball ball, either right or left
x1 += xspeed1;
y1 += yspeed1;
x2 += xspeed2;
y2 += yspeed2;
// Simplified Version:
// When the ball is at either left or right border
if (x1 <= 0 || x1 >= width) {
// Flip the sign of the direction
xspeed1 *= -1;
}
if (y1 <= 0 || y1 >= height) {
// Flip the sign of the direction
yspeed1 *= -1;
}
if (x2 <= 0 || x2 >= width) {
// Flip the sign of the direction
xspeed1 *= -1;
}
if (y2 <= 0 || y2 >= height) {
// Flip the sign of the direction
yspeed2 *= -1;
}
// Draw the ball
ellipse(x1, y1, 50, 50);
ellipse(x2, y2, 50, 50);
}