Python 中的元胞自动机模拟错误

Error with a cellular-automata simulation in Python

我正在尝试使用 Python 及其库“pygame”构建著名的康威“生命游戏”。

这是模拟的维基百科页面:https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life

代码:https://pastebin.com/kKGvshVK

(我 post 它也在问题的末尾,因为网站告诉我我必须输入一些代码)

我创建了一个长代码,应该按顺序执行以下操作:

现在,一些技术性的东西。我创建了确定邻居数量的函数,因为我不理解(当然)应该帮助我制作这个游戏的简单算法(摩尔算法)。所以事情是,我从头开始制作这个函数并且它很长,并且在它的开头有多个例外我为了考虑何时将分析单元格以确定它放置在屏幕的边框或角落等。 如果这段代码有点乱,请见谅。

现在的问题是,我想我已经成功初始化了单元格,但是,单元格没有更新,也没有错误。我认为在主循环中发生了一些阻碍流程的事情,因为应该打印“算法成功”的打印函数没有出现,所以有一个我不明白的错误。就这些,谢谢你的任何回答!

#Conway Game of Life

import pygame
import random


#PYGAME INITIALIZATION
success, failure = pygame.init()

screen_width = 800
screen_height = 600

screen = pygame.display.set_mode((screen_width, screen_height)) #Init the screen
time = pygame.time.Clock() #Time from startup
FPS = 5

#Screen Area = 480000 px (width * height)
#Area of a cell = 100px --> 4800 Cell

BLACK = (0, 0, 0)#Live cell
WHITE = (255, 255, 255)#dead cell


class Cell:
    """ x: x coordinate
        y: y coordinate
        size: width and height (same)
        alive: int (boolean, 0 o 1), to track the status of a cell (live or dead), at the startup is random
    """
    def __init__(self, x, y, alive):
        self.x = x
        self.y = y
        self.size = 10 #it's a square
        self.alive = alive
        if self.alive == 1:
            self.color = BLACK
        elif self.alive == 0:
            self.color = WHITE

#Function needed in the next function ------------------------------------------------
def checkAlive(cell, cellArray, curr_x, curr_y, counter):
    """ Check wheter the current cell near the original cell is alive. If it is alive it adds 1 to the counter
        cell: instance of the original cell
        cellArray: cell list with all the initialized cells
        curr_x: x coordinate of the cell which will be examined
        curr_y: y coordinate of the cell which will be examined
        counter: variable that is updated whenever a cell near to original has the "alive" attribute == 1
    """

    for current_cell in cellArray:
        if (current_cell.x == curr_x and current_cell.y == curr_y):
            if (current_cell.alive == 1):
                counter += 1


#Function to find the neighbours of a cell ---------------------------------------------------

def neighbour(cells, cell):
    """Give as output the number of neighbours of a cell (only neighbours with the alive attribute = 1)
        cells: List containing all the instances of the initialized cells
        cell: The single instance of cell which will be examined to determine the number of live neighbours
        return: number of live neighbours
    """
    num_neighbours = 0 #Number of near live cells(Moore neighbourhood)
    x = cell.x
    y = cell.y

    #List of exceptions before the main algorithm
    #Upper-left corner (x = 0, y = 0) !*!*!*!*!*!*!*!*!**!*!*!*!*!*!*!*
    if (x == 0 and y == 0):
        #Cell on the right -----------
        current_x = 1
        current_y = 0

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell below current ----------------------------------------
        current_x = 1
        current_y = 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell below original cell
        current_x = 0
        current_y = 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Return the number of neighbours
        return num_neighbours

    #Upper-right corner (x = window, y = 0)!*!*!*!*!**!*!*!*!*!*!*!*!*!*!*!*!*!*!**!*!*!*!*!*!*!*!
    elif (x == screen_width - cell.size and y == 0):
        #Cell below -------------------------------------
        current_x = screen_width - cell.size
        current_y = 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the left of current -----------------------------------
        current_x -= 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the left of original
        current_y = 0

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Return the number of neighbours
        return num_neighbours

    #Lower-left corner (x = 0, y = window) !*!*!*!**!*!!*!**!*!!**!*!*!*!*!*
    elif(x == 0 and y == (screen_height - cell.size)):

        #Cell over original ----------------------
        current_x = 0
        current_y = (screen_height - cell.size) - 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the right of current ------------------------------------------
        current_x += 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell below current ---------------------------------------------
        current_y += 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Return the number of neighbours
        return num_neighbours



    #Lower right corner !*!*!*!*!*!!*!*!*!*!*!*!**!!*!*!*
    elif (x == (screen_width - cell.size) and y == (screen_height - cell.size)):

        #Cell to the left of original ------------------------------------------------
        current_x = (screen_width - cell.size) - 1
        current_y = screen_height - cell.size

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell on top of current -------------------------------------------------------
        current_y -= 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the right of current
        current_x += 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Return the number of neighbours
        return num_neighbours


    #If the cell is in the first row (y = 0) (2 corners excluded) !*!*!*!*!*!!*!!*!*!*!*!
    elif (y == 0 and (x != 0 and x != (screen_width - cell.size))):
        #Cell to the right of original
        current_x = x + 1
        current_y = 0

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell below current
        current_y += 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell below original
        current_x = x

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the left of current
        current_x -= 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the left of original
        current_y -= 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Return the number of neighbours
        return num_neighbours


    #If the cell is in the last row (y = screen_height) 2 corners excluded !*!*!*!*!*!*!*!!*!*
    elif (y == (screen_height - cell.size) and (x != 0 and x != (screen_width - cell.size))):
        #Cell to the left of original
        current_x = x - 1
        current_y = y

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell on top of current
        current_y -= 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the right of current
        current_x += 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the right of current
        current_x += 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell below current
        current_y += 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Return the number of neighbours
        return num_neighbours


    #If the cell is in the first column (2 corners excluded) !*!*!*!*!*!*!*!*!*!*!*!*
    elif (x == 0 and (y != 0 and y != (screen_height - cell.size))):
        #Cell on top of original
        current_x = x
        current_y = y - 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the right of current
        current_x += 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell below current
        current_y += 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell below current
        current_y += 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the left of current
        current_x -= 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)


        return num_neighbours


    #If the cell is in the last column (x = screen width) !*!*!*!*!*!*!*!!**!!*
    elif (x == (screen_width - cell.size) and (y != 0 and y != (screen_height - cell.size))):
        #Cell below original
        current_x = x
        current_y = y + 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the left of current
        current_x -= 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell on top of current
        current_y -= 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell on top of current
        current_y -= 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the right of current
        current_x += 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        return num_neighbours


    #GENERAL RULE
    else:
        #8 Neighbours
        #Cell on top of original
        current_x = x
        current_y = y - 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the right of original
        current_x += 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell below current
        current_y += 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell below current
        current_y += 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the left of current
        current_x -= 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell to the left of current
        current_x -= 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell on top of current
        current_y -= 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        #Cell on top of current
        current_y -= 1

        checkAlive(cell, cells, current_x, current_y, num_neighbours)

        return num_neighbours




