gpt4 book ai didi

python - PyGame 中的 Conways 生命游戏

转载 作者:行者123 更新时间:2023-11-28 16:20:33 25 4
gpt4 key购买 nike

所以我阅读了 Conways Game of Life 并尝试使用 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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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,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()

最佳答案

我没有安装pygame,所以我不能运行你的代码。但是,导致错误的错误是在确定细胞在下一代中是存活还是死亡后,您没有将细胞的邻居计数重置为零。因此,在每一代中,每个单元格的新邻居计数都会添加到先前累积的邻居计数中。您可能应该在 .breed 方法中进行重置。

这是该方法的一个更紧凑的版本:

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

我对您的代码还有一些意见。

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


这里有一个比您的代码当前所做的更紧凑的方法来构建您的 CELL_LIST:

CELL_LIST = []
for y, row in enumerate(CELL_MAP):
for x, v in enumerate(row):
CELL_LIST.append(Cell(v == 1, x, y))

这里和列表理解是一样的:

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

但正如我之前所说,将 CELL_LIST 设为二维列表可能是个好主意:

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

您的 CELL_MAP 不是将生活模式放入您的程序的便捷方式,但我想它可以用于测试目的。看看this answer我本月早些时候写信寻求替代方案。

最终,您应该让您的程序能够读取常见的 RLE format被许多 Life 程序使用。

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

关于python - PyGame 中的 Conways 生命游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40574010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com