这是完整的代码。我遗漏了一些不必要的东西
import random
import pygame
FPS = 1
WIDTH, HEIGHT = 400, 400
RESOLUTION = 40
GRAY = (200, 200, 200)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game of Life")
def draw_grid(win, cols, rows):
for i in range(cols):
for j in range(rows):
x = i * RESOLUTION
y = j * RESOLUTION
pygame.draw.rect(win, GRAY, (x, y, RESOLUTION, RESOLUTION), 1)
def make_2d_array(cols, rows):
arr = []
for i in range(cols):
arr.append([])
for j in range(rows):
arr[i].append(0)
return arr
def count_neighbours(grid, x, y):
neighbourCount = 0
for i in range(-1, 2):
for j in range(-1, 2):
neighbourCount += grid[x + i][y + j]
return neighbourCount
def draw_squares(win, grid, cols, rows):
#nextA = make_2d_array(cols, rows)
nextA = grid
for i in range(len(grid)):
for j in range(len(grid[i])):
x = i * RESOLUTION
y = j * RESOLUTION
if grid[i][j] == 1:
pygame.draw.rect(win, WHITE, (x, y, RESOLUTION, RESOLUTION))
elif grid[i][j] == 0:
pygame.draw.rect(win, BLACK, (x, y, RESOLUTION, RESOLUTION))
for i in range(cols):
for j in range(rows):
if i == 0 or i == cols-1 or j == 0 or j == rows-1:
nextA[i][j] = grid[i][j]
else:
state = grid[i][j]
neighbours = count_neighbours(grid, i, j)
if state == 0 and neighbours == 3:
nextA[i][j] = 1
elif state == 1 and (neighbours < 2 or neighbours > 3):
nextA[i][j] = 0
else:
nextA[i][j] = state
grid = nextA
def main():
run = True
clock = pygame.time.Clock()
cols = int(WIDTH / RESOLUTION)
rows = int(HEIGHT / RESOLUTION)
grid = make_2d_array(cols, rows)
"""for i in range(cols):
for j in range(rows):
grid[i][j] = random.randint(0, 1)"""
#glider test
grid = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 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, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
draw_squares(WIN, grid, cols, rows)
draw_grid(WIN, cols, rows)
pygame.display.update()
pygame.quit()
main()
我遵循的是用Java或JavaScript编写的教程,但是规则是相同的,应该可以使用,但不能使用。简化的规则应为:
if state == 0 and neighbours == 3:
nextA[i][j] = 1
elif state == 1 and (neighbours < 2 or neighbours > 3):
nextA[i][j] = 0
else:
nextA[i][j] = state
但是当我运行代码时,我很确定第一个if语句可以工作(很难理解程序的行为很奇怪)。
规则的执行是正确的,但在Conway的《人生游戏》中,您必须每转一个步骤创建一个新的空网格。必须根据当前网格中的字段和演化规则来确定新网格中的字段。
在中创建一个新的空网格draw_squares
,但从函数返回新网格:
def draw_squares(win, grid, cols, rows):
# nextA = grid # <--- DELETE
nextA = make_2d_array(cols, rows) # <--- ADD
# [...]
return nextA # <-- return the new grid
使新电网当前电网通过分配新的网格通过返回draw_squares
到grid
:
grid = draw_squares(WIN, grid, cols, rows)
另外,邻居的计算是错误的。字段不是其自身的邻居:
def count_neighbours(grid, x, y):
neighbourCount = 0
for i in range(-1, 2):
for j in range(-1, 2):
if i != 0 or j != 0:
neighbourCount += grid[x + i][y + j]
return neighbourCount
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句