xxxxxxxxxx
41
let x;
let y;
let xspeed = 1;
let yspeed = 2;
function setup() {
createCanvas(400, 400);
//position
x = width / 2;
y = height / 2;
}
function draw() {
background(220);
drawellipse();
//move
speed();
// control the bounce
direction();
}
function drawellipse() {
// Draw the ball
ellipse(x, y, 50, 50);
}
function speed() {
x += xspeed;
y += yspeed;
}
function direction() {
if (x <= 0 || x >= width) {
xspeed *= -1;
}
if (y <= 0 || y >= height) {
yspeed *= -1;
}
}