我正在制作生活游戏,我希望有一个窗口(在我的代码中称为Grid),它使用第二个网格(称为SecGrid)信息刷新
import numpy as np
import pylab as pl
from random import randint
i = 0
o = 0
#Chose Grid size
m = int(input("Enter the width and length of the Grid: ",))
n = m
print("Your Grid will be",m,"x",n)
Grid = np.zeros((m+2,n+2))
SecGrid = np.zeros((m+2,n+2))
#Random /Grid filling in
while(i <= n/3*m):
i = i+1
Grid[randint(1,m), randint(1,n)] = 1
img = pl.imshow(Grid, cmap = 'PuRd', interpolation = 'none')
#Main algo
while (o < 2):
print(o)
v = input("continue: ")
o = o + 1
for i in range(1,m):
for j in range(1,n):
#Nbr of entities
nbr = 0
for k in range(-1,1):
for l in range(-1,1):
nbr = nbr + Grid[i+k,j+l]
#cells that are alive
if Grid[i,j] == 1:
if nbr > 1 and nbr < 4:
SecGrid[i,j] = 1
else:
SecGrid[i,j] = 0
#cells that are dead
else:
if nbr == 3:
SecGrid[i,j] = 1
else:
SecGrid[i,j] = 0
如何使用新信息刷新网格?
我试过这个
Grid = SecGrid
pl.draw(img)
img.set_data(Grid)
但我是python的新手,所以我不知道._。请帮助meh:3
答案 0 :(得分:0)
pylab绘制方法不带任何参数。看看从命令行运行python的绘图文档。
For example:
$ python
>>> import pylab as pl
>>> help(pl.draw)
关于您的问题“如何使用新信息刷新网格?”,当您在“随机/网格填写”注释中更新网格中的值时,我将假设您询问如何更新该数据在你的img对象中。如果是这样,请参阅以下内容:
# Note: entered in your code line by line into python at command line then
>>> help(img.set_data)
Help on method set_data in module matplotlib.image:
set_data(self, A) method of matplotlib.image.AxesImage instance
Set the image array
ACCEPTS: numpy/PIL Image A