康威生活游戏

2024-04-20 02:26:11 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我读了康威人生游戏,并尝试用PyGame来实现它。在

我试着让它面向对象。它的工作方式是,我有一个单元实例的列表,然后根据它们的邻居来检查它们有多少个邻居,然后要么生存要么死亡。然后这个过程就会重复。在

问题是,当我用一些已知的启动模式(例如在下面的代码(CELL_MAP))测试它时,它并没有按照它应该的方式工作。在

我一遍又一遍地读代码,我真的不明白我在这里遗漏了什么。我发布了下面的整个代码,因为我不知道我的错误在哪里,但如果有人能给我指出正确的方向,我将不胜感激。在

提前谢谢!在

import pygame

class Cell:
    def __init__(self, live, xcor, ycor):
        self.alive = live
        self.x = xcor
        self.y = ycor
        self.neighbours = 0

def checkNeighbours(self, cellList):
    for cell in cellList:
        #left
        if cell.x == self.x-1 and cell.y == self.y and cell.alive == True:
            self.neighbours += 1        
        #right
        elif cell.x == self.x+1 and cell.y == self.y and cell.alive == True:
            self.neighbours += 1 
        #upleft
        elif cell.x == self.x-1 and cell.y == self.y-1 and cell.alive == True:
            self.neighbours += 1     
        #up
        elif cell.x == self.x and cell.y == self.y-1 and cell.alive == True:
            self.neighbours += 1 
        #upright
        elif cell.x == self.x+1 and cell.y == self.y-1 and cell.alive == True:
            self.neighbours += 1     
        #downleft
        elif cell.x == self.x-1 and cell.y == self.y+1 and cell.alive == True:
            self.neighbours += 1     
        #down
        elif cell.x == self.x and cell.y == self.y+1 and cell.alive == True:
            self.neighbours += 1 
        #downright
        elif cell.x == self.x+1 and cell.y == self.y+1 and cell.alive == True:
            self.neighbours += 1 

def breed(self):
    if self.alive == False and self.neighbours == 3:
        #dead cell ressurects if neighbours equals 3
        self.alive = True
    elif self.alive and self.neighbours < 2:
        #die from loneliness
        self.alive = False
    elif self.alive and self.neighbours == 2:
        #stay alive
        pass
    elif self.alive and self.neighbours == 3:
        #stay alive
        pass
    elif self.alive and self.neighbours > 3:
        #die from overpopulation
        self.alive = False

def render(self, display):
    if self.alive:
        pygame.draw.rect(display, (0,0,0), [self.x*10, self.y*10, 10, 10])
    elif self.alive == False:
        pygame.draw.rect(display, (0,0,255), [self.x*10, self.y*10, 10, 10])




WID = 33
HEI = 20            
CELL_MAP = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]

CELL_LIST = []

xc = -1
yc = -1
for yList in CELL_MAP:
    yc += 1
    for x in yList:
        xc += 1
        if x == 0:
            #create dead cell
            newCell = Cell(False, xc, yc)
            CELL_LIST.append(newCell)
        elif x == 1:
            #create alive cell
            newCell = Cell(True, xc, yc)
            CELL_LIST.append(newCell)
    xc = -1



#pygame init

pygame.init()
(width, height) = (WID*10, HEI*10)

pygame.display.set_caption('Game of Life')

screen = pygame.display.set_mode((width, height))

#game loop

def gameLoop():
    gameLoop = True 

    while gameLoop:
        #check for exit
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameLoop = False
                pygame.quit()               

        #render cells
        for cell in CELL_LIST:
            cell.render(screen)    

        #check neighbours
        for cell in CELL_LIST:
            cell.checkNeighbours(CELL_LIST)

        pygame.display.flip()

        #breed
        for cell in CELL_LIST:
            cell.breed()

        pygame.time.wait(5)

    quit()

if __name__ == "__main__":
    gameLoop()

Tags: andinselffalsetrueforifdisplay
1条回答
网友
1楼 · 发布于 2024-04-20 02:26:11

我没有安装pygame,所以无法运行您的代码。然而,导致错误的错误是,在确定了下一代细胞是活的还是死的之后,没有将它的邻居计数重置为零。每一代邻居的新计数都会被累加到上一代的每一个新的计数中。您可能应该在.breed方法中进行重置。在

以下是该方法的更精简版本:

def breed(self):
    self.alive = self.neighbours == 3 or self.alive and self.neighbours == 2
    self.neighbours = 0

我还有一些关于你的代码的评论。在

您的checkNeighbours方法效率非常低:对于每个单元格,它会扫描整个网格来寻找单元格的邻居!一个简单的替代方法是将单元格存储在二维列表中,以便快速找到单元格的邻居。在


这里有一种比当前代码更简洁的方法来构建CELL_LIST

^{pr2}$

这和列表理解是一样的:

CELL_LIST = [Cell(bool(v), x, y)
    for y, row in enumerate(CELL_MAP)
        for x, v in enumerate(row)
]

但正如我前面所说,制作一个2D列表可能是个好主意:

cell_list = [[Cell(bool(v), x, y) for x, v in enumerate(row)]
    for y, row in enumerate(CELL_MAP)]

您的CELL_MAP不是将生命模式放入程序中的方便方法,但我想它对于测试目的来说是可以的。看看我这个月早些时候为另一个选择写的this answer。在

最后,您应该让您的程序能够读取许多Life程序使用的公共RLE format。在

您还可以查看我编写的这个使用Numpy:numpy_life.py的中等效率版本。与我链接的另一个版本一样,它在Linux终端中显示输出,但是这两个版本都应该很容易适应pygame或其他GUI框架。在

相关问题 更多 >