xxxxxxxxxx
41
// Declare a variable for the horizontal position of the ball
let x,y
// Declare a variable for the horizontal speed of the ball
let xspeed = 1;
let yspeed = 2;
function setup() {
createCanvas(600, 500);
// Initialize the position in the center
x = width / 2;
y= height /2;
}
function draw() {
background(220);
move();
bounce();
bounce();
display();
// Move the ball ball, either right or left
function move(){
x += xspeed;
y += yspeed;
}
function bounce(){
// Simplified Version:
// When the ball is at either left or right border
if (x < 0 || x > width) {
xspeed *= -1;
}
}
function display(){
// Draw the ball
ellipse(x, y, 50, 50);
}
}