# bounce.py

from Processing import *
window(500, 500)
h = height()
w = width()

sx = 0.0    # x position
sy = 0.0    # y position
vx = 1.0    # x velocity
vy = 1.0    # y velocity

ay = 0.2    # y acceleration (gravity)
fr = 0.9    # Losses due to friction

fill(255, 0, 0)

def draw(o, e):
    global sx, sy, vx, vy

    # Equations of Motion
    sx = sx + vx
    sy = sy + vy + 0.5*ay
    vy = vy + ay

    # Bounce off walls
    if sx <= 0.0 or sx >= w:
        vx = -fr*vx

    # Bounce off floor and
    # lose some velocity due to friction
    if sy > (h-10) and vy > 0.0:
        sy = h-10
        vy = -fr*vy

    # Draw at current location
    background(255)
    ellipse(sx, sy, 20, 20)

frameRate(50)
onLoop += draw
loop()
