xxxxxxxxxx
42
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(0);
drawball();
speed();
bounce();
}
function drawball() {
noStroke();
ellipse(x, y, 50, 50);
}
function speed() {
x += xspeed;
y += yspeed;
}
function bounce() {
if (x <= 0 || x >= width) {
xspeed *= -1;
fill(random(255), random(255), random(255));
}
if (y <= 0 || y >= height) {
yspeed *= -1;
fill(random(255), random(255), random(255));
}
}