code-lauren icon indicating copy to clipboard operation
code-lauren copied to clipboard

Example program: Bat and ball (OO-style)

Open maryrosecook opened this issue 9 years ago • 0 comments

ball: make-circle(50 50 20)
ball: set(ball "x-speed" 0)
ball: set(ball "y-speed" 0)

gravity: 0.5

forever {
  bat: make-rectangle(mouse-x mouse-y 100 10)

  if are-overlapping(bat ball) {
    bat-ball-difference: subtract(get(ball "x") get(bat "x"))
    ball: set(ball "x-speed" divide(bat-ball-difference 10))  
    ball: set(ball "y-speed" opposite(get(ball "y-speed")))
  } else {
    ball: set(ball "y-speed" add(gravity get(ball "y-speed")))
  }

  ball: set(ball "x" add(get(ball "x") get(ball "x-speed")))
  ball: set(ball "y" add(get(ball "y") get(ball "y-speed")))

  clear-screen()
  draw(ball)
  draw(bat)
}

maryrosecook avatar Dec 15 '15 22:12 maryrosecook