| 8 | |
| 9 | |
| 10 | class FloodFill: |
| 11 | def __init__(self, window_width, window_height): |
| 12 | self.window_width = int(window_width) |
| 13 | self.window_height = int(window_height) |
| 14 | |
| 15 | pygame.init() |
| 16 | pygame.display.set_caption("Floodfill") |
| 17 | self.display = pygame.display.set_mode((self.window_width, self.window_height)) |
| 18 | self.surface = pygame.Surface(self.display.get_size()) |
| 19 | self.surface.fill((0, 0, 0)) |
| 20 | |
| 21 | self.generateClosedPolygons() # for visualisation purposes |
| 22 | |
| 23 | self.queue = [] |
| 24 | |
| 25 | def generateClosedPolygons(self): |
| 26 | if self.window_height < 128 or self.window_width < 128: |
| 27 | return # surface too small |
| 28 | |
| 29 | from random import randint, uniform |
| 30 | from math import pi, sin, cos |
| 31 | |
| 32 | for n in range(0, randint(0, 5)): |
| 33 | x = randint(50, self.window_width - 50) |
| 34 | y = randint(50, self.window_height - 50) |
| 35 | |
| 36 | angle = 0 |
| 37 | angle += uniform(0, 0.7) |
| 38 | vertices = [] |
| 39 | |
| 40 | for i in range(0, randint(3, 7)): |
| 41 | dist = randint(10, 50) |
| 42 | vertices.append( |
| 43 | (int(x + cos(angle) * dist), int(y + sin(angle) * dist)) |
| 44 | ) |
| 45 | angle += uniform(0, pi / 2) |
| 46 | |
| 47 | for i in range(0, len(vertices) - 1): |
| 48 | pygame.draw.line( |
| 49 | self.surface, (255, 0, 0), vertices[i], vertices[i + 1] |
| 50 | ) |
| 51 | |
| 52 | pygame.draw.line( |
| 53 | self.surface, (255, 0, 0), vertices[len(vertices) - 1], vertices[0] |
| 54 | ) |
| 55 | |
| 56 | def run(self): |
| 57 | looping = True |
| 58 | while looping: |
| 59 | evsforturn = [] |
| 60 | for ev in pygame.event.get(): |
| 61 | if ev.type == pygame.QUIT: |
| 62 | looping = False |
| 63 | else: |
| 64 | evsforturn.append(ev) # TODO: Maybe extend with more events |
| 65 | self.update(evsforturn) |
| 66 | self.display.blit(self.surface, (0, 0)) |
| 67 | pygame.display.flip() |