-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
234 lines (182 loc) · 5.26 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""
gol - John Conway's game of life
1. add documentation
2. if dw !== dh, the cells get streched --> needs fix.
"""
##############################################
# #
# press SPACE to start the simulation #
# press RETURN to go back to the game intro #
# #
##############################################
import pygame as pg
from sys import exit
from math import floor
from itertools import chain
# pygame initialization
#RGB colors
white = (255, 255, 255)
black = (0, 0, 0)
clock = pg.time.Clock()
#tick = 5
window_title = pg.display.set_caption('gol')
dw = 1000 # display width
dh = 1000 # display height
gameDisplay = pg.display.set_mode((dw, dh))
num_cul = 40
num_row = 40
cul_size = dw / num_cul
row_size = dh/ num_row
class Board:
"""
the board refers to the white screen on which the game of life simulation is accurring.
this board contains many cells that together totally cover the board.
"""
def __init__(self, cul_size, row_size, num_cul, num_row):
self.cul_size = cul_size
self.row_size = row_size
self.num_cul = num_cul
self.num_row = num_row
self.game_state = {}
self.grid = self.get_grid()
# initialize game_state dict
for i in self.grid:
self.game_state[i] = Cell(i[0], i[1], self)
# initialize drawing board
def get_grid(self):
# in this case y is goes down (like -y)
grid_list = [[(x,y) for x in range(self.num_cul)] for y in range(self.num_row)]
# merging lists in coordinates_list(s)
grid = list(chain.from_iterable(grid_list))
return grid
def update(self):
l = []
for i in self.game_state.keys():
cell = self.game_state.get(i)
live_neighbors = cell.get_neighbors()
s = cell.state
# cell update rules
if s == 1:
# underpopulation or overpopulation
# cell dies
if (live_neighbors < 2) or (live_neighbors > 3):
l.append(cell)
# stable
elif (live_neighbors == 2) or (live_neighbors == 3):
continue
elif s == 0:
# ressurect dead cell
if live_neighbors == 3:
l.append(cell)
for i in l:
i.update_state()
def reset_game_state(self):
# returns the game state to it's original state (ie. all cell states as 0)
for i in self.game_state.keys():
cell = self.game_state.get(i)
cell.state = 0
print('game_state reset')
class Cell:
"""
cells are instantiated inside the Board class instance.
a cell contains information about it's state and position.
"""
def __init__(self, x, y, cls):
self.x = x
self.y = y
self.state = 0
self.game_state = cls.game_state
# draw cell on the board
self.rect = pg.draw.rect(gameDisplay, white, (cul_size*self.x, row_size*self.y, cul_size, row_size))
def update_state(self):
self.state = (self.state + 1) % 2 #ie. [0, 1] as possible states
if self.state == 1:
# fill with black
color = black
elif self.state == 0:
# fill with white
color = white
gameDisplay.fill(color, self.rect) # color, surface
#print('cell rect after filling with color:', (color,self.rect))
@staticmethod
def get_floor_pos(x,y):
xf, yf = floor(x), floor(y)
if x == xf:
xf = x-1
if y == yf:
yf = y-1
return xf,yf
def get_neighbors(self):
#print('x, y:', self.x,self.y)
"""
returns an integer representing the number of live neighbors around a cell.
"""
neighbors_list = [
(self.x - 1, self.y - 1), (self.x, self.y - 1), (self.x + 1, self.y - 1), # top row : top-left, top, top-right
(self.x - 1, self.y), (self.x + 1, self.y), # mid row : right, left
(self.x - 1, self.y + 1), (self.x, self.y + 1), (self.x + 1, self.y + 1)# bottom row : bottom-left, bottom, bottom-right
]
live_neighbors = 0
for i in neighbors_list:
value = self.game_state.get(i)
if value == None:
continue
else:
value = value.state
if value == 1:
live_neighbors += 1
return live_neighbors
class Game:
def __init__(self):
self.board = Board(cul_size, row_size, num_cul, num_row)
pg.init()
self.game_intro()
#self.game_loop()
def game_intro(self):
# cell drawing stage:
print('game_intro called')
tick = 6
running = True
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
pg.quit()
exit()
if event.type == pg.MOUSEBUTTONUP:
xmpos, ympos = pg.mouse.get_pos()
print('clicked at :', (xmpos,ympos))
# update game_state
x, y = Cell.get_floor_pos(xmpos/cul_size, ympos/row_size)
cell_obj = self.board.game_state.get((x,y))
cell_obj.update_state()
if event.type == pg.KEYUP:
if event.key == pg.K_SPACE:
running = False
# move to the game loop
pg.display.update()
clock.tick(tick)
self.game_loop()
def game_loop(self):
# cell simulation/interaction stage:
tick = 4
running = True
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
pg.quit()
exit()
if event.type == pg.KEYUP:
if event.key == pg.K_RETURN:
# if RETURN pressed, restart game (call self.game_intro())
self.board.reset_game_state()
gameDisplay.fill(white)
self.game_intro()
self.board.update()
pg.display.update()
clock.tick(tick)
def main():
game = Game()
if __name__ == '__main__':
main()