-
Notifications
You must be signed in to change notification settings - Fork 1
/
gameoflife.py
executable file
·96 lines (82 loc) · 2.22 KB
/
gameoflife.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
#!/usr/bin/env python3
import pygame, time, random, copy
pygame.init()
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
darkBlue = (0,0,128)
white = (255,255,255)
black = (0,0,0)
pink = (255,200,200)
width, height = 100,100
tile = 5
padding = 1
s = pygame.display.set_mode((width*tile+padding*width, height*tile+padding*height))
def main():
done = False
tick = 0
t = time.time()
dt = time.time()
field = generate_field()
while not done:
tick += 1
dt = time.time()-t
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
field = step(field)
field = step(field)
draw(field)
pygame.display.flip()
def step(field):
s.fill(white)
updates = []
for y, f in enumerate(field):
for x, v in enumerate(f):
n = neighbors(field,x,y)
updates.append((y,x,rules(field[y][x], n)))
for y,x,update in updates:
field[y][x] = update
return field
def rules(cell, n):
if cell:
if n < 2: return False
if n == 2 or n == 3: return True
if n > 3: return False
else:
if n == 3: return True
def neighbors(field, x,y):
n = 0
X = width-1
Y = height-1
if y > 0:
if x > 0 and field[y-1][x-1]: n+=1
if x < X and field[y-1][x+1]: n+=1
if field[y-1][x]: n+=1
if x > 0:
if y < Y and field[y+1][x-1]: n+=1
if field[y][x-1]: n+=1
if y < Y:
if x < X and field[y+1][x+1]: n+=1
if field[y+1][x]: n+=1
if x < X and field[y][x+1]: n+=1
return n
def draw(original):
s.fill(white)
field = original
for y, f in enumerate(field):
for x, v in enumerate(f):
tile_y = y*tile+y*padding
tile_x = x*tile+x*padding
color = black if field[y][x] else white
pygame.draw.rect(s, color, (tile_y, tile_x, tile, tile), 0)
def generate_field():
field = []
for a in range(width):
l = []
for b in range(height):
l.append(random.random()>0.9)
field.append(l)
return field
main()