xxxxxxxxxx
28
let x, y; // position of the ball
let xspeed, yspeed; // speed of the ball
let ballSize = 20; // size of the ball
function preload(){
}
function setup() {
createCanvas(400, 400);
x = width / 2; // start in the middle of the canvas
y = height / 2;
xspeed = random(-5, 5); // random speed in x and y directions
yspeed = random(-5, 5);
}
function draw() {
background(0); // black background
fill(255); // white ball
ellipse(x, y, ballSize, ballSize); // draw the ball
x += xspeed; // move the ball
y += yspeed;
if (x + ballSize / 2 > width || x - ballSize / 2 < 0) { // bounce off walls
xspeed = -xspeed;
}
if (y + ballSize / 2 > height || y - ballSize / 2 < 0) {
yspeed = -yspeed;
}
}