简单版的Game Of Life游戏无法正常运行

giji676

这是完整的代码。我遗漏了一些不必要的东西

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语句可以工作(很难理解程序的行为很奇怪)。

拉比德76

规则的执行是正确的,但在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_squaresgrid

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

简单的猜谜游戏无法正常工作

Java简单RPG游戏(无法运行)

游戏无法正常运行

简单的Android SearchView无法正常运行

简单的bash无法通过cron正常运行

Python游戏无法正常运行

临时发行版Xcode 10,应用无法正常运行

我在pygame中的游戏无法正常运行

赛车游戏无法在pygame中正常运行

摇滚/剪纸/剪刀游戏无法正常运行

无法在 Elementary 发行版上编译简单的 C 程序

简单的CUDA向量搜索/索引程序无法正常运行

无法通过简单的验收测试即可正常运行

Android发行版无法正常运行。开发构建工作正常

网站在移动版 Firefox、移动版 iOS 和 Safari 上无法正常运行

我的简单按钮应用程序的Java程序无法正常显示v7版本

旧游戏在Windows 8中无法正常工作/ Windows 8中的旧游戏无法正常运行

人生游戏Parallel.For循环无法正常运行

第二循环,游戏无法正常运行,Sublime Text问题

基本的JavaScript剪刀布游戏无法正常运行

如何为运行简单的Web浏览器构建自定义发行版?

无法运行简单的世界

游戏“ Life”中发行版和调试版之间加速比的奇怪比例

简单用户脚本运行缓慢,有时无法正常工作

超级简单的嵌套Pythonic for-in循环无法正常运行...与输入有关

使用JS的简单用户名提示无法正常运行

uWSGI无法正常运行的简单Bottle应用程序(返回404)

酶:简单,无法正常工作

简单的Servlet无法正常工作