Advertisement
Guest User

pygame file

a guest
Aug 10th, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. pygame.init()
  5.  
  6. width = 1280
  7. height = 720
  8. screen = pygame.display.set_mode((width,height))
  9. white = (255,255,255)
  10. black = (0,0,0)
  11. running = True
  12. stars = []
  13. center_x = width//2
  14. center_y = height//2
  15. radius = 10
  16.  
  17. class Star:
  18. def __init__(self):
  19. self.x = random.randint(-width,width)
  20. self.y = random.randint(-height,height)
  21. self.z = random.randint(center_y,width)
  22. self.pz = self.z
  23.  
  24. def update(self):
  25. self.z -= 10
  26. if self.z < 1: # when our z is reduced too much instead of crashing we remake the points to be at random new spots, this also saves us from having to make a new star too
  27. self.x = random.randint(-width, width)
  28. self.y = random.randint(-height, height)
  29. self.z = random.randint(center_y, width)
  30. self.pz = self.z
  31.  
  32. def show(self):
  33. sx = self.x / self.z * center_y + center_x
  34. sy = self.y / self.z * center_y + center_y
  35. pygame.draw.circle(screen, white, (sx,sy), radius)
  36.  
  37.  
  38. for i in range(0,26):
  39. stars.append(Star()) # add 100 random stars to our list
  40.  
  41. while running:
  42. screen.fill(black)
  43.  
  44.  
  45. for star in stars:
  46. star.update()
  47. star.show()
  48.  
  49. for event in pygame.event.get():
  50. if event.type == pygame.QUIT:
  51. running = False
  52.  
  53. pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement