overactive

gerfds

Jul 31st, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. import pygame
  2. import random
  3. import sys
  4. import time
  5.  
  6. WIDTH, HEIGHT = 600, 700
  7. WHITE = (255, 255, 255)
  8. BLUE = (0,100,200)
  9. RAIN_DENSITY = 50
  10. rain=[]
  11. canvas = pygame.display.set_mode((WIDTH,HEIGHT))
  12. background  = pygame.Surface((WIDTH,HEIGHT))
  13. pygame.display.set_caption('Rain')
  14. pygame.init()
  15.  
  16. def quitGame():
  17.     pygame.quit()
  18.     sys.exit(0)
  19.  
  20. def randomize():
  21.     x = random.randint(0, WIDTH-2)
  22.     y = random.randint(-700, -10)
  23.     speed = random.randint(1, 2)
  24.     return x, y, speed
  25.  
  26. class Drop():
  27.     def __init__(self):
  28.         self.x, self.y, self.speed = randomize()
  29.        
  30.     def update(self):
  31.         rain[i].y += rain[i].speed
  32.         self.dropRect = (rain[i].x, rain[i].y, 1, 20)
  33.         if rain[i].y > HEIGHT:
  34.             rain[i].x, rain[i].y, rain[i].speed = randomize()
  35.  
  36.     def show(self):
  37.         pygame.draw.rect(canvas, BLUE, self.dropRect, 1)
  38.  
  39.  
  40. drop = Drop()
  41. for i in range(RAIN_DENSITY):
  42.     rain.append(Drop())
  43.  
  44. while True:
  45.     for event in pygame.event.get():
  46.         if event.type == pygame.QUIT:
  47.             quitGame()
  48.  
  49.     canvas.blit(background, (0,0))
  50.    
  51.     for i in range(len(rain)):
  52.         drop.update()
  53.         drop.show()
  54.  
  55.     pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment