Advertisement
Guest User

Stargate

a guest
Dec 8th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. class Star():
  2.     def __init__(self):
  3.         self.x = random(-width, width) #plots x in a random position between one end of the width of the screen to the other
  4.         self.y = random(-height, height) #like the code above, from top to bottom of screen
  5.         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
  6.         self.pz = self.z #causes previous pos of z to start at z
  7.    
  8.    
  9.     def update(self):
  10.         self.z = self.z - speed #defines the speed of the stars by constantly changing its position
  11.         if (self.z < 1): #if z becomes less than 1
  12.             self.z = width #z should return back to its og position
  13.             self.x = random(-width, width)
  14.             self.y = random(-height, height)
  15.             self.pz = self.z
  16.        
  17.    
  18.     def show(self): #defines the show seen in def draw()
  19.    
  20.         sx = map(self.x / self.z, 0, 1, 0, width) #maps a ratio
  21.         sy = map(self.y / self.z, 0, 1, 0, height)
  22.    
  23.         px = map(self.x / self.pz, 0, 1, 0, width)
  24.         py = map(self.y / self.pz, 0, 1, 0, height)
  25.    
  26.         self.pz = self.z
  27.    
  28.         line(px, py, sx, sy) #draws the line of the stars (when speed increases) according to their previous pos and their current pos
  29.  
  30.  
  31. stars = []
  32.  
  33. def setup():
  34.     global stars #defines stars as a global var
  35.     size(2000, 1000) #size of the screen
  36.     for i in range(4000): #amount of stars that can be produced at a time
  37.         stars.append(i) #continuously adds more stars
  38.         stars[i] = Star()
  39.  
  40.  
  41. def draw():
  42.     global stars, speed #defines stars and speed as global vars
  43.     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
  44.     background(0) #color of bg (black)
  45.     translate(width / 2, height / 2) #centers the image on the screen
  46.            
  47.     for i in range(4000):
  48.         stroke(random(255), 1, 255) #changes the color of stars between pink, blue and purple
  49.         stars[i].update() #updates the position of the stars on the screen
  50.         stars[i].show() #shows the stars on the screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement