xxxxxxxxxx
let ballX, ballY;
let speedX, speedY;
let colourList = ['red', 'yellow', 'blue', 'green', 'pink', 'purple', 'indigo', 'black']
let currentColour = colourList[0]
let count = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
ballX = (width / 2);
ballY = (height / 2);
speedX = 5;
speedY = 5;
}
function draw() {
background("grey");
countNumber();
drawBall();
moveBall();
}
function countNumber() {
textSize(20)
text(`Number of bounces: ${count}`, 20, 30);
}
function drawBall() {
fill(currentColour)
circle(ballX, ballY, 50)
}
function moveBall() {
ballX += speedX;
ballY += speedY;
if (ballOutOfBoundsX()) {
currentColour = random(colourList);
speedX = -speedX;
count += 1
}
if (ballOutOfBoundsY()) {
currentColour = random(colourList);
speedY = -speedY;
count += 1
}
}
function ballOutOfBoundsX() {
return ((ballX + 25) > width || (ballX - 25) <= 0);}
function ballOutOfBoundsY() {
return ((ballY + 25) > height || (ballY - 25) <= 0);}