python生活脚本,Python模块绘制生活游戏脚本

我想知道你推荐的绘制X-Y平面坐标列表的模块是什么。目的是实现人生游戏,我已经尝试过了matplotlib.pyplot但我有一些关于轴比的问题,一般来说,这似乎不是绘制某种动画的最佳方式。脚本的主要思想是绘制细胞生存的世界,并在时间。睡觉. 在import matplotlib.pyplot as plt

class Cell:

#Cell class, with coordinates and an ON-OFF state

def __init__(self,x=0,y=0,state=0):

self.state=state

self.x=x

self.y=y

self.coord=[self.x,self.y]

def __repr__(self):

if self.state:

return "*"

else:

return " "

#methods to turn on and off the cells:

def turn_on(self):

self.state=1

return self

def turn_off(self):

self.state=0

return self

class Mondo:

#the world in which the cells live:

def __init__(self,width=10,height=30,starting_cells=[]):

self.width=width

self.height=height

self.grid=[[Cell(i,j) for j in range(height)] for i in range(width)]

for cell in starting_cells:

self.grid[cell.x][cell.y]=cell

def __repr__(self):

#this method is needed just if I wanted to use just the print statement instead of matplotlib

return "_"*(2*self.height+3)+"\n"+"\n".join([" ".join(["|"]+[" "+str(cell)+" " for cell in self.grid[row]]+["|"]) for row in range(self.width)])+"\n"+"_"*(2*self.height+3)

def plotlib(self):

#this is the best I can do:

Xaxis=[cell.x for i in range(self.width) for cell in self.grid[i] if cell.state]

Yaxis=[cell.y for i in range(self.width) for cell in self.grid[i] if cell.state]

plt.axis([0,self.width, 0, self.height])

return plt.show(plt.scatter(Xaxis,Yaxis,c="000000", marker='s'))

def __getitem__(self,row):

return self.grid[row]

def pos_cell(self,coord):

#retrieves the cell given the coordinates

return self.grid[coord[0]][coord[1]]

def cell_state(self,coord):

#outputs the cell's state on given coord

return self.pos_cell(coord).state

def neighbors_state(self,coord,cont=0):

#sums the neghbors' cell state

cell=self.pos_cell(coord)

x,y=cell.x,cell.y

for hor in [-1,0,1]:

for ver in [-1,0,1]:

if not hor==ver==0 and (0<=x+hor

cont+=self.cell_state([(x+hor)%self.width,(y+ver)%self.height])

return cont

def alive_dead(self,coord):

#for each cell, tells wether it must switch its state or not

cell=self.pos_cell(coord)

if not cell.state:

if self.neighbors_state(cell.coord)==3:

return True

else:

return False

elif cell.state:

if self.neighbors_state(cell.coord) in [2,3]:

return True

else:

return False

#NB starting cells must be given as a list, not as a grid

def game_of_life(width,height,starting_cells):

#the heart of the script

test=[[Cell(i,j) for j in range(height)] for i in range(width)]

world=Mondo(width,height,starting_cells)

world.plotlib()

for i in range(world.width):

for j in range(world.height):

if world.alive_dead([i,j]):

test[i][j].turn_on()

starting_cells=[test[i][j] for i in range(width) for j in range(height)]

plt.pause(0.5)

return game_of_life(width,height,starting_cells)

#Example of an oscillator:

#print game_of_life(10,10,[Cell(1,2,1),Cell(2,3,1),Cell(3,1,1),Cell(3,2,1),Cell(3,3,1)])

#Example of the Gosper gun:

gosper=[[Cell(i,j) for j in range(70)] for i in range(30)]

for i in range(len(gosper)):

for j in range(len(gosper[0])):

if [i,j] in [[1,26],[2,24],[2,26],[3,14],[3,15],[3,22],[3,23],[3,36],[3,37],[4,36],[4,37],[4,22],[4,23],[4,13],[4,17],[5,12],[5,18],[5,22],[5,23],[5,1],[5,2],[6,1],[6,2],[6,12],[6,16],[6,18],[6,19],[6,24],[6,26],[7,26],[7,18],[7,12],[8,13],[8,17],[9,14],[9,15]]:

gosper[i][j].turn_on()

gosp=[gosper[i][j] for i in range(len(gosper)) for j in range(len(gosper[0])) if gosper[i][j].state]

print game_of_life(30,70,gosp)

#Mondo(30,70,gosp).plotlib()

#print Mondo(30,70,gosp)

我看了看matplotlib.animation但也许有太多的东西让我用在大学作业上。在

有没有什么模块可以用来打印一些简单的动画?我应该更努力吗matplotlib.pyplot,如果是这样,我如何调整轴比和其他东西?

浏览问题我发现的模块图形,可能有用吗?在

谢谢你

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值