Advertisement
sokaroka

Pygame rectangle

Apr 3rd, 2013
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. import pygame
  2. import random
  3. import pdb;
  4.  
  5. # Define some colors
  6. red = (255,0,0)
  7. green = (0,255,0)
  8. blue = (0,0,255)
  9. darkBlue = (0,0,128)
  10. white = (255,255,255)
  11. black = (0,0,0)
  12. pink = (255,200,200)
  13. pygame.init()
  14.  
  15. class Rectangle():
  16.     x = random.randrange(0, 701)
  17.     y = random.randrange(0, 501)
  18.     width = random.randrange(20,71)
  19.     height = random.randrange(20,71)
  20.     change_x = random.randrange(-3,4)
  21.     change_y = random.randrange(-3,4)
  22.     def move(self):
  23.         self.x += self.change_x
  24.         self.y += self.change_y
  25.     def draw(self):
  26.         pygame.draw.rect(screen, green, [self.x,self.y, self.width, self.height], 0)
  27.    
  28. # Height/width of screen
  29. size = (700, 500)
  30. screen = pygame.display.set_mode(size)
  31.  
  32. # Name the window
  33. pygame.display.set_caption("Awwshiet First Game Window")
  34.  
  35. # Loop until close button is clicked
  36. done = False
  37.  
  38.  
  39. # Manages how fast screen updates
  40. clock = pygame.time.Clock()
  41.  
  42. myList = []
  43.  
  44. for i in range(10):
  45.     myList.append(Rectangle())
  46.  
  47. # ------- Main Program Loop -------#
  48. while done == False:
  49.    
  50.     # ALL EVENT PROCESSING BELOW THIS COMMENT
  51.    
  52.     for event in pygame.event.get(): # User did something
  53.         if event.type == pygame.QUIT: # If user clicked close
  54.             done = True # Flag as done so we exit this loop
  55.            
  56.     # ALL EVENT PROCESSING ABOVE THIS COMMENT
  57.  
  58.  
  59.     # ALL GAME LOGIC BELOW THIS COMMENT
  60.  
  61.     # ALL GAME LOGIC ABOVE THIS COMMENT
  62.    
  63.  
  64.  
  65.     # ALL CODE TO DRAW BELOW THIS COMMENT
  66.            
  67.     screen.fill(black)
  68.     for i in myList:
  69.         i.draw()
  70.            
  71.     # ALL CODE TO DRAW ABOVE THIS COMMENT
  72.  
  73.     pygame.display.flip()
  74.            
  75.     # 20FPS
  76.     clock.tick(20)
  77.  
  78. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement