Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
812 views
in Technique[技术] by (71.8m points)

python - Conway's Game of life pygame implementation not working with a copy of the grid, not using numpy

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()

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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[:])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...