Bouncing ball

Link is here.

var ball = {
  x: 300,
  y: 200,
  xmove: 4,
  ymove: 4
}

function setup() {
  createCanvas(windowWidth, windowHeight);
}

function draw() {
  background(0);
  stroke(255)
  strokeWeight(4)
  noFill()
  ellipse(ball.x, ball.y, 25, 25)
  
  reverses(-1)
  
  moves()
}

function reverses(amount){
  if(ball.x > width || ball.x < 0 ){
    ball.xmove = ball.xmove * amount
     }
  
  if(ball.y > height || ball.y < 0){
    ball.ymove = ball.ymove * amount
  }
}

function moves(){
  ball.x = ball.x + ball.xmove
  ball.y = ball.y + ball.ymove
  //console.log(ball.x)
  //console.log(ball.xmove)
}

Leave a Reply