xxxxxxxxxx
53
let x1, y1, x1speed, y1speed;
let x2, y2, x2speed, y2speed;
function setup() {
createCanvas(400, 400);
x1 = width / 2;
y1 = height / 2;
x1speed = 1;
y1speed = 0.3;
x2 = width / 2;
y2 = height / 2;
x2speed = 1.4;
y2speed = -1.3;
}
function draw() {
background(220);
move();
display();
}
function display() {
ellipse(x1, y1, 50, 50);
ellipse(x2, y2, 50, 50);
}
function move() {
x1 += x1speed;
y1 += y1speed;
x2 += x2speed;
y2 += y2speed;
x1speed = bounce(x1, 0, width, x1speed);
y1speed = bounce(y1, 0, height, y1speed);
x2speed = bounce(x2, 0, width, x2speed);
y2speed = bounce(y2, 0, height, y2speed);
}
function bounce(pos, low, high, speed) {
if (pos < low || pos > high) {
speed *= -1;
return speed;
} else return speed;
}