-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconway.py
More file actions
55 lines (43 loc) · 1.34 KB
/
Copy pathconway.py
File metadata and controls
55 lines (43 loc) · 1.34 KB
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
import pygame
import numpy as np
import sys
H = 200
W = 300
scale = 10
fps = 30
def main(width=64, height=48, scale=8, fps=60):
# global P
pygame.init()
screen = pygame.display.set_mode((width * scale, height * scale))
pygame.display.set_caption("CGOL")
clock = pygame.time.Clock()
P = np.random.randint(0, 2, size=(height, width), dtype=np.uint8)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Q = np.zeros_like(P, dtype=np.uint8)
Q += np.roll(P, 1, axis=0)
Q += np.roll(P, -1, axis=0)
Q += np.roll(P, 1, axis=1)
Q += np.roll(P, -1, axis=1)
Q += np.roll(P, (1, 1), axis=(0, 1))
Q += np.roll(P, (1, -1), axis=(0, 1))
Q += np.roll(P, (-1, 1), axis=(0, 1))
Q += np.roll(P, (-1, -1), axis=(0, 1))
P[Q < 2] = 0
P[Q > 3] = 0
P[Q == 3] = 1
for y in range(height):
for x in range(width):
val = P[y, x] * 255
color = (val, val, val)
rect = pygame.Rect(x * scale, y * scale, scale, scale)
screen.fill(color, rect)
pygame.display.flip()
clock.tick(fps)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main(W, H, scale, fps)