#CELL INITIALIZATION
cell_array = []
#Useful variable in the for loop
x = 0
y = 0
init = False #Become true when Initialization is completed


#Initialization
while not init:

    is_alive = random.choices([0,1], weights = (95, 5), k=1)[0]#Randomly spawn cells with probability (Dead 95%, Alive 5 %)
    cell = Cell(x, y, is_alive)#Single object
    x += cell.size
    cell_array.append(cell)
    if x == screen_width: #End of a row
        x = 0
        y += cell.size
    if y == screen_height:#Last row
        init = True


#DRAWING CELLS
for cl in cell_array:
    pygame.draw.rect(screen, cl.color, pygame.Rect(cl.x, cl.y, cl.size, cl.size))#Draw any single cell

pygame.display.flip() #To update the screen

#Debug
print("Initialization Completed.")


done = False #Check whether the program should run

#Main loop
while not done:
    #FPS
    time.tick(FPS)

    #EVENT HANDLER
    for event in pygame.event.get():
        if event.type == pygame.QUIT: #Exit button
            print("Quitting.")
            done = True



    #SIMULATION --------------------------------------------------------------------

    #Run the algorithm of the game and update the screen (Moore algorithm)
    for cell in cell_array:
        if neighbour(cell_array, cell) in (2, 3): #2 or 3 live neighbours (survive)
            cell.alive = 1
        elif neighbour(cell_array, cell) < 2: #Few than 2 live neighbours (dies)
            cell.alive = 0
        elif neighbour(cell_array, cell) > 3: #More than 3 live neighbours (dies)
            cell.alive = 0
        elif ((cell.alive == 0) and (neighbour(cell_array, cell) == 3)): #Dead cell with 3 live neigh (live)
            cell.alive == 1

    #Debug
    print("Algorithm succesful.")

    #DRAWING CELLS
    for cl in cell_array:
        pygame.draw.rect(screen, cl.color, pygame.Rect(cl.x, cl.y, cl.size, cl.size))
    #Debug
    print("Cell loaded to the screen")

    pygame.display.flip() #To update the screen

这里有很多错误。您应该逐步检查这些功能是否正常工作。几个例子:

  1. checkAlive 方法可能应该检查一些东西并且 return 原始单元格附近的当前单元格是否像你写的那样活着。它没有 return 任何东西。相反,它改变了 counter 的值,它不是 returned
  2. 可能您希望 counter 变量在 checkAlive 函数中与 neighbour 函数中的变量 num_neighbours 相同 - 不,它们是不同的原语。
  3. 当您更改 alive 属性 - 您没有将颜色设置为正确的颜色,因此 alive 状态会改变,但颜色不会改变。您应该在设置颜色的地方为 alive 创建一个 setter。

当你改完这些问题后,你可能会通过代码,我们会考虑还有什么问题。