# StarField.py
from Processing import *
window(600, 600)

class Star:
    def __init__(self):
        self.x = random(-2000, 2000)
        self.y = random(-2000, 2000)
        self.z = random(0, 1000)

    def step(self):
        self.z = self.z - 5;
        if self.z <= 0.0:
            self.reset()

    def reset(self):
        # Reset star to a position far away
        self.x = random(-2000, 2000)
        self.y = random(-2000, 2000)
        self.z = 1000.0

    def display(self):
        # Project star only viewport
        offsetX = 100.0*(self.x/self.z)
        offsetY = 100.0*(self.y/self.z)
        scaleZ = 0.0001*(1000.0-self.z)

        # Draw this star
        pushMatrix()
        translate(offsetX, offsetY)
        scale(scaleZ)
        ellipse(0,0,50,50)
        popMatrix()

# List of all stars
stars = []

stroke(255)
strokeWeight(5)
rectMode(CENTER)

# Init all stars
for i in range(200):
    stars.append( Star() )

# Draw all stars
def draw(o, e):
    background(0)

    pushMatrix()

    # Draw all stars wrt center of screen
    translate(0.5*width(), 0.5*height())

    for i in range( len(stars) ):
        stars[i].step()
        stars[i].display()

    popMatrix()

# Draw all
frameRate(50)
onLoop += draw
loop()
