-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygame_visualization.py
53 lines (40 loc) · 1.39 KB
/
pygame_visualization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import pygame
import sys
from game import get_next_generation, get_random_generation
from pygame.locals import QUIT, K_RETURN, KEYUP
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
CELL_SIZE_PX = 20
ROWS = 20
COLUMNS = 40
FPS = 20
def draw(surface, generation):
surface.fill(WHITE)
for y, inner in enumerate(generation):
for x, alive in enumerate(inner):
if alive:
rect_x = x * CELL_SIZE_PX
rect_y = y * CELL_SIZE_PX
rect = (rect_x, rect_y, CELL_SIZE_PX, CELL_SIZE_PX)
pygame.draw.rect(surface, GRAY, rect)
if __name__ == '__main__':
pygame.init()
window_height = ROWS * CELL_SIZE_PX
window_width = COLUMNS * CELL_SIZE_PX
MAIN_SURFACE = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("Hello Conway's Game!")
fps_clock = pygame.time.Clock()
world = get_random_generation(COLUMNS, ROWS)
draw(MAIN_SURFACE, world)
while True:
world = get_next_generation(world)
for key_up_event in pygame.event.get(KEYUP):
if key_up_event.key == K_RETURN:
world = get_random_generation(COLUMNS, ROWS)
draw(MAIN_SURFACE, world)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fps_clock.tick(FPS)