Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- import time
- # Define some colors
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- GREEN = (0, 255, 0)
- RED = (255, 0, 0)
- class Rectangle():
- '''Draw a rectangle'''
- def __init__(self):
- '''These are the rectangle's attributes.'''
- self.x = 0
- self.y = 0
- self.width = 0
- self.height = 0
- self.change_x = 0
- self.change_y = 0
- self.color = [0, 0, 0]
- def draw(self, screen):
- '''Drawing the rectangle.'''
- pygame.draw.rect(screen, self.color, [self.x, self.y, self.width, self.height])
- def move(self):
- '''Moving the rectangle around the screen.'''
- self.x += self.change_x
- self.y += self.change_y
- def main():
- pygame.init()
- # Set the width and height of the screen [width, height]
- size = (700, 500)
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption("My Game")
- # Loop until the user clicks the close button.
- done = False
- # Used to manage how fast the screen updates
- clock = pygame.time.Clock()
- my_list = []
- color_list = []
- '''Creates 1000 rectangles.'''
- for i in range(2000):
- my_object = Rectangle()
- r = random.randrange(256)
- g = random.randrange(256)
- b = random.randrange(256)
- my_object.color = [r, g, b]
- color_list.append(my_object)
- my_object.x = random.randrange(701)
- my_object.y = random.randrange(501)
- my_object.change_x = random.randrange(-3, 4)
- my_object.change_y = random.randrange(-3, 4)
- my_object.width = random.randrange(20, 71)
- my_object.height = random.randrange(20, 71)
- my_list.append(my_object)
- # -------- Main Program Loop -----------
- while not done:
- # --- Main event loop
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- screen.fill(WHITE)
- # Start the clock
- start = time.time()
- for item in my_list:
- item.draw(screen)
- item.move()
- # --- Go ahead and update the screen with what we've drawn.
- pygame.display.flip()
- # End the clock
- elapsed = time.time() - start
- print(elapsed)
- # --- Limit to 60 frames per second
- clock.tick(60)
- # Close the window and quit.
- pygame.quit()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement