我正在努力构建著名的"《生命的游戏》;Conway使用Python及其库";pygame";。
这是模拟的维基百科页面:https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
代码:https://pastebin.com/kKGvshVK
(我也把它贴在问题的最后,因为网站告诉我我必须放一些代码(
我创建了一个长代码,应该按顺序执行以下操作:
- 初始化pygame库和窗口
- 游戏基于2D网格,由每个大小相同的单元格组成
- 创建一个类"Cell";包括每个单元格的属性和特征;基础是";"活着";确定细胞是活的(因此屏幕中为黑色(还是死的(白色(的属性
- 创建一个函数
checkAlive
,用于检查单元格列表中的某个单元格是否处于活动状态 - 创建第二个函数
neighbour
,在输出中给出邻居的数量(使用Moore邻居理论(,其中使用checkAlive
- while循环中的单元初始化
- 带有事件处理程序和算法的主循环应该按照生活游戏的规则更新屏幕
现在,一些技术性的东西。我创建了确定邻居数量的函数,因为我(肯定(不理解一个简单的算法(moore算法(来帮助我制作这个游戏。事情是这样的,我从头开始做这个函数,它很长,在它开始的时候,我做了多个例外,以考虑何时将要分析的单元格放在屏幕的边界或角落上,以确定它的邻居数量。所以,如果这个代码有点乱,请原谅我。
现在,问题是,我想我已经成功初始化了单元格,但是,单元格s没有更新,也没有错误。我认为在主循环中,它正在发生一些阻塞流的事情,因为应该打印的打印函数";算法成功";没有出现,所以有一个我不理解的错误。仅此而已,谢谢您的回答!
#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
这里有很多错误。您应该逐步检查这些功能是否正常工作。几个例子:
checkAlive
方法可能应该检查一些内容,并返回原始单元格附近的当前单元格是否如您所写的那样是活动的。它什么也不回。相反,它会更改未返回的counter
的值- 您可能希望
counter
变量在checkAlive
函数中与neighbour
函数中的变量num_neighbours
-nope相同,它们是不同的基元 - 当您更改
alive
属性时,您没有将颜色设置为正确的颜色,因此alive
状态会更改,但颜色不会更改。你应该在你设置颜色的地方为alive创建一个setter
当您完成更改这些问题时,您可能会传递代码,我们会认为还有什么问题。