Advertisement
professor_craven

Drawing rectangles with Pygame

Mar 3rd, 2016
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. import pygame
  2. import random
  3. import time
  4.  
  5. # Define some colors
  6. BLACK = (0, 0, 0)
  7. WHITE = (255, 255, 255)
  8. GREEN = (0, 255, 0)
  9. RED = (255, 0, 0)
  10.  
  11.  
  12. class Rectangle():
  13.     '''Draw a rectangle'''
  14.     def __init__(self):
  15.         '''These are the rectangle's attributes.'''
  16.         self.x = 0
  17.         self.y = 0
  18.         self.width = 0
  19.         self.height = 0
  20.         self.change_x = 0
  21.         self.change_y = 0
  22.         self.color = [0, 0, 0]
  23.  
  24.     def draw(self, screen):
  25.         '''Drawing the rectangle.'''
  26.         pygame.draw.rect(screen, self.color, [self.x, self.y, self.width, self.height])
  27.  
  28.     def move(self):
  29.         '''Moving the rectangle around the screen.'''
  30.         self.x += self.change_x
  31.         self.y += self.change_y
  32.  
  33.  
  34. def main():
  35.     pygame.init()
  36.  
  37.     # Set the width and height of the screen [width, height]
  38.     size = (700, 500)
  39.     screen = pygame.display.set_mode(size)
  40.  
  41.     pygame.display.set_caption("My Game")
  42.  
  43.     # Loop until the user clicks the close button.
  44.     done = False
  45.  
  46.     # Used to manage how fast the screen updates
  47.     clock = pygame.time.Clock()
  48.  
  49.     my_list = []
  50.     color_list = []
  51.     '''Creates 1000 rectangles.'''
  52.     for i in range(2000):
  53.         my_object = Rectangle()
  54.  
  55.         r = random.randrange(256)
  56.         g = random.randrange(256)
  57.         b = random.randrange(256)
  58.         my_object.color = [r, g, b]
  59.         color_list.append(my_object)
  60.  
  61.         my_object.x = random.randrange(701)
  62.         my_object.y = random.randrange(501)
  63.  
  64.         my_object.change_x = random.randrange(-3, 4)
  65.         my_object.change_y = random.randrange(-3, 4)
  66.  
  67.         my_object.width = random.randrange(20, 71)
  68.         my_object.height = random.randrange(20, 71)
  69.  
  70.         my_list.append(my_object)
  71.  
  72.     # -------- Main Program Loop -----------
  73.     while not done:
  74.         # --- Main event loop
  75.         for event in pygame.event.get():
  76.             if event.type == pygame.QUIT:
  77.                 done = True
  78.  
  79.         screen.fill(WHITE)
  80.  
  81.         # Start the clock
  82.         start = time.time()
  83.  
  84.         for item in my_list:
  85.             item.draw(screen)
  86.             item.move()
  87.  
  88.         # --- Go ahead and update the screen with what we've drawn.
  89.         pygame.display.flip()
  90.  
  91.         # End the clock
  92.         elapsed = time.time() - start
  93.         print(elapsed)
  94.  
  95.         # --- Limit to 60 frames per second
  96.         clock.tick(60)
  97.  
  98.     # Close the window and quit.
  99.     pygame.quit()
  100.  
  101. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement