# python-examples: 443854fb317220a462488d0441a1b0b086b2f6b6

```     1: import random, pygame, time
2: pygame.init()
3: screen = pygame.display.set_mode((600,600))
4:
5: class Square:
6:
7: 	def __init__(self, side, topleft, height):
8: 		self.side = side
9: 		self.topleft = topleft
10: 		self.height = height
11:
12: 	def draw(self, screen):
13: 		pygame.draw.rect(screen, (0, 255 / self.height, 0), (self.topleft, (self.side, self.side)))
14:
15: 	def subdivide(self):
16: 		new_squares = []
17: 		new_squares.append(Square(self.side/2, self.topleft, self.height + random.randint(-1, 1)))
18: 		new_squares.append(Square(self.side/2, (self.topleft[0] + self.side/2, self.topleft[1]), self.height + random.randint(-1, 1)))
19: 		new_squares.append(Square(self.side/2, (self.topleft[0], self.topleft[1] + self.side/2), self.height + random.randint(-1, 1)))
20: 		new_squares.append(Square(self.side/2, (self.topleft[0] + self.side/2, self.topleft[1] + self.side/2), self.height + random.randint(-1, 1)))
21: 		return new_squares
22:
23: square = Square(600, (0, 0), 128)
24: #subdivided = []
25: square.draw(screen)
26: subdivided1 = square.subdivide()
27: subdivided2 = []
28: for subsquare in subdivided1:
29: 	subdivided2.append(subsquare.subdivide())
30: for subsquare in subdivided2:
31: 	for element in subsquare:
32: 		element.draw(screen)
33: pygame.display.update()
34: while True:
35: 	time.sleep(1)
```

Generated by git2html.