# bounce3
# Demonstrating the use of lists
# Expand program to bounce a variable number of balls

from Processing import *
window(500, 500)
w = width()
h = height()

# Init all variables
sx = []                     # x positions
sy = []                     # y positions
vx = []                     # x velocities
vy = []                     # y velocities

ay = 0.2                    # y acceleration (gravity)
fr = 0.9                    # Losses due to friction

nBalls = 20
fill(255, 0, 0)

# Initialize lists
for i in range(nBalls):
    sx.append( random(0.0, w) )
    sy.append( random(0.0, 10.0) )
    vx.append( random(-3.0, 3.0) )
    vy.append( random(0.0, 5.0) )

# Redraw all balls
def draw(o, e):
    global sx, sy, vx, vy
    background(255)

    for i in range(nBalls):
        # Equations of Motion
        sx[i] = sx[i] + vx[i]
        sy[i] = sy[i] + vy[i] + 0.5*ay
        vy[i] = vy[i] + ay

        # Bounce off walls and floor
        if sx[i] <= 0.0 or sx[i] >= w:
            vx[i] = -fr*vx[i]
        if sy[i] >= (h-10.0) and vy[i] > 0.0:
            sy[i] = (h-10.0)
            vy[i] = -fr*vy[i]

        # Draw ball
        ellipse( sx[i], sy[i], 20, 20)

# Set up looping
onLoop += draw
frameRate(50)
loop()
