使用python写的康威生命游戏
-
前期准备工作
首先你需要建立两个.py文件
1.lifegame.py 保存细胞状态 形成细胞等等
2.view.py 写窗体 按键 并制作画布等 -
在lifegame中写入以下代码
import random # 导入随机函数模块
height = 100 # 列表、画布高
width = 100 # 列表、画布高
def randomize(liebiao, width, height): # 随机生成1 和0 代表有无细胞函数
for z in range(0, height):
for j in range(0, width):
liebiao[z][j] = random.randint(0, 1)
return liebiao
# 初始化第一代细胞的状态:二位列表100*100
wang_ge_hua_model = [0] * height # 第一代细胞
next_wangge_model = [0] * height # 下一代细胞
for k in range(0, height):
wang_ge_hua_model[k] = [0] * width
next_wangge_model[k] = [0] * width
# randomize(wang_ge_hua_model, height, width)
# 定义计算下一代细胞状态的函数
def next_xi_bao():
global wang_ge_hua_model, next_wangge_model #调用全局
for z in range(0, height):
for j in range(0, width):
cell = 0 # 细胞状态 0 死亡 1 存活
count = count_zhouweixibao(wang_ge_hua_model, z, j) # 周围细胞数量
if wang_ge_hua_model[z][j] == 0:
if count == 3:
cell = 1 # 细胞状态 0 死亡 1 存活
elif wang_ge_hua_model[z][j] == 1:
if count == 2 or count == 3:
cell = 1 # 细胞状态 0 死亡 1 存活
next_wangge_model[z][j] = cell
temp = wang_ge_hua_model # 上一代保存
wang_ge_hua_model = next_wangge_model
next_wangge_model = temp
# 检查周围细胞数量的函数
def count_zhouweixibao(liebiao, row, col): # 康威生命游戏规则细胞周围的细胞数量过多 细胞会死 太少 也会死 所以要计算本细胞周围到底有几个细胞
count = 0
if row-1 >= 0:
count = count + liebiao[row-1][col]
if(row-1 >= 0) and (col-1 >= 0):
count = count + liebiao[row-1][col-1]
if (row-1 >= 0) and (col+1 < width):
count = count + liebiao[row-1][col+1]
if col-1 >= 0:
count = count + liebiao[row][col-1]
if col+1 < width:
count = count + liebiao[row][col+1]
if row+1 < height:
count = count + liebiao[row+1][col]
if(row+1 < height) and (col-1 >= 0):
count = count + liebiao[row+1][col-1]
if (row+1 < height) and (col+1 < width):
count = count + liebiao[row+1][col+1]
return count
# 滑翔机模型
huaxiangji = [[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]]
#选择模型
def load_model(model, x_offset=0, y_offset=0):
global wang_ge_hua_model
for z in range(0, height):
for j in range(0, width):
wang_ge_hua_model[z][j] = 0
j = y_offset
for row in model:
z = x_offset
for value in row:
wang_ge_hua_model[z][j] = value
z = z + 1
j = j + 1
if __name__ == '__main__':
next_xi_bao()
- 在view中写入以下代码
from tkinter import *
import lifegame # 导入 lifegame文件
cell_size = 5 # 细胞大小
is_running = False # 判断是否正在运行
# 绘制细胞
def draw_cell(row, col, color):
global wang_ge_shi_tu, cell_size
if color == 'black':
outline = "grey"
else:
outline = "white"
wang_ge_shi_tu.create_rectangle(row * cell_size,
col * cell_size,
row * cell_size + cell_size,
(col + 1) * cell_size,
fill=color, outline=outline)
# 按钮开始或暂停的更换
def start_chuli(event):
global is_running, start_button
if is_running:
is_running = False
start_button.config(text='开始')
else:
is_running = True
start_button.config(text='暂停')
update()
def clear_chuliqi(event):
global is_running, start_button
is_running = False
start_button.config(text='开始')
for z in range(0, lifegame.height):
for k in range(0, lifegame.height):
lifegame.wang_ge_hua_model[z][k] = 0
update()
def update():
global chaung_ti, wang_ge_shi_tu
wang_ge_shi_tu.delete(ALL)
lifegame.next_xi_bao()
for q in range(0, lifegame.height):
for a in range(0, lifegame.height):
if lifegame.wang_ge_hua_model[q][a] == 1:
draw_cell(q, a, 'black')
if is_running:
chaung_ti.after(100, update)
def option_chuliqi(event):
global choice, is_running, start_button
selection = choice.get()
if selection == '滑翔机':
if is_running:
is_running = False
start_button.config(text='开始')
lifegame.load_model(lifegame.huaxiangji, 10, 10)
elif selection == '随机':
if is_running:
is_running = False
start_button.config(text='开始')
lifegame.randomize(lifegame.wang_ge_hua_model, lifegame.width, lifegame.height)
update()
def wangge_chuli(event):
global cell_size, wang_ge_shi_tu
x = int(event.x / cell_size)
y = int(event.y / cell_size)
if lifegame.wang_ge_hua_model[x][y] == 1:
lifegame.wang_ge_hua_model[x][y] = 0
draw_cell(x, y, 'white')
else:
lifegame.wang_ge_hua_model[x][y] = 1
draw_cell(x, y, 'black')
chaung_ti = Tk()
chaung_ti.title("康威生命游戏")
wang_ge_shi_tu = Canvas(chaung_ti, width=lifegame.width*cell_size,
height=lifegame.height*cell_size,
bg="white")
start_button = Button(chaung_ti, text="开始", width=12)
clear_button = Button(chaung_ti, text="清除", width=12)
wang_ge_shi_tu.grid(row=0, columnspan=2, padx=20, pady=20)
wang_ge_shi_tu.bind('<Button-1>', wangge_chuli)
start_button.grid(row=1, column=0, sticky=W, padx=20, pady=20)
start_button.bind('<Button-1>', start_chuli)
choice = StringVar(chaung_ti)
choice.set('选择模型')
option = OptionMenu(chaung_ti, choice, '滑翔机', '随机', command=option_chuliqi)
option.config(width=20)
option.grid(row=1, column=1, padx=20)
clear_button.grid(row=1, column=2, sticky=E, padx=20, pady=20)
clear_button.bind('<Button-1>', clear_chuliqi)
for i in range(0, lifegame.height):
for j in range(0, lifegame.height):
if lifegame.wang_ge_hua_model[i][j] == 1:
draw_cell(i, j, 'black')
chaung_ti.mainloop()
- 点击运行 你就拥有一个康威生命游戏了
代码来自抖音“侯老师编程”