xxxxxxxxxx
49
// Re-organize your bouncing ball code into at least 2 functions
// Create a reusable bounce() function that can be used to bounce positions, colors, anything you can think of.
let x,y;
let xspeed =4;
let yspeed =4;
function setup() {
createCanvas(600, 400);
//starting point
x= width/2;
y= height/2;
}
function draw() {
background(220);
// defining functions
drawBall();
move();
bounce();
}
function drawBall() {
ellipse(x, y, 50);
}
function move() {
x += xspeed;
y += yspeed;
}
function bounce() {
if (x <= 25 || x >= width-25) {
xspeed = -xspeed;
//color change
fill(random(255),random(255),random(255));
}
if (y <= 25 || y >= height-25) {
yspeed = -yspeed;
//color change
fill(random(255),random(255),random(255));
}
}