Python制作康威生命游戏

简介

最近学习了Python中的Pygame库,练习做了个游戏

Code

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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import pygame, sys, random

pygame.init()
bian = 50
bianchang = 15
HEIGHT = 40 + bian * bianchang
WIDTH = HEIGHT + 140
window_rect = pygame.rect.Rect(0, 0, WIDTH, HEIGHT)
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('生命游戏')

dai = 0
font = pygame.font.Font('1.ttf', 30)
text = font.render(str(dai), True, (0, 0, 0))
text2 = font.render('编辑模式', True, (0, 0, 0))
text_rect = text.get_rect()
text2_rect = text2.get_rect()
text2_rect.bottomright = window_rect.bottomright
text_rect.bottomright = window_rect.bottomright
zhuangtai = 0
kaishi = False
bianji = False

class Button():
def __init__(self, window, font: str, text: str, size: int, pos, width: int, height: int, text_color=(0, 0, 0), bg_color=(255, 255, 255)):
self.text = text
self.size = size
self.text_color = text_color
self.bg_color = bg_color
self.bg_color2 = bg_color
self.pos = pos
self.width = width
self.height = height
self.font = font
self.window = window

def draw(self):
x = pygame.draw.rect(self.window, self.bg_color, (self.pos[0], self.pos[1], self.width, self.height))
Font = pygame.font.Font(self.font, self.size)
text = Font.render(self.text, True, tuple(self.text_color))
text_rect = text.get_rect()
text_rect.center = x.center
self.window.blit(text, text_rect)

def down(self, pos):
mx, my = pos
bx, by = self.pos
bw, bh = self.width, self.height
if bx <= mx <= bx + bw and by <= my <= by + bh:
self.bg_color = (150, 150, 150)
self.draw()
return True

def up(self, pos):
mx, my = pos
bx, by = self.pos
bw, bh = self.width, self.height
if bx <= mx <= bx + bw and by <= my <= by + bh:
self.bg_color = self.bg_color2
self.draw()
pygame.display.update()
return True

buttonlist = [
Button(window, '1.ttf', '随机', 20, (HEIGHT + 10, 20), 100, 40, (255, 255, 255), (0, 0, 0)),
Button(window, '1.ttf', '下一步', 20, (HEIGHT + 10, 80), 100, 40, (255, 255, 255), (0, 0, 0)),
Button(window, '1.ttf', '开始', 20, (HEIGHT + 10, 140), 100, 40, (255, 255, 255), (0, 0, 0)),
Button(window, '1.ttf', '暂停', 20, (HEIGHT + 10, 200), 100, 40, (255, 255, 255), (0, 0, 0)),
Button(window, '1.ttf', '编辑', 20, (HEIGHT + 10, 260), 100, 40, (255, 255, 255), (0, 0, 0)),
Button(window, '1.ttf', '滑翔枪', 20, (HEIGHT + 10, 380), 100, 40, (255, 255, 255), (0, 0, 0)),
Button(window, '1.ttf', '滑翔机', 20, (HEIGHT + 10, 440), 100, 40, (255, 255, 255), (0, 0, 0)),
Button(window, '1.ttf', '太空船', 20, (HEIGHT + 10, 500), 100, 40, (255, 255, 255), (0, 0, 0)),
Button(window, '1.ttf', '青蛙', 20, (HEIGHT + 10, 560), 100, 40, (255, 255, 255), (0, 0, 0)),
Button(window, '1.ttf', '直线', 20, (HEIGHT + 10, 620), 100, 40, (255, 255, 255), (0, 0, 0)),
Button(window, '1.ttf', '清空', 20, (HEIGHT + 10, 680), 100, 40, (255, 255, 255), (0, 0, 0))
]


def linJuNum(i, j):
global lin
lin = 0

if i == 0 and j == 0:
zuo_shang = None
zuo = None
zuo_xia = None
you_shang = None
shang = None

you = lie[i][j + 1]
you_xia = lie[i + 1][j + 1]
xia = lie[i + 1][j]

elif i == bian - 1 and j == bian - 1:
you_xia = None
xia = None
you = None
you_shang = None
zuo_xia = None

