-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conways_life_game.py
162 lines (119 loc) · 4.43 KB
/
Conways_life_game.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
"""
Conway's Game of Life
The "game" is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input.
At each step in time, the following transitions occur:
1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overpopulation.
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
"""
import pygame, sys, time
import numpy as np
from pygame.locals import *
#The WIDTH and HEIGHT of the game
WIDTH = 80
HEIGHT = 40
#The global vairable for detecting the click button
pygame.button_down = False
# The world map
pygame.world=np.zeros((HEIGHT,WIDTH))
# Draw cell
class Cell(pygame.sprite.Sprite):
size = 10
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([self.size, self.size])
# fill white
self.image.fill((255,255,255))
# 创建一个以左上角为锚点的矩形
self.rect = self.image.get_rect()
self.rect.topleft = position
#绘图函数,注意到我们是把画布重置了再遍历整个世界地图,因此有很大的性能提升空间
def draw():
screen.fill((0,0,0))
for sp_col in range(pygame.world.shape[1]):
for sp_row in range(pygame.world.shape[0]):
if pygame.world[sp_row][sp_col]:
new_cell = Cell((sp_col * Cell.size,sp_row * Cell.size))
screen.blit(new_cell.image,new_cell.rect)
#根据细胞更新规则更新地图
def next_generation():
nbrs_count = sum(np.roll(np.roll(pygame.world, i, 0), j, 1)
for i in (-1, 0, 1) for j in (-1, 0, 1)
if (i != 0 or j != 0))
pygame.world = (nbrs_count == 3) | ((pygame.world == 1) & (nbrs_count == 2)).astype('int')
#地图初始化
def init():
pygame.world.fill(0)
draw()
return 'Stop'
#停止时的操作
def stop():
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN and event.key == K_RETURN:
return 'Move'
if event.type == KEYDOWN and event.key == K_r:
return 'Reset'
if event.type == MOUSEBUTTONDOWN:
pygame.button_down = True
pygame.button_type = event.button
if event.type == MOUSEBUTTONUP:
pygame.button_down = False
if pygame.button_down:
mouse_x, mouse_y = pygame.mouse.get_pos()
sp_col = mouse_x / Cell.size;
sp_row = mouse_y / Cell.size;
if pygame.button_type == 1: #鼠标左键
pygame.world[sp_row][sp_col] = 1
elif pygame.button_type == 3: #鼠标右键
pygame.world[sp_row][sp_col] = 0
draw()
return 'Stop'
#计时器,控制帧率
pygame.clock_start = 0
#进行演化时的操作
def move():
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN and event.key == K_SPACE:
return 'Stop'
if event.type == KEYDOWN and event.key == K_r:
return 'Reset'
if event.type == MOUSEBUTTONDOWN:
pygame.button_down = True
pygame.button_type = event.button
if event.type == MOUSEBUTTONUP:
pygame.button_down = False
if pygame.button_down:
mouse_x, mouse_y = pygame.mouse.get_pos()
sp_col = mouse_x / Cell.size;
sp_row = mouse_y / Cell.size;
if pygame.button_type == 1:
pygame.world[sp_row][sp_col] = 1
elif pygame.button_type == 3:
pygame.world[sp_row][sp_col] = 0
draw()
if time.clock() - pygame.clock_start > 0.02:
next_generation()
draw()
pygame.clock_start = time.clock()
return 'Move'
if __name__ == '__main__':
#状态机对应三种状态,初始化,停止,进行
state_actions = {
'Reset': init,
'Stop': stop,
'Move': move
}
state = 'Reset'
pygame.init()
pygame.display.set_caption('Conway\'s Game of Life')
screen = pygame.display.set_mode((WIDTH * Cell.size, HEIGHT * Cell.size))
while True: # 游戏主循环
state = state_actions[state]()
pygame.display.update()