康威的生命游戏 - 规则

时间:2016-11-12 18:39:22

标签: python arrays python-3.x pygame conways-game-of-life

下面是康威生命游戏的代码。我目前正在努力解决不正确的功能 - 细胞仍在繁殖而不是灭绝或收敛点。我认为规则功能出了问题(我认为,特定规则可以吗?),但我无法弄清楚。如果你知道出了什么问题,我将非常感谢你的帮助。感谢

import pygame
import sys
import random
from pygame.locals import *

FPS = 10
fpsClock = pygame.time.Clock()

WINDOWSIZE = 500
CELLSIZE = 5
assert WINDOWSIZE % CELLSIZE == 0, "win size must be a multiple of cell"


class Board():

    def __init__(self):

        pygame.init()
        pygame.display.set_caption('Game of Life')
        self.DISPLAYSURF = pygame.display.set_mode((WINDOWSIZE, WINDOWSIZE))
        self.grid = [[0] * (WINDOWSIZE // CELLSIZE) for i in range(WINDOWSIZE // CELLSIZE)]

    def draw(self):
        i = 0
        j = 0

        for x in range(0, WINDOWSIZE, CELLSIZE):
            for y in range(0, WINDOWSIZE, CELLSIZE):

                if self.grid[i][j] == 0:
                    pygame.draw.rect(
                    self.DISPLAYSURF, (20, 120, 20), Rect((x, y), (CELLSIZE, CELLSIZE)))

                else:
                    pygame.draw.rect(
                    self.DISPLAYSURF, (255, 255, 255), Rect((x, y), (CELLSIZE, CELLSIZE)))

                if j == (WINDOWSIZE // CELLSIZE) - 1:
                    j = 0

                else:
                    j = j + 1

            i = i + 1

    def randomize(self):
        for i in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):
            for j in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):

                if random.randint(0, 100) < 15:
                    self.grid[i][j] = 1


    def rules(self):
        for i in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):
            for j in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):

                neighbors = 0

                if self.grid[i][j] == 0:
                    neighbors = self.grid[i + 1][j] + self.grid[i - 1][j] + self.grid[i][j + 1] + self.grid[i][j - 1] + self.grid[i - 1][j - 1] + self.grid[i + 1][j + 1] + self.grid[i + 1][j - 1] + self.grid[i - 1][j + 1]

                    if neighbors == 3:
                        self.grid[i][j] = 1
                        continue
                    else:
                        self.grid[i][j] = 0

                if self.grid[i][j] == 1:
                    neighbors = self.grid[i + 1][j] + self.grid[i - 1][j] + self.grid[i][j + 1] + self.grid[i][j - 1] + self.grid[i - 1][j - 1] + self.grid[i + 1][j + 1] + self.grid[i + 1][j - 1] + self.grid[i - 1][j + 1]

                    if neighbors < 2:
                        self.grid[i][j] = 0

                    elif neighbors > 3:
                        self.grid[i][j] = 0

                    else:
                        self.grid[i][j] = 1


board = Board()
board.randomize()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    board.rules()
    board.draw()
    pygame.display.update()
    fpsClock.tick(FPS)

2 个答案:

答案 0 :(得分:0)

您的错误 - 当您仍需要原始值来计算其他单元格时,您可以更改grid中单元格中的值。您无法更改oryginal grid中的值。您必须在新new_grid中添加新值并在结尾处替换网格。

import pygame
import sys
import random

# --- constanst ---

FPS = 10
WINDOWSIZE = 500
CELLSIZE = 5

assert WINDOWSIZE % CELLSIZE == 0, "win size must be a multiple of cell"

# --- classes ---

class Board():

    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((WINDOWSIZE, WINDOWSIZE))
        pygame.display.set_caption('Game of Life')

        self.grid = [[0] * (WINDOWSIZE // CELLSIZE) for i in range(WINDOWSIZE // CELLSIZE)]


    def draw(self):

        for i, x in enumerate(range(0, WINDOWSIZE, CELLSIZE)):
            for j, y in enumerate(range(0, WINDOWSIZE, CELLSIZE)):

                if self.grid[i][j] == 0:
                    color = (20, 120, 20)
                else:
                    color = (255, 255, 255)

                pygame.draw.rect(self.screen, color, pygame.Rect((x, y), (CELLSIZE, CELLSIZE)))


    def randomize(self):

        for i in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):
            for j in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):
                if random.randint(0, 100) < 15:
                    self.grid[i][j] = 1


    def rules(self):
        # create new grid
        new_grid = [[0] * (WINDOWSIZE // CELLSIZE) for i in range(WINDOWSIZE // CELLSIZE)]

        # put results in new grid
        for i in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):
            for j in range(CELLSIZE, (WINDOWSIZE // CELLSIZE) - CELLSIZE):

                neighbors = self.grid[i + 1][j] + self.grid[i - 1][j] + self.grid[i][j + 1] + self.grid[i][j - 1] + self.grid[i - 1][j - 1] + self.grid[i + 1][j + 1] + self.grid[i + 1][j - 1] + self.grid[i - 1][j + 1]

                if self.grid[i][j] == 0:
                    if neighbors == 3:
                        new_grid[i][j] = 1
                    else:
                        new_grid[i][j] = 0
                elif self.grid[i][j] == 1:
                    if neighbors < 2:
                        new_grid[i][j] = 0
                    elif neighbors > 3:
                        new_grid[i][j] = 0
                    else:
                        new_grid[i][j] = 1

        # replace grid
        self.grid = new_grid


    def mainloop(self):
        fps_clock = pygame.time.Clock()

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        pygame.quit()
                        sys.exit()

            self.rules()
            self.draw()
            pygame.display.update()

            fps_clock.tick(FPS)        

# --- main ---

board = Board()
board.randomize()
board.mainloop()

答案 1 :(得分:0)

在移动到下一代之前,请勿修改单元格!以下是您需要遵循的简单算法。您可以查看详细的实施和视频演示here

  1. 创建一个代表您的单元格的网格。
  2. 细胞可以死亡或活着。所以有两个css类,一个用于活着,一个用于死亡。
  3. 找出一个单元格的邻居。
  4. 应用上述规则,并确定下一代细胞是活着还是死亡。
  5. 将需要移动的单元格存储到下一代。
  6. 存放不会存活或将在下一代死亡的细胞。
  7. 迭代移动到下一代的单元格列表,并通过向它们添加活动css类将它们标记为活着
  8. 迭代将在下一代死亡的单元格列表,并通过向它们添加死css类将它们标记为死亡。
  9. 在一小段时间后再次从3-8开始执行步骤。