@@ -9,13 +9,14 @@ class LifeGame(Frame):
9
9
# 2. A life dies when the number of surrounding lives is greater than 3
10
10
# 3. A dead revives when the number of surrounding lives is 3
11
11
# 4. A life stays the same when the number of surrounding lives is 2 or 3
12
- def __init__ (self , parent , grid : list = None , interval : float = 1 , cell_size : int = 5 ):
12
+ def __init__ (self , parent , grid : list = None , interval : float = 0.05 , cell_size : int = 5 ):
13
13
Frame .__init__ (self , parent )
14
- self .grid = [list (map (bool , row )) for row in grid ]
14
+ self .grid = [list (map (bool , row )) for row in grid ] if grid else LifeGame . randomGrid ()
15
15
self .interval = interval
16
16
self .cell_size = cell_size
17
17
self .hcells = len (self .grid [0 ])
18
18
self .vcells = len (self .grid )
19
+ self .cells = {}
19
20
self .canvas = Canvas (self ,
20
21
height = self .vcells * self .cell_size ,
21
22
width = self .hcells * self .cell_size )
@@ -25,12 +26,19 @@ def __init__(self, parent, grid: list = None, interval: float = 1, cell_size: in
25
26
def draw_cell (self , x , y , alive : bool ):
26
27
loc_x = x * self .cell_size
27
28
loc_y = y * self .cell_size
28
- self .canvas .create_rectangle (loc_x , loc_y ,
29
- loc_x + self .cell_size ,
30
- loc_y + self .cell_size ,
31
- fill = [None , 'black' ][alive ])
29
+ if alive :
30
+ cell = self .canvas .create_rectangle (loc_x , loc_y ,
31
+ loc_x + self .cell_size ,
32
+ loc_y + self .cell_size ,
33
+ fill = 'black' )
34
+ self .cells [(x , y )] = cell
35
+ else :
36
+ self .canvas .delete (self .cells [(x , y )])
37
+ del self .cells [(x , y )]
32
38
33
39
def start (self ):
40
+ self .cells .clear ()
41
+ self .pause = False
34
42
self .running = True
35
43
def func ():
36
44
for i , row in enumerate (self .grid ):
@@ -40,15 +48,17 @@ def func():
40
48
print ('Done Initing' )
41
49
while self .running :
42
50
time .sleep (self .interval )
43
- if self .running :
51
+ if self .pause :
52
+ pass
53
+ elif self .running :
44
54
self .endOfTurn ()
45
55
print ('End of turn' )
46
56
print ('Stopped.' )
47
57
self .thread = threading .Thread (target = func )
48
58
self .thread .start ()
49
59
50
60
def endOfTurn (self ):
51
- newCells = []
61
+ newGrid = []
52
62
for i , row in enumerate (self .grid ):
53
63
newRow = []
54
64
for j , cell in enumerate (row ):
@@ -75,20 +85,75 @@ def endOfTurn(self):
75
85
surrounding .append (self .grid [i + 1 ][j - 1 ])
76
86
alive = surrounding .count (True )
77
87
new = alive == 3 or (alive == 2 and cell )
78
- if new != cell :
79
- self .draw_cell (i , j , new )
88
+ if new != cell : self .draw_cell (i , j , new )
80
89
newRow .append (new )
81
- newCells .append (newRow )
82
- self .grid = newCells
90
+ newGrid .append (newRow )
91
+ self .grid = newGrid
92
+
93
+ def resume (self ):
94
+ self .pause = False
95
+
96
+ def pause (self ):
97
+ self .pause = True
83
98
84
99
def stop (self ):
85
100
self .running = False
86
101
102
+ def getNewGrid (grid ):
103
+ hmax = len (grid [0 ])
104
+ vmax = len (grid )
105
+ newGrid = []
106
+ for i , row in enumerate (grid ):
107
+ newRow = []
108
+ for j , cell in enumerate (row ):
109
+ surrounding = []
110
+ up = i > 0
111
+ left = j > 0
112
+ down = i + 1 < hmax
113
+ right = j + 1 < vmax
114
+ if left :
115
+ surrounding .append (row [j - 1 ])
116
+ if up :
117
+ surrounding .append (grid [i - 1 ][j - 1 ])
118
+ if up :
119
+ surrounding .append (grid [i - 1 ][j ])
120
+ if right :
121
+ surrounding .append (grid [i - 1 ][j + 1 ])
122
+ if right :
123
+ surrounding .append (row [j + 1 ])
124
+ if down :
125
+ surrounding .append (grid [i + 1 ][j + 1 ])
126
+ if down :
127
+ surrounding .append (grid [i + 1 ][j ])
128
+ if left :
129
+ surrounding .append (grid [i + 1 ][j - 1 ])
130
+ alive = surrounding .count (True )
131
+ new = alive == 3 or (alive == 2 and cell )
132
+ newRow .append (new )
133
+ newGrid .append (newRow )
134
+ return newGrid
135
+
136
+ def print (grid ):
137
+ for row in grid :
138
+ print (list (map (int , row )))
139
+
140
+ def randomGrid ():
141
+ grid = [[0 ] * 300 for _ in range (300 )]
142
+ rand = random .randint (0 , 2 )
143
+ num = 2 # rand
144
+ if num == 0 :
145
+ grid [50 ][50 ] = grid [51 ][51 ] = grid [49 ][52 ] = grid [50 ][52 ] = grid [51 ][52 ] = 1
146
+ elif num == 1 :
147
+ grid [30 ][30 ] = grid [31 ][30 ] = grid [32 ][30 ] = grid [36 ][30 ] = grid [37 ][30 ] = grid [38 ][30 ] = 1
148
+ grid [34 ][26 ] = grid [34 ][27 ] = grid [34 ][28 ] = grid [34 ][32 ] = grid [34 ][33 ] = grid [34 ][34 ] = 1
149
+ elif num == 2 :
150
+ for i in range (10 ):
151
+ grid [30 ][30 + i ] = 1
152
+ return grid
153
+
87
154
if __name__ == '__main__' :
88
- grid = [[0 ] * 300 for _ in range (300 )]
89
- grid [50 ][50 ] = grid [50 ][51 ] = grid [51 ][51 ] = 1
90
155
root = Tk ()
91
- game = LifeGame (root , grid )
156
+ game = LifeGame (root )
92
157
game .pack ()
93
158
root .mainloop ()
94
159
game .stop ()
0 commit comments