pygame for loop for draw shape

Solutions on MaxInterview for pygame for loop for draw shape by the best coders in the world

showing results for - "pygame for loop for draw shape"
Aicha
04 Oct 2016
1import pygame 
2from pygame.locals import *
3from sys import exit
4from random import *
5
6pygame.init()
7
8screen = pygame.display.set_mode((640, 480), 0,32)
9
10class Rectangle:
11    def __init__(self, pos, color, size):
12        self.pos = pos
13        self.color = color
14        self.size = size
15    def draw(self):
16        pygame.draw.rect(screen, self.color, Rect(self.pos, self.size))
17
18rectangles = []     
19
20for count in range(10):
21    random_color = (randint(0,255), randint(0,255), randint(0,255))
22    random_pos = (randint(0,639), randint(0,479))
23    random_size = (639-randint(random_pos[0], 639), 479-randint(random_pos[1],479))
24
25    rectangles.append(Rectangle(random_pos, random_color, random_size))
26
27
28
29while True:
30    for event in pygame.event.get():
31        if event.type == QUIT:
32            exit()
33    screen.lock()
34    for rectangle in rectangles:
35        rectangle.draw()
36    screen.unlock()
37    pygame.display.update()