Conway(Python)

import random, time, copy
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
from matplotlib.animation import FuncAnimation
# 设置画布dpi和大小
fig = plt.figure(dpi=100, figsize=(6, 6))
# 画布边缘设置颜色
fig.patch.set_facecolor('yellow')
# 设置透明度
fig.patch.set_alpha(0.5)
ax = fig.add_subplot(111)
# 设置背景颜色
ax.patch.set_facecolor('yellow')
# 设置透明度
ax.patch.set_alpha(1)

# 隐藏坐标轴
plt.axis('off')

WIDTH = 100
HEIGHT = 100
scatter1 = plt.scatter([], [])

def init():
    # x轴范围设置
    plt.xlim(0, 100)

    # y轴范围设置
    plt.ylim(0, 100)

    # 有x轴刻度线间隔
    x_major_locator = MultipleLocator(20)

    # y轴刻度线间隔
    y_major_locator = MultipleLocator(20)

    ax.yaxis.set_major_locator(y_major_locator)
    ax.xaxis.set_major_locator(x_major_locator)
    plt.title("Conway")
    return scatter1,


nextCells = []
for x in range(WIDTH):
    colum = []
    for y in range(HEIGHT):
        if random.randint(0, 1) == 0:
            colum.append('#')
        else:
            colum.append('*')
    nextCells.append(colum)


def update(frame):
    print("\n\n\n")
    xaxis = []
    yaxis = []
    currentCells = copy.deepcopy(nextCells)
    for y in range(HEIGHT):
        for x in range(WIDTH):
            if currentCells[x][y] == '#':
                xaxis.append(x)
                yaxis.append(y)

    scatter1 = plt.scatter(xaxis, yaxis, color='purple', s=10, marker='+',alpha=1)

    for x in range(WIDTH):
        for y in range(HEIGHT):
            leftCoord = (x - 1) % WIDTH
            rightCoord = (x + 1) % WIDTH
            aboveCoord = (y - 1) % HEIGHT
            belowCoord = (y + 1) % HEIGHT

            numNeighbors = 0

            if currentCells[leftCoord][aboveCoord] == '#':
                numNeighbors += 1
            if currentCells[x][aboveCoord] == '#':
                numNeighbors += 1
            if currentCells[rightCoord][aboveCoord] == '#':
                numNeighbors += 1
            if currentCells[leftCoord][y] == '#':
                numNeighbors += 1
            if currentCells[rightCoord][y] == '#':
                numNeighbors += 1
            if currentCells[leftCoord][belowCoord] == '#':
                numNeighbors += 1
            if currentCells[x][belowCoord] == '#':
                numNeighbors += 1
            if currentCells[rightCoord][belowCoord] == '#':
                numNeighbors += 1

            if currentCells[x][y] == '#' and (numNeighbors == 2 or numNeighbors == 3):
                nextCells[x][y] = '#'
                #print((x, y), '生存')
            elif currentCells[x][y] == '*' and numNeighbors == 3:
                nextCells[x][y] = '#'
                #print((x, y), '生存')
            else:
                nextCells[x][y] = '*'
                #print((x,y),'死亡')

    return scatter1,


ani = FuncAnimation(fig
                    , update
                    , init_func=init
                    , frames=200
                    , interval=90
                    , blit=True
                    )
plt.show()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值