zuo = lie[i][j - 1]
zuo_shang = lie[i - 1][j - 1]
shang = lie[i - 1][j]

elif i == 0 and j == bian - 1:
you_shang = None
shang = None
you = None
you_xia = None
zuo_shang = None

zuo = lie[i][j - 1]
zuo_xia = lie[i + 1][j - 1]
xia = lie[i + 1][j]

elif i == bian - 1 and j == 0:
zuo_xia = None
zuo_shang = None
zuo = None
xia = None
you_xia = None

shang = lie[i - 1][j]
you_shang = lie[i - 1][j + 1]
you = lie[i][j + 1]

elif i == 0:
zuo_shang = None
shang = None
you_shang = None

zuo = lie[i][j - 1]
zuo_xia = lie[i + 1][j - 1]
xia = lie[i + 1][j]
you_xia = lie[i + 1][j + 1]
you = lie[i][j + 1]

elif j == 0:
zuo_shang = None
zuo = None
zuo_xia = None

shang = lie[i - 1][j]
you_shang = lie[i - 1][j + 1]
you = lie[i][j + 1]
you_xia = lie[i + 1][j + 1]
xia = lie[i + 1][j]

elif i == bian - 1:
zuo_xia = None
xia = None
you_xia = None

zuo = lie[i][j - 1]
zuo_shang = lie[i - 1][j - 1]
shang = lie[i - 1][j]
you_shang = lie[i - 1][j + 1]
you = lie[i][j + 1]

elif j == bian - 1:
you_shang = None
you = None
you_xia = None

shang = lie[i - 1][j]
zuo_shang = lie[i - 1][j - 1]
zuo = lie[i][j - 1]
zuo_xia = lie[i + 1][j - 1]
xia = lie[i + 1][j]

else:
zuo_shang = lie[i - 1][j + 1]
zuo = lie[i][j + 1]
zuo_xia = lie[i + 1][j + 1]
xia = lie[i + 1][j]
you_xia = lie[i + 1][j - 1]
you = lie[i][j - 1]
you_shang = lie[i - 1][j - 1]
shang = lie[i - 1][j]

for i in [zuo_shang, shang, you_shang, zuo, you, zuo_xia, xia, you_xia]:
if i:
lin += 1

return lin

def jiSuan():
global lie, zhuangtai, newLie, dai, text, text_rect
newLie = [[0 for i in range(bian)] for j in range(bian)]

for i in range(bian):
for j in range(bian):
lin = linJuNum(i, j)
if lie[i][j] == 0:
if lin == 3:
zhuangtai = 1
else:
zhuangtai = 0
else:
if lin == 2 or lin == 3:
zhuangtai = 1
else:
zhuangtai = 0

newLie[i][j] = zhuangtai

lie = newLie.copy()
dai += 1
text = font.render(str(dai), True, (0, 0, 0))
text_rect = text.get_rect()
text_rect.bottomright = window_rect.bottomright

def suiJi():
global lie, kaishi, dai
dai = 0
kaishi = False
lie = [[random.randint(0, 1) for i in range(bian)] for j in range(bian)]

def showHeiDian(heng, lie):
pygame.draw.rect(window, (0, 0, 0), (20 + 2 + (heng - 1) * bianchang, 20 + 2 + (lie - 1) * bianchang, bianchang - 3, bianchang - 3))

def showWangGe():
x, y = 20, 20

for i in range(bian + 1):
pygame.draw.line(window, (0, 0, 0), (x, y), (x + (HEIGHT - 40), y))
y += bianchang

x, y = 20, 20

for j in range(bian + 1):
pygame.draw.line(window, (0, 0, 0), (x, y), (x, y + (HEIGHT - 40)))
x += bianchang

x, y = 20, 20

def forShow():
for i in range(bian):
for j in range(bian):
if lie[j][i] == 1:
showHeiDian(i + 1, j + 1)

def huaxiangqiang():
global lie, dai, kaishi
kaishi = False
dai = 0
lie = [[0 for j in range(bian)] for i in range(bian)]
lie[5][2], lie[5][3] = 1, 1
lie[6][2], lie[6][3] = 1, 1

