【问题标题】:I'm trying to make conway game of life but something happening我正在尝试制作康威生活游戏,但发生了一些事情
【发布时间】:2021-06-04 04:59:05
【问题描述】:

编辑:我在问题中犯了一个巨大的错误,请不要费心帮忙。

我正在尝试,但是当运行代码时,我会得到一个可以绘制的窗口,但是当我运行(我将按钮设置为键盘输入)模拟时,它会摆脱所有单元格。 代码:

import pygame,sys,math

from pygame.constants import USEREVENT

pygame.init()

srceen_size = (900,900)
srceen = pygame.display.set_mode(srceen_size)
pygame.display.set_caption("conway game of life")
clock = pygame.time.Clock()

def D_list(list):
    list2 = []
    for i in list:
        if i not in list2:
            list2.append(i)
    return list2

fps = 120
chuck = (320,320)
cell = []
cell_color = (120, 120, 139)
cell_size = 18
cell_delete = False
movement = USEREVENT
cell_create = False
cell_moving = False
cell_re = False
list_dumbie = []
index1 = 0
index4 = 0
gen = []
index2 = 0
index3 = 0
new_gen = []
celless = True

pygame.time.set_timer(movement, 3000)
while True:
    clock.tick(fps)
    if cell_create == True:
        pos_mouse = pygame.mouse.get_pos()
        x,y = pos_mouse
        X = x % 20
        Y = y % 20
        index2 = index2 + 1
        x, y = x - X, y - Y
        cell.append((x,y))
        cell = D_list(cell)
        cell.sort()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
            if event.key == pygame.K_KP_ENTER:
                if cell_moving == True:
                    cell_moving = False
                else:
                    cell_moving = True
            if event.key == pygame.K_LSHIFT:
                if cell_re == False:
                    cell_re = True
                if cell_re == True:
                    cell_re = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if cell_re == False:
                cell_create = True
                cell_delete = False
            if cell_re == True:
                cell_create = False
                cell_delete = True
        if event.type == pygame.MOUSEBUTTONUP:
            if cell_create == True:
                cell_create = False
            if cell_delete == True:
                cell_delete = False
        if event.type == movement: #this is where i had problem
            if cell_moving == True:
                for a in cell:
                    list_dumbie = cell
                    list_dumbie.pop(index1)
                    for b in list_dumbie:
                        if b[0] == a[0] - cell_size or b[0] == a[0] + cell_size:
                            if b[1] == a[1] - cell_size or b[1] == a[1] + cell_size:
                                index4 = index4 + 1
                        if index4 == 3 or index4 == 4:
                            new_gen.append(a)
                    index4 = 0


                    index1 = index1 + 1
                cell = new_gen
                new_gen = []
                cell.sort()
                index1 = 0
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LSHIFT:
                cell_re = False
    srceen.fill((22, 22, 22))
    for x,y in cell:
        pygame.draw.rect(srceen,cell_color,(x+1,y+1,cell_size,cell_size))
    pygame.display.update()

康威生命游戏也是元胞自动机。只有两条规则

  1. 如果一个细胞旁边有 3 或 4 个细胞,并且该细胞旁边有更多或更少的细胞,则该细胞会死亡
  2. 如果一个死细胞被 3 个活细胞包围,死细胞就会活过来。

【问题讨论】:

  • 你在哪里计算一个单元格的邻居数?
  • 代码调用单元格中有一个单元格列表
  • !?你什么意思?这如何回答我的评论?是的,我知道,但是你在哪里计算一个细胞的活着的邻居?
  • 问题解决了吗?
  • 是的,没关系

标签: python pygame simulation conways-game-of-life


【解决方案1】:

首先,您必须计算单元格的邻居。创建一个网格来显示一个单元格是否活着,并创建一个第二个网格来计算每个活单元格的邻居。根据这 2 个大梁的数据,您可以实施规则并制作新的活细胞列表:

while True:
    # [...]

    for event in pygame.event.get():
        # [...]

        if event.type == movement:
            if cell_moving == True:

                alive = [[False] * 45 for _ in range(45)]
                ncount = [[0] * 45 for _ in range(45)]
                for a in cell:
                    i, j = a[0] // 20, a[1] // 20
                    alive[j][i] = True
                    for k, l in [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]:
                        if 0 <= i+k < 45 and 0 <= j+l < 45:
                            ncount[j+l][i+k] += 1

                new_gen = []
                for i in range(45):
                    for j in range(45):
                        x, y = i * 20, j * 20

                        if alive[j][i] and (ncount[j][i] == 2 or ncount[j][i] == 3):
                            new_gen.append((x, y))

                        elif not alive[j][i] and ncount[j][i] == 3:
                            new_gen.append((x, y))
                
                cell = new_gen
                cell.sort()

【讨论】:

  • @pawnadona 不。您没有正确计算单元格的活着的邻居。每个单元格有 8 个邻居。你完全错过了让死细胞复活的部分
  • 我最大的问题是忘记了单元格大小
  • 我还没去做
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-16
  • 1970-01-01
  • 2012-01-19
  • 2013-02-21
  • 1970-01-01
  • 1970-01-01
  • 2017-09-16
相关资源
最近更新 更多