Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Star():
- def __init__(self):
- self.x = random(-width, width) #plots x in a random position between one end of the width of the screen to the other
- self.y = random(-height, height) #like the code above, from top to bottom of screen
- self.z = random(width) #even though animation is 2d, adding a z var creates depth, there is an illusion of stars being close and far
- self.pz = self.z #causes previous pos of z to start at z
- def update(self):
- self.z = self.z - speed #defines the speed of the stars by constantly changing its position
- if (self.z < 1): #if z becomes less than 1
- self.z = width #z should return back to its og position
- self.x = random(-width, width)
- self.y = random(-height, height)
- self.pz = self.z
- def show(self): #defines the show seen in def draw()
- sx = map(self.x / self.z, 0, 1, 0, width) #maps a ratio
- sy = map(self.y / self.z, 0, 1, 0, height)
- px = map(self.x / self.pz, 0, 1, 0, width)
- py = map(self.y / self.pz, 0, 1, 0, height)
- self.pz = self.z
- line(px, py, sx, sy) #draws the line of the stars (when speed increases) according to their previous pos and their current pos
- stars = []
- def setup():
- global stars #defines stars as a global var
- size(2000, 1000) #size of the screen
- for i in range(4000): #amount of stars that can be produced at a time
- stars.append(i) #continuously adds more stars
- stars[i] = Star()
- def draw():
- global stars, speed #defines stars and speed as global vars
- speed = map(mouseX, 0, width, 0, 90) #speed of production and movement of stars according to mousex position between 0 and width and 0 and 90
- background(0) #color of bg (black)
- translate(width / 2, height / 2) #centers the image on the screen
- for i in range(4000):
- stroke(random(255), 1, 255) #changes the color of stars between pink, blue and purple
- stars[i].update() #updates the position of the stars on the screen
- stars[i].show() #shows the stars on the screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement