from Processing import *
from math import sin
window(500, 500)

class Bouncer:
    def __init__(self, x, y):
        self.x, self.y = x, y
        self.vx = random(-3, 3)
        self.vy = random(-3, 3)

    def advance(self):
        self.x += self.vx
        self.y += self.vy
        if self.x < 0 or self.x > width():
            self.vx = -self.vx
        if self.y < 0 or self.y > height():
            self.vy = -self.vy

    def draw(self):
        fill(0, 0, 255)
        ellipse(self.x, self.y, 50, 50)

class Blinker:
    def __init__(self, x, y):
        self.x, self.y = x, y
        self.count = 0
        self.blinking = False

    def advance(self):
        self.count = self.count + 1
        if self.count % 50 == 0:
            self.blinking = not self.blinking

    def draw(self):
        if self.blinking:
            fill( 255, 0, 0 )
        else:
            fill( 128)
        ellipse( self.x, self.y, 50, 50)

class Rotator:
    def __init__(self, x, y):
        self.x, self.y = x, y
        self.angle = 0

    def advance(self):
        self.angle = (self.angle + 0.05) % (2*PI)

    def draw(self):
        rectMode(CENTER)
        fill(0, 255, 0)
        pushMatrix()
        translate( self.x, self.y)
        rotate( self.angle )
        rect( 0, 0, 50, 50)
        popMatrix()

class Pulsator:
    def __init__(self, x, y):
        self.x, self.y = x, y
        self.angle = 0

    def advance(self):
        self.angle = (self.angle + 0.05) % (2*PI)

    def draw(self):
        fill(255, 255, 0)
        pushMatrix()
        s = sin(self.angle) + 1
        translate( self.x, self.y )
        scale(s)
        triangle(-50, -50, 0, 50, 50, -50)
        popMatrix()

o1 = Bouncer( 200, 200 )
o2 = Blinker( 100, 100 )
o3 = Bouncer( 400, 400 )
o4 = Rotator( 400, 100 )
o5 = Pulsator( 100, 400 )
objects = [o1, o2, o3, o4, o5]

def advance(o, e):
    background(255)
    for o in objects:
        o.advance()
        o.draw()

frameRate(50)
onLoop += advance
loop()
