-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_of_life.py
102 lines (79 loc) · 3.05 KB
/
game_of_life.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import time
import pygame
import numpy as np
COLOR_BG = (10, 10, 10)
COLOR_GRID = (40, 40, 40)
COLOR_DIE_NEXT = (170, 170, 170)
COLOR_ALIVE_NEXT = (255, 255, 255)
CELL_SIZE = 15
FPS = 10
def update(screen, cells, size, with_progress=False):
updated_cells = np.zeros((cells.shape[0], cells.shape[1]))
padded_cells = np.pad(cells, 1, mode="wrap")
for row, col in np.ndindex(cells.shape):
neighbors = padded_cells[row : row + 3, col : col + 3]
alive = np.sum(neighbors) - padded_cells[row + 1, col + 1]
color = COLOR_BG if cells[row, col] == 0 else COLOR_ALIVE_NEXT
if cells[row, col] == 1 and (alive < 2 or alive > 3):
if alive < 2 or alive > 3:
if with_progress:
color = COLOR_DIE_NEXT
elif 2 <= alive <= 3:
updated_cells[row, col] = 1
if with_progress:
color = COLOR_ALIVE_NEXT
else:
if alive == 3:
updated_cells[row, col] = 1
if with_progress:
color = COLOR_ALIVE_NEXT
pygame.draw.rect(screen, color, (col * size, row * size, size - 1, size - 1))
return updated_cells
def main() -> None:
pygame.init()
pygame.display.set_caption("Game of Life")
screen = pygame.display.set_mode((800, 800))
clock = pygame.time.Clock()
cells = set()
cells = np.zeros((80, 80))
screen.fill(COLOR_GRID)
update(screen, cells, CELL_SIZE)
pygame.display.flip()
pygame.display.update()
running = False
while True:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
return
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_g:
cells = np.random.choice([0, 1], size=(80, 80), p=[0.7, 0.3])
update(screen, cells, CELL_SIZE)
pygame.display.update()
if event.key == pygame.K_SPACE:
running = not running
update(screen, cells, CELL_SIZE, with_progress=True)
pygame.display.update()
elif event.key == pygame.K_c:
cells = np.zeros((80, 80))
screen.fill(COLOR_GRID)
update(screen, cells, CELL_SIZE)
pygame.display.update()
if pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos()
if cells[pos[1] // CELL_SIZE, pos[0] // CELL_SIZE] == 1:
cells[pos[1] // CELL_SIZE, pos[0] // CELL_SIZE] = 0
else:
cells[pos[1] // CELL_SIZE, pos[0] // CELL_SIZE] = 1
time.sleep(0.1)
update(screen, cells, CELL_SIZE)
pygame.display.update()
if running:
cells = update(screen, cells, CELL_SIZE)
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()