Python模块,用于绘制游戏的生活脚本

Python模块,用于绘制游戏的生活脚本,python,matplotlib,conways-game-of-life,Python,Matplotlib,Conways Game Of Life,我想知道您推荐用于在X-Y平面上绘制坐标列表的模块是什么。目的是实现生命的游戏;我试过matplotlib.pyplot,但我在轴比率方面遇到了一些问题,一般来说,这似乎不是绘制某种动画的最佳方式。该脚本的主要思想是用time.sleep给出的记号来描绘细胞生活的世界 import matplotlib.pyplot as plt class Cell: #Cell class, with coordinates and an ON-OFF state def __init__

我想知道您推荐用于在X-Y平面上绘制坐标列表的模块是什么。目的是实现生命的游戏;我试过matplotlib.pyplot,但我在轴比率方面遇到了一些问题,一般来说,这似乎不是绘制某种动画的最佳方式。该脚本的主要思想是用time.sleep给出的记号来描绘细胞生活的世界

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<self.width and 0<=y+ver<self.height):
                    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.pyplot作为plt
类别单元格:
#单元类,具有坐标和开关状态
定义初始化(self,x=0,y=0,state=0):
self.state=状态
self.x=x
self.y=y
self.coord=[self.x,self.y]
定义报告(自我):
如果是自我状态:
返回“*”
其他:
返回“”
#打开和关闭单元格的方法:
def开启(自):
自我状态=1
回归自我
def关闭(自):
self.state=0
回归自我
蒙多班:
#细胞生存的世界:
def uuu init uuuu(self,宽度=10,高度=30,起始单元格=[]):
自身宽度=宽度
自我高度=高度
self.grid=[[范围(高度)中j的单元格(i,j)]范围(宽度)中i的单元格(i,j)]
对于起始单元格中的单元格:
self.grid[cell.x][cell.y]=单元格
定义报告(自我):
#如果我只想使用print语句而不是matplotlib,就需要这个方法
对于self.grid[row]]+[“[self.width]”范围内的行,返回“|”*(2*self.height+3)+“\n”+“\n.join”([“”.join([“|”])+[“”+str(cell)++”(2*self.height+3)
def plotlib(自身):
#这是我能做的最好的:
Xaxis=[cell.x表示self.grid[i]中单元格的范围内的i(self.width)如果cell.state]
Yaxis=[cell.y表示self.grid[i]中单元格的范围内的i(self.width)如果cell.state]
plt.轴([0,自宽,0,自高])
返回plt.show(plt.scatter(Xaxis,Yaxis,c=“000000”,marker='s'))
定义获取项目(自身,行):
返回self.grid[行]
def pos_单元(自身、协调):
#检索给定坐标的单元格
返回self.grid[coord[0]][coord[1]]
def单元_状态(自身、协调):
#在给定坐标上输出单元格的状态
返回自身位置单元(坐标)状态
def邻居_状态(自身、协调、连续=0):
#对负电池状态求和
单元=自身位置单元(坐标)
x、 y=单元x,单元y
对于[-1,0,1]中的hor:
对于[-1,0,1]中的版本:

如果不是hor==ver==0和(0Hi,欢迎来到StackOverflow!请注意,询问工具建议的问题在这里通常是不受欢迎的。您能否重新表述您的问题,以显示您迄今为止尝试了什么以及您遇到了什么困难?有关此方面的更多信息,请查看您可以检查我曾经执行过的实现。