-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconway.py
71 lines (61 loc) · 1.55 KB
/
conway.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
import pygame
import random
pygame.init()
screen_length = 150 #python can't handle 300 (max)
fps = 60
run = True
cell_size = 1
chance = 5
pause = False
grid_size = screen_length / cell_size
itr = range(int(grid_size))
g = grid_size - 1
screen = pygame.display.set_mode((screen_length, screen_length), pygame.SCALED)
clock = pygame.time.Clock()
cells = [[] for a in itr]
for i in itr:
for j in itr:
d = bool(random.randint(0, chance))
cells[i].append([d, d])
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
pause = not pause
if event.key == pygame.K_RETURN:
for i in cells:
for j in i:
j[0] = j[1] = bool(random.randint(0, chance))
if not pause:
screen.fill((0, 0, 0))
for x, i in enumerate(cells):
for y, j in enumerate(i):
if not j[0]:
screen.set_at((x, y), (255, 255, 255))
if (x > 0 and x < g and y > 0 and y < g):
alive = (8 - cells[x - 1][y - 1][0] -
cells[x][y - 1][0] -
cells[x + 1][y - 1][0] -
cells[x - 1][y][0] -
cells[x + 1][y][0] -
cells[x - 1][y + 1][0] -
cells[x][y + 1][0] -
cells[x + 1][y + 1][0])
if not j[0]:
if not (4 > alive > 1):
j[1] = True
else:
if alive == 3:
j[1] = False
else:
if not j[0]:
j[1] = j[0] = True
for i in cells:
for j in i:
j[0] = j[1]
clock.tick(fps)
print(clock.get_fps())
pygame.display.update()
pygame.quit()