-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameoflife.py
130 lines (108 loc) · 3.21 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
# In the name of god #
# Game of life
# By Pooya.Sh.K
# inspired by John conway
import pygame
import sys
import os
pygame.init()
dis = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Game of Life")
WINW, WINH = dis.get_size()
BOXW = 10
BOXH = 10
assert WINW % BOXW == 0 and WINH % BOXH == 0
BOARDW = WINW//BOXW
BOARDH = WINH//BOXH
fill_color = (255, 255, 255)
empty_color = (0, 0, 0)
def make_new_board(w, h):
return list([0]*w for i in range(h))
def check(board, x, y):
m = 0
for i in range(-1, 2):
for j in range(-1, 2):
if not(i == j == 0) and (0 <= i+y < BOARDH) and (0 <= j+x < BOARDW):
m += board[i+y][j+x]
if board[y][x]:
if m == 2 or m == 3:
return True
if not board[y][x]:
if m == 3:
return True
return False
def update(board):
nb = make_new_board(BOARDW, BOARDH)
for i in range(BOARDH):
for j in range(BOARDW):
nb[i][j] = check(board, j, i)
return nb
def draw(win, board):
for i in range(BOARDH):
for j in range(BOARDW):
if board[i][j]:
pygame.draw.rect(win, fill_color, (j*BOXW, i*BOXH, BOXW, BOXH))
else:
pygame.draw.rect(win, empty_color,
(j*BOXW, i*BOXH, BOXW, BOXH))
def recommend_name():
n = 0
a = os.listdir()
l = []
for f in a:
if f.startswith("gol") and f.endswith(".png"):
l.append(f)
t = True
while t:
n += 1
t = False
if "gol"+str(n)+".png" in l:
t = True
return "gol"+str(n)+".png"
def load(name):
nb = make_new_board(BOARDW, BOARDH)
s = pygame.image.load(name)
for i in range(BOARDH):
for j in range(BOARDW):
nb[i][j] = (s.get_at((j*BOXW+BOXW//2, i*BOXH+BOXH//2))
== fill_color)
return nb
board = make_new_board(BOARDW, BOARDH)
updating = False
drag = False
down = False
dragval = False
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif e.type == pygame.MOUSEBUTTONDOWN:
x, y = e.pos
x, y = x//BOXW, y//BOXH
down = True
dragval = not board[y][x]
if drag == False:
board[y][x] = not board[y][x]
elif e.type == pygame.MOUSEBUTTONUP:
down = False
elif e.type == pygame.KEYDOWN:
if e.key == pygame.K_RETURN:
updating = not updating
elif e.key == pygame.K_RIGHT:
board = update(board)
elif e.key == pygame.K_s:
pygame.image.save(dis, recommend_name())
elif e.key == pygame.K_l:
board = load(input())
elif e.key == pygame.K_d:
drag = not drag
if drag and down:
x, y = pygame.mouse.get_pos()
x, y = x//BOXW, y//BOXH
board[y][x] = dragval
if updating:
board = update(board)
draw(dis, board)
pygame.display.update()