xxxxxxxxxx
32
let x = 0;
let xspeed=3;
function setup() {
createCanvas(400,400);
}
function draw() {
background(220);
move();
display();
bounce();
}
//Always move the ball
function move(){
xspeed=bounce(x,0,width,xspeed);
x=x+xspeed;
}
function bounce(pos,low,high,speed){
if(pos<low||pos>high){
return speed=-speed;
} else{
return speed;
}
}
function display(){
ellipse(x, height/2, 50, 50);
}