lie[2][15] = 1
lie[3][14], lie[3][13 + 3] = 1, 1
lie[4][13], lie[4][17], lie[4][18] = 1, 1, 1
lie[5][13], lie[5][17], lie[5][18] = 1, 1, 1
lie[6][13], lie[6][17], lie[6][18] = 1, 1, 1
lie[7][14], lie[7][16] = 1, 1
lie[8][15] = 1

lie[0][27] = 1
lie[1][24], lie[1][25], lie[1][26], lie[1][27] = 1, 1, 1, 1
lie[2][23], lie[2][24], lie[2][25], lie[2][26] = 1, 1, 1, 1
lie[3][23], lie[3][26] = 1, 1
lie[4][23], lie[4][24], lie[4][25], lie[4][26] = 1, 1, 1, 1
lie[5][24], lie[5][25], lie[5][26], lie[5][27] = 1, 1, 1, 1
lie[6][27] = 1

lie[1][32] = 1
lie[2][32] = 1

lie[3][36], lie[3][37] = 1, 1
lie[4][36], lie[4][37] = 1, 1

def xiaochuan():
global lie, dai, kaishi
kaishi = False
dai = 0
lie = [[0 for j in range(bian)] for i in range(bian)]
lie[2][3] = 1
lie[3][4] = 1
lie[4][2], lie[4][3], lie[4][4] = 1, 1, 1

def taikongchuan():
global lie, dai, kaishi
kaishi = False
dai = 0
lie = [[0 for j in range(bian)] for i in range(bian)]
lie[2][2], lie[2][5] = 1, 1
lie[3][6] = 1
lie[4][2], lie[4][6] = 1, 1
lie[5][3], lie[5][4], lie[5][5], lie[5][6] = 1, 1, 1, 1

def chanchu():
global lie, dai, kaishi
kaishi = False
dai = 0
lie = [[0 for j in range(bian)] for i in range(bian)]
lie[3][3], lie[3][4], lie[3][5] = 1, 1, 1
lie[4][4], lie[4][5], lie[4][6] = 1, 1, 1

def zhixian():
global lie, dai, kaishi
kaishi = False
dai = 0
lie = [[0 for j in range(bian)] for i in range(bian)]
lie[25] = [1 for i in range(bian)]
lie[24] = [1 for i in range(bian)]

def quanbai():
global lie, dai, kaishi
kaishi = False
dai = 0
lie = [[0 for j in range(bian)] for i in range(bian)]

lie = [[0 for j in range(bian)] for i in range(bian)]

def showbutton():
for i in buttonlist:
i.draw()

def buttonup(pos):
for i in buttonlist:
i.up(pos)

def buttondown(pos):
y = []
for i in range(len(buttonlist)):
if buttonlist[i].down(pos):
y.append(i)
return y

while True:
pygame.time.Clock().tick(60)

window.fill((255, 255, 255))
showWangGe()
forShow()

showbutton()

if kaishi:
jiSuan()

if not bianji:
window.blit(text, text_rect)
else:
window.blit(text2, text2_rect)

for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
kaishi = not kaishi

elif event.type == pygame.MOUSEBUTTONDOWN:
pos = event.pos
if not bianji:
x = buttondown(pos)
for i in x:
if i == 0:
suiJi()
elif i == 1:
jiSuan()
elif i == 2:
kaishi = True
elif i == 3:
kaishi = False
elif i == 4:
bianji = True
elif i == 5:
huaxiangqiang()
elif i == 6:
xiaochuan()
elif i == 7:
taikongchuan()
elif i == 8:
chanchu()
elif i == 9:
zhixian()
elif i == 10:
quanbai()
else:
if buttonlist[4].down(pos):
bianji = False
elif 20 <= pos[0] <= bian * bianchang + 20 and 20 <= pos[1] <= 20 + bian * bianchang:
lieshu = (pos[0] - 5) // 15 - 1
hangshu = (pos[1] - 5) // 15 - 1
lie[hangshu][lieshu] = 1

elif event.type == pygame.MOUSEBUTTONUP:
pos = event.pos
buttonup(pos)

pygame.display.update()