第71行: |
第71行: |
| | | |
| == 康威生命游戏python 实现== | | == 康威生命游戏python 实现== |
− | [https://github.com/Wangler2333/Conway-s-Game-of-Life/blob/master/conway.py 康威生命游戏python 实现下载:] | + | 点击此处[https://github.com/Wangler2333/Conway-s-Game-of-Life/blob/master/conway.py 康威生命游戏python 实现下载:] |
− | <syntaxhighlight lang="python">
| |
− | """
| |
− | conway.py
| |
− | A simple Python/matplotlib implementation of Conway's Game of Life.
| |
− | Author: Mahesh Venkitachalam
| |
− | """
| |
− | | |
− | import sys, argparse
| |
− | import numpy as np
| |
− | import matplotlib.pyplot as plt
| |
− | import matplotlib.animation as animation
| |
− | | |
− | ON = 255
| |
− | OFF = 0
| |
− | vals = [ON, OFF]
| |
− | | |
− | | |
− | def randomGrid(N):
| |
− | """随机产生"""
| |
− | return np.random.choice(vals, N * N, p=[0.2, 0.8]).reshape(N, N)
| |
− | | |
− | | |
− | def addGlider(i, j, grid):
| |
− | """添加一个滑翔机"""
| |
− | glider = np.array([[0, 0, 255],
| |
− | [255, 0, 255],
| |
− | [0, 255, 255]])
| |
− | grid[i:i + 3, j:j + 3] = glider
| |
− | | |
− | | |
− | def addGosperGliderGun(i, j, grid):
| |
− | """添加一个滑翔机发射器"""
| |
− | gun = np.zeros(11 * 38).reshape(11, 38)
| |
− | | |
− | gun[5][1] = gun[5][2] = 255
| |
− | gun[6][1] = gun[6][2] = 255
| |
− | | |
− | gun[3][13] = gun[3][14] = 255
| |
− | gun[4][12] = gun[4][16] = 255
| |
− | gun[5][11] = gun[5][17] = 255
| |
− | gun[6][11] = gun[6][15] = gun[6][17] = gun[6][18] = 255
| |
− | gun[7][11] = gun[7][17] = 255
| |
− | gun[8][12] = gun[8][16] = 255
| |
− | gun[9][13] = gun[9][14] = 255
| |
− | | |
− | gun[1][25] = 255
| |
− | gun[2][23] = gun[2][25] = 255
| |
− | gun[3][21] = gun[3][22] = 255
| |
− | gun[4][21] = gun[4][22] = 255
| |
− | gun[5][21] = gun[5][22] = 255
| |
− | gun[6][23] = gun[6][25] = 255
| |
− | gun[7][25] = 255
| |
− | | |
− | gun[3][35] = gun[3][36] = 255
| |
− | gun[4][35] = gun[4][36] = 255
| |
− | | |
− | grid[i:i + 11, j:j + 38] = gun
| |
− | | |
− | | |
− | def update(frameNum, img, grid, N):
| |
− | """
| |
− | 根据游戏规则刷新图像
| |
− | :param frameNum: matplotlib.animation模块需要传入的数据
| |
− | :param img: 原图
| |
− | :param grid: 坐标
| |
− | :param N: 尺寸
| |
− | :return: 新图
| |
− | """
| |
− | newGrid = grid.copy()
| |
− | for i in range(N):
| |
− | for j in range(N):
| |
− | # 计算周围八个格的和(0,255),计算周围有多少生命
| |
− | # %N用于考虑边界条件
| |
− | total = int((grid[i, (j - 1) % N] + grid[i, (j + 1) % N] +
| |
− | grid[(i - 1) % N, j] + grid[(i + 1) % N, j] +
| |
− | grid[(i - 1) % N, (j - 1) % N] + grid[(i - 1) % N, (j + 1) % N] +
| |
− | grid[(i + 1) % N, (j - 1) % N] + grid[(i + 1) % N, (j + 1) % N]) / 255)
| |
− | # 生命更新规则
| |
− | if grid[i, j] == ON:
| |
− | if (total < 2) or (total > 3):
| |
− | newGrid[i, j] = OFF
| |
− | else:
| |
− | if total == 3:
| |
− | newGrid[i, j] = ON
| |
− | # 更新数据
| |
− | img.set_data(newGrid)
| |
− | grid[:] = newGrid[:]
| |
− | return img,
| |
− | | |
− | | |
− | def main():
| |
− | # 接收传入的参数
| |
− | parser = argparse.ArgumentParser(description="Runs Conway's Game of Life simulation.")
| |
− | parser.add_argument('--grid-size', dest='N', required=False)
| |
− | parser.add_argument('--mov-file', dest='movfile', required=False)
| |
− | parser.add_argument('--interval', dest='interval', required=False)
| |
− | parser.add_argument('--glider', action='store_true', required=False)
| |
− | parser.add_argument('--gosper', action='store_true', required=False)
| |
− | args = parser.parse_args()
| |
− | | |
− | # 设置默认地图尺寸
| |
− | N = 100
| |
− | if args.N and int(args.N) > 8:
| |
− | N = int(args.N)
| |
− | | |
− | # 设置默认更新间隔时间ms毫秒
| |
− | updateInterval = 50
| |
− | if args.interval:
| |
− | updateInterval = int(args.interval)
| |
− | | |
− | # 选择起始图像
| |
− | grid = np.array([])
| |
− | # 没有选择则使用随机图像
| |
− | if args.glider:
| |
− | grid = np.zeros(N * N).reshape(N, N)
| |
− | addGlider(1, 1, grid)
| |
− | elif args.gosper:
| |
− | grid = np.zeros(N * N).reshape(N, N)
| |
− | addGosperGliderGun(10, 10, grid)
| |
− | else:
| |
− | grid = randomGrid(N)
| |
− | | |
− | # 设置动画
| |
− | fig, ax = plt.subplots()
| |
− | img = ax.imshow(grid, interpolation='nearest')
| |
− | ani = animation.FuncAnimation(fig, update, fargs=(img, grid, N,),
| |
− | frames=10,
| |
− | interval=updateInterval,
| |
− | save_count=50)
| |
− | | |
− | # 选择输出位置
| |
− | if args.movfile:
| |
− | ani.save(args.movfile, fps=30, extra_args=['-vcodec', 'libx264'])
| |
− | | |
− | plt.show()
| |
− | | |
− | | |
− | # 运行
| |
− | if __name__ == '__main__':
| |
− | main()
| |
− | | |
− | </syntaxhighlight>
| |
| ===效果展示=== | | ===效果展示=== |
| *Conway随机初始条件 | | *Conway随机初始条件 |