xxxxxxxxxx
49
// Declare a variable for the horizontal position of the ball
let x,y,s,speed;
// Declare a variable for the horizontal speed of the ball
// let xspeed = 1;
// let yspeed = 2;
let move = 1
function setup() {
createCanvas(400, 400);
// Initialize the position in the center
x = width / 2;
y = height / 2;
}
function draw() {
background(220);
ballMoveSpeed(x+=1);
ballMoveSpeed(y+=2);
console.log(x)
ballBounce(x);
ballBounce(y)
drawBall();
}
function ballMoveSpeed(s,speed) {
// Move the ball, either right, left, up, down
s += speed;
// x += xspeed;
// y += yspeed;
}
function ballBounce(s,speed){
// When the ball is at either left or right border
if (s <= 0 || s >= width) {
// Flip the sign of the direction
speed *= -1;
}
// When the ball is at either left or right border
if (s <= 0 || s >= height) {
// Flip the sign of the direction
speed *= -1;
}
}
function drawBall(){
// Draw the ball
ellipse(x, y, 50, 50);
}