问题描述
我正在尝试使用 pygame 在 python 中实现康威的生活游戏.但是,主循环中每个单元格的计算似乎都有问题.为确保每个计算同时完成,我创建了网格的副本并在该网格上进行了计算.尽管如此,该程序不起作用,我无法弄清楚.这是我的代码的副本.
I'm trying to implement Conway's game of life in python using pygame. However, it seems there is a problem with the calculation of each cell in the main loop. To ensure each calculation is done at the same time, I created a copy of the grid and got the calculations on that grid. Nevertheless, the program is not working and I can't figure it out. Here is a copy of my code.
import pygame
import time
def create_grid(ROWS, COLS, SCREEN):
"""Creates a grid, sets all values to 0"""
assert ROWS > 0, "ROWS must be greater than 0"
assert COLS > 0, "COLS must be greater than 0"
grid = []
for i in range(ROWS):
grid.append([])
for j in range(COLS):
grid[i].append(0)
return grid
pygame.init()
#SCREEN setup
ScreenHeight = 700
ScreenWidth = 700
SCREEN_COLOR = (20, 20, 20)
SCREEN = pygame.display.set_mode((ScreenWidth, ScreenHeight)) #Create Screen
SCREEN.fill(SCREEN_COLOR)
#Number of ROWS and COLUMNS
ROWS = 30
COLS = 40
#How far will the next cube be placed
SQUARESTEPY = ScreenWidth / ROWS
SQUARESTEPX = ScreenWidth / COLS
GREY = (70, 70, 70)
WHITE = (255, 255, 255)
#draw grid
grid = create_grid(ROWS, COLS, SCREEN)
# grid[0][0] = 1
# grid[1][0] = 1
# grid[0][1] = 1
while True:
#create a copy of the grid to calculate the condition of all cells at the same time
copy_of_grid = grid[:]
for ev in pygame.event.get():
#Quit the game
if ev.type == pygame.QUIT:
pygame.quit()
#if mouse click draws or erases a cell
if pygame.MOUSEBUTTONDOWN == ev.type:
posX, posY = pygame.mouse.get_pos()
print(posX, posY)
posX, posY = int(posX / SQUARESTEPX), int(posY / SQUARESTEPY)
grid[posY][posX] = 1 - grid[posY][posX]
#calculate conway's rules and draw each cell
for y in range(ROWS):
for x in range(COLS):
neighbors = copy_of_grid[(y - 1) % ROWS][(x - 1) % COLS] + \
copy_of_grid[y % ROWS][(x - 1) % COLS] + \
copy_of_grid[(y + 1) % ROWS][(x - 1) % COLS] + \
copy_of_grid[(y - 1) % ROWS][x % COLS] + \
copy_of_grid[(y + 1) % ROWS][x % COLS] + \
copy_of_grid[(y - 1) % ROWS][(x + 1) % COLS] + \
copy_of_grid[y % ROWS][(x + 1) % COLS] + \
copy_of_grid[(y + 1) % ROWS][(x + 1) % COLS]
#print(x, y, "neighbors: {}, ON: {}".format(neighbors, grid[y][x]))
#A dead cell surrounded by exactly 3 cells will revive
if copy_of_grid[y][x] == 0 and (neighbors == 3 or neighbors == 2):
grid[y][x] = 1
#A living cell surrounded by less than 2 or more than 3 neighbors wil die
elif grid[y][x] == 1 and (neighbors < 2 or neighbors > 3):
grid[y][x] = 0
#paint
if grid[y][x] == 1:
pygame.draw.rect(SCREEN, WHITE, (SQUARESTEPX * x, SQUARESTEPY * y, SQUARESTEPX, SQUARESTEPY))
else:
pygame.draw.rect(SCREEN, SCREEN_COLOR , (SQUARESTEPX * x, SQUARESTEPY * y, SQUARESTEPX, SQUARESTEPY))
pygame.display.flip()
time.sleep(0.1)
pygame.quit()
推荐答案
copy_of_grid = grid[:]
不是网格的副本.它是外部列表的浅拷贝(参见列表).但是列表copy_of_grid
中的元素仍然与列表grid
中的元素相同.
您必须在循环中复制嵌套列表:
copy_of_grid = grid[:]
is not a copy of the grid. It is a shallow copy of the outer list (see Lists). But the elements in the list copy_of_grid
are still the same as the elements in the list grid
.
You have to copy the nested list in a loop:
copy_of_grid = []
for row in grid:
copy_of_grid.append(row[:])
这篇关于康威的生命游戏 pygame 实现不使用网格副本,不使用 numpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!