-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameofLife.py
217 lines (200 loc) · 7.49 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
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
import cv2
import numpy as np
import random
# define parameters
width=1366
height=768
x=0
y=0
class GameofLife:
def __init__(self):
self.menuButtoncolor=(250,250,250)
self.menuoptioncolor=(200,200,200)
self.myFont=cv2.FONT_HERSHEY_SIMPLEX
self.menustate:bool=False
self.play:bool=False
self.userinput:bool=False
self.randomstate:bool=False
self.clearstate:bool=False
self.clickstate:bool=False
self.gridstate:bool=False
self.alivecellcolour=(41, 204, 33)
self.deadcellcolour=(30,30,30)
# create random function
def fillRandom(self,frame):
x=np.zeros([height,width,3],dtype=np.uint8)
for i in range(0,frame.shape[1],8):
for j in range(0,frame.shape[0],8):
if random.choice([0,1,0,0,0,0]):
frame[j:j+8,i:i+8]=self.alivecellcolour
return x
# generate next gen cells
def generateNextgencells(self,layer1):
x=self.createbackground()
# left upper corner point
s=0
if layer1[12][4][0]==self.alivecellcolour[0]:
s+=1
if layer1[4][12][0]==self.alivecellcolour[0]:
s+=1
if layer1[12][12][0]==self.alivecellcolour[0]:
s+=1
if layer1[4][4][0]==self.deadcellcolour[0]:
if s==3:
x[:8,:8]=self.alivecellcolour
else:
if not(s==2 or s==3):
x[:8,:8]=self.deadcellcolour
# right upper point
s=0
if layer1[4][width-8][0]==self.alivecellcolour[0]:
s+=1
if layer1[12][width-8][0]==self.alivecellcolour[0]:
s+=1
if layer1[12][width-4][0]==self.alivecellcolour[0]:
s+=1
if layer1[4][width-4][0]==self.deadcellcolour[0]:
if s==3:
x[:8,1360:]=self.alivecellcolour
else:
if not(s==2 or s==3):
x[:8,1360:]=self.deadcellcolour
# lower left point
s=0
if layer1[height-12][4][0]==self.alivecellcolour[0]:
s+=1
if layer1[height-12][12][0]==self.alivecellcolour[0]:
s+=1
if layer1[height-4][12][0]==self.alivecellcolour[0]:
s+=1
if layer1[height-4][4][0]==self.deadcellcolour[0]:
if s==3:
x[height-8:,:8]=self.alivecellcolour
else:
if not(s==2 or s==3):
x[height-8:,:8]=self.deadcellcolour
# lower right point
s=0
if layer1[height-12][width-4][0]==self.alivecellcolour[0]:
s+=1
if layer1[height-12][width-8][0]==self.alivecellcolour[0]:
s+=1
if layer1[height-4][width-8][0]==self.alivecellcolour[0]:
s+=1
if layer1[height-4][width-4][0]==self.deadcellcolour[0]:
if s==3:
x[height-8:,1360:]=self.alivecellcolour
else:
if not(s==2 or s==3):
x[height-8:,1360:]=self.deadcellcolour
# the topmost row
for i in range(8,layer1.shape[1]-8,8):
s=0
if layer1[4][i-4][0]==self.alivecellcolour[0]:
s+=1
if layer1[4][i+12][0]==self.alivecellcolour[0]:
s+=1
if layer1[12][i-4][0]==self.alivecellcolour[0]:
s+=1
if layer1[12][i+12][0]==self.alivecellcolour[0]:
s+=1
if layer1[12][i+4][0]==self.alivecellcolour[0]:
s+=1
if layer1[4][i+4][0]==self.deadcellcolour[0]:
if s==3:
x[:8,i:i+8]=self.alivecellcolour
else:
if not(s==2 or s==3):
x[:8,i:i+8]=self.deadcellcolour
# the bottommost row
for i in range(8,layer1.shape[1]-8,8):
s=0
if layer1[height-4][i-4][0]==self.alivecellcolour[0]:
s+=1
if layer1[height-4][i+12][0]==self.alivecellcolour[0]:
s+=1
if layer1[height-12][i-4][0]==self.alivecellcolour[0]:
s+=1
if layer1[height-12][i+4][0]==self.alivecellcolour[0]:
s+=1
if layer1[height-12][i+12][0]==self.alivecellcolour[0]:
s+=1
if layer1[height-4][i+4][0]==self.deadcellcolour[0]:
if s==3:
x[height-8:,i:i+8]=self.alivecellcolour
else:
if not(s==2 or s==3):
x[height-8:,i:i+8]=self.deadcellcolour
# the leftmost column
for i in range(8,layer1.shape[0]-8,8):
s=0
if layer1[i-4][4][0]==self.alivecellcolour[0]:
s+=1
if layer1[i-4][12][0]==self.alivecellcolour[0]:
s+=1
if layer1[i+4][12][0]==self.alivecellcolour[0]:
s+=1
if layer1[i+12][12][0]==self.alivecellcolour[0]:
s+=1
if layer1[i+12][4][0]==self.alivecellcolour[0]:
s+=1
if layer1[i+4][4][0]==self.deadcellcolour[0]:
if s==3:
x[i:i+8,:8]=self.alivecellcolour
else:
if not(s==2 or s==3):
x[i:i+8,:8]=self.deadcellcolour
# the rightmost column
for i in range(8,layer1.shape[0]-8,8):
s=0
if layer1[i-4][width-4][0]==self.alivecellcolour[0]:
s+=1
if layer1[i-4][width-12][0]==self.alivecellcolour[0]:
s+=1
if layer1[i+4][width-12][0]==self.alivecellcolour[0]:
s+=1
if layer1[i+12][width-12][0]==self.alivecellcolour[0]:
s+=1
if layer1[i+12][width-4][0]==self.alivecellcolour[0]:
s+=1
if layer1[i+4][width-4][0]==self.deadcellcolour[0]:
if s==3:
x[i:i+8,1360:]=self.alivecellcolour
else:
if not(s==2 or s==3):
x[i:i+8,1360:]=self.deadcellcolour
# the central region
for i in range(8,layer1.shape[1]-8,8):
for j in range(8,layer1.shape[0]-8,8):
s=0
if layer1[j-4][i-4][0]==self.alivecellcolour[0]:
s+=1
if layer1[j+4][i-4][0]==self.alivecellcolour[0]:
s+=1
if layer1[j+12][i-4][0]==self.alivecellcolour[0]:
s+=1
if layer1[j+12][i+4][0]==self.alivecellcolour[0]:
s+=1
if layer1[j+12][i+12][0]==self.alivecellcolour[0]:
s+=1
if layer1[j+4][i+12][0]==self.alivecellcolour[0]:
s+=1
if layer1[j-4][i+12][0]==self.alivecellcolour[0]:
s+=1
if layer1[j-4][i+4][0]==self.alivecellcolour[0]:
s+=1
if layer1[j+4][i+4][0]==self.deadcellcolour[0]:
if s==3:
x[j:j+8,i:i+8]=self.alivecellcolour
else:
if not(s==2 or s==3):
x[j:j+8,i:i+8]=self.deadcellcolour
else:
x[j:j+8,i:i+8]=self.alivecellcolour
return x
# create background function
def createbackground(self):
background=np.zeros([height,width,3],dtype=np.uint8)
# Change the background lightness
background[:,:]=self.deadcellcolour
return background