xxxxxxxxxx
52
let x, y;
let xspeed, yspeed;
function setup() {
createCanvas(400, 400);
x = width/2;
y = height/2;
xspeed = 1;
yspeed = 2;
}
function draw() {
background(220);
move();
// Bounce hor
xspeed1 = bounce(x1, xspeed1, 0, width);
// Bounce vert
yspeed1 = bounce(y1, yspeed1, 0, height);
// Bounce hor
xspeed2 = bounce(x2, xspeed2, 0, width);
// Bounce vert
yspeed2 = bounce(y2, yspeed2, 0, height);
display();
}
function move() {
// Update position
x+=xspeed;
y+=yspeed;
//console.log("YSPEED: ", yspeed);
}
function bounce(pos, dir, lower, upper) {
// Bouncing the movement direction
if(pos < lower || pos > upper) {
dir *= -1;
}
console.log("DIR: ", dir);
return dir;
}
function display() {
// Display code
ellipse(x, y, 50, 50);
}