PyGame中的康威生命游戏没有生成正确的图案

1 投票
1 回答
1342 浏览
提问于 2025-04-18 05:54

我正在用Python和Pygame制作康威的生命游戏,但我发现生成的图案完全不对劲。我检查了代码无数遍,但还是找不到问题出在哪里。

这是其中一个生成状态的截图:

gameoflife

下面是这个游戏的完整代码。

import pygame, sys
from pygame.locals import *
from random import randint
import numpy

#inititalize
pygame.init()
clock = pygame.time.Clock()

#constants
FPS = 10
BLACK = (0,0,0)
RED = (255,0,0)
GREY = (30,30,30)
SCREENX = 640
SCREENY = 480
CELLSIZE = 10
HEIGHT = SCREENY/CELLSIZE
WIDTH = SCREENX/CELLSIZE

#set up window
window = pygame.display.set_mode((SCREENX, SCREENY))
pygame.display.set_caption('Game of Life')
window.fill(BLACK)

#generate random seed
cells = numpy.zeros((WIDTH,HEIGHT), dtype=numpy.int)
for x in range(0,WIDTH):
    for y in range(0,HEIGHT):
        #0 is a dead cell, 1 is an alive cell
        cells[x][y] = randint(0,1)

def findNeighbors(grid, x, y):
    if 0 < x < len(grid) - 1:
        xi = (0, -1, 1)
    elif x > 0:
        xi = (0, -1)
    else:
        xi = (0, 1)

    if 0 < y < len(grid[0]) - 1:
        yi = (0, -1, 1)
    elif y > 0:
        yi = (0, -1)
    else:
        yi = (0, 1)

    for a in xi:
        for b in yi:
            if a == b == 0:
                continue
            yield grid[x + a][y + b]

def update(grid, x, y):
    #determine num of living neighbors
    neighbors = findNeighbors(cells,x,y)
    alive = 0
    for i in neighbors:
        if i == 1:
            alive+=1

    #if current cell is alive
    if grid[x][y] == 1:
        #kill if less than 2 or more than 3 alive neighbors
        if (alive < 2) or (alive > 3):
            return 0
        else:
            return 1
    #if current cell is dead
    elif grid[x][y] == 0:
        #make alive if 3 alive neighbors
        if alive == 3:
            return 1
        else:
            return 0

#main loop
while True:

    #check if user wants to exit
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    #update cells
    for x in range(0,WIDTH):
        for y in range(0,HEIGHT):
            cells[x][y] = update(cells,x,y)

    #draw grid
    for x in range(0,SCREENX,CELLSIZE):
        for y in range(0,SCREENY,CELLSIZE):
            #if cell is alive
            if cells[x/CELLSIZE][y/CELLSIZE] == 1:
                #draw red square
                pygame.draw.rect(window, RED, [x, y, CELLSIZE, CELLSIZE])
            else:
                #draw black square
                pygame.draw.rect(window, BLACK, [x, y, CELLSIZE, CELLSIZE])
            #draw square border
            pygame.draw.rect(window, GREY, [x, y, CELLSIZE, CELLSIZE], 1)

    #draw updates
    pygame.display.update()

    #generations per second
    clock.tick(FPS)

我觉得问题不在于findNeighbors这个函数,因为我是在这个StackOverflow的回答中找到的。所以我认为问题可能出在update函数上,但根据游戏规则,我看不出逻辑哪里出错了。

1 个回答

6

我觉得问题可能出在这里:

cells[x][y] = update(cells,x,y)

你只有一个网格,而你在还在用这个网格计算的时候就更新它。第n+1代只应该考虑第n代的信息——但现在你却混合了第n代和第n+1代的信息。这样一来,你会得到上面和左边的邻居的值,而右边和下面的邻居却是值,因为它们还没有被重新计算。

举个例子,看看这个模式(#表示“活着”):

...
###
...

这个应该变成:

.#.
.#.
.#.

...但实际上,你会得到(我想):

.##
#.#
... // Bottom-middle only has one live neighbour at computation time

当我们计算右上角的时候,它有三个邻居。左中间在计算时有2个活着的邻居;中间有4个,右中间有2个。到计算底行的时候,没有任何一个有三个邻居,所以它们保持死亡状态。

通常情况下,康威的生命游戏实现会为每一代计算一个全新的网格,或者在两个网格之间切换,从一个网格计算出另一个网格的所有内容。

撰写回答