overactive

jhvgf

Aug 1st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 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. class Drop():
  21.     def __init__(self):
  22.         pass
  23.  
  24.     def randomize(self):
  25.         self.x = random.randint(0, WIDTH-2)
  26.         self.y = random.randint(-700, -10)
  27.         self.speed = random.randint(1, 2)
  28.         return self.x, self.y, self.speed
  29.        
  30.     def update(self):
  31.         self.y += self.speed
  32.         self.dropRect = (self.x, self.y, 1, 20)
  33.         if self.y > HEIGHT:
  34.             drop.randomize()
  35.  
  36.     def show(self):
  37.         pygame.draw.rect(canvas, BLUE, self.dropRect, 1)
  38.  
  39. drop = Drop()
  40. for i in range(RAIN_DENSITY):
  41.     drop.randomize()
  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