xxxxxxxxxx
46
let x1, y1, x1speed, y1speed;
let x2, y2, x2speed, y2speed;
function setup() {
createCanvas(400, 400);
x1 = width / 2;
y1 = height / 2;
x1speed = 1;
y1speed = 3;
x2 = width / 2;
y2 = height / 2;
x2speed = 2;
y2speed = -3;
}
function draw() {
background(220);
move(x1, y1, x1speed, y1speed);
x1speed = bounce(x1, 0, width, x1speed);
y1speed = bounce(y1, 0, height, y1speed);
drawball(x1, y1);
move(x2, y2, x2speed, y2speed);
x2speed = bounce(x2, 0, width, x2speed);
y2speed = bounce(y2, 0, height, y2speed);
drawball(x2, y2);
}
function move(x, y, xspeed, yspeed) {
x += xspeed;
y += yspeed;
return
}
function drawball(x, y) {
ellipse(x, y, 50, 50);
}
function bounce(x, low, high, speed) {
if (x < low || x > high) {
speed *= -1;
}
return speed;
}