微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

生命游戏更新错误

如何解决生命游戏更新错误

我可能只是一个木偶,但在我的game of life中,当您创建glider时,第一次迭代有效,然后由于一些单元格未更新而彻底中断。实际上,他们没有办法将自己从队列中删除。当我删除从队列中删除切片的行时(请参阅第104行),它可以工作,但是它在if语句和该语句的内部对于发生的事情不正确,这让我感到困惑。这是我的代码

import pygame
import time
import random

# if the game loop is running
Running = True
# if the game is paused or not
Paused = False
# width of window
Width = 1000
# height of window
Height = 1000
# pygame surface
Game = pygame.display.set_mode((Width,Height),display=0)
# how many cols
cCol = 50
# how many rows
cRow = 50
# height of each cell
cWidth = Width // cCol
# width of each cell
cHeight = Height // cRow
# the entire board stored in a 2D array
tiles = []
# to optimize times,i created it (important tiles) that stores all of the tiles that Could become active
it = []
# updates per second
Updaterate = 2

BLACK = (0,0)
WHITE = (255,255,255)
RED = (255,0)
BLUE = (0,0)
GREEN = (0,255)
Game.fill(WHITE)
pygame.display.flip()


class Tile:
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.neighbors = []
        for v in [(x + 1,y),(x+1,y-1),y+1),(x - 1,(x-1,(x,y + 1),y - 1)]:
            if cRow > v[0] >= 0 and cCol > v[1] >= 0:
                self.neighbors.append([v[0],v[1]])
        self.active = False
        self.dying = False
        self.creating = False
        self.color = WHITE
        self.remove = False
        self.debug = False

    def get_neighbors(self,t):
        for i in range(0,len(self.neighbors)):
            self.neighbors[i] = t[self.neighbors[i][0]][self.neighbors[i][1]]

    def update_color(self):
        if self.active:
            self.color = BLACK
        else:
            self.color = WHITE

    def draw(self,sur):
        self.update_color()
        pygame.draw.rect(sur,self.color,pygame.Rect((self.x * cWidth,self.y * cHeight),(cWidth,cHeight)))

    def update(self):
        a = 0
        for n in self.neighbors:
            if n.active:
                a += 1
        if a == 1:
            self.remove = False
            self.dying = True
            self.creating = False
        elif a > 3:
            self.remove = False
            self.dying = True
            self.creating = False
        elif a == 3:
            self.remove = False
            self.dying = False
            self.creating = True
        if a == 0:
            self.creating = False
            self.dying = True
            self.remove = True
        if self.debug:
            print(f"a = {a}")
        if a != 0:
            self.remove = False

    def purge(self):
        if self.debug:
            print(f"dying = {self.dying}")
        if self.dying:
            # dying
            self.active = False
            self.creating = False
            self.dying = False
            if self.remove:
                print(self.remove)
                it.remove(self)
                if self.debug:
                    print("REMOVING")
                self.remove = False
            if self.debug:
                print("DYING")
        elif self.creating:
            # coming alive
            self.creating = False
            self.active = True
            for n in self.neighbors:
                if n not in it:
                    it.append(n)
        self.draw(Game)


def clear_board():
    b = []
    for x in range(0,cCol):
        b.append([])
        for y in range(0,cRow):
            # print(f"x = {x},y = {y}")
            b[x].append(Tile(x,y))
    for y in b:
        for t in y:
            t.get_neighbors(b)
    return b


def draw_lines(sur):
    for x in range(1,cCol + 1):
        pygame.draw.line(sur,BLACK,(x * cWidth,0),Height))
    for y in range(1,cRow + 1):
        pygame.draw.line(sur,(0,y * cHeight),(Width,y * cHeight))


def draw_tiles(sur):
    for t in it:
        t.draw(sur)


def get_index(p,isWidth):
    if isWidth:
        p -= p % cWidth
        return p//cWidth
    else:
        p -= p % cHeight
        return p//cHeight
    pass


def draw(s):
    draw_tiles(s)
    draw_lines(s)


tiles = clear_board()
for y in tiles:
    for t in y:
        t.draw(Game)
pygame.display.flip()
draw_lines(Game)
start_time = time.time()
UpdaterateCount = 0
CircIsThere = False
while Running:
    if not Paused:
        c = WHITE
        if time.time()-start_time >= (1/Updaterate):
            if CircIsThere:
                tiles[4][4].draw(Game)
                tiles[4][5].draw(Game)
                tiles[5][4].draw(Game)
                tiles[5][5].draw(Game)
                CircIsThere = False
            for t in it:
                t.update()
            for ti in it:
                print(f"{ti.debug} {ti.x},{ti.y}")
                ti.purge()
            start_time = time.time()

    draw(Game)
    if Paused:
        pygame.draw.circle(Game,RED,[100,100],10)
        CircIsThere = True
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            Running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                Paused = not Paused
            if event.key == pygame.K_c:
                tiles = clear_board()
                it = []
                for y in tiles:
                    for t in y:
                        t.draw(Game)
            if event.key == pygame.K_r:
                for y in tiles:
                    for t in y:
                        t.active = random.choice([True,False])
                        if t not in it:
                            it.append(t)
                        t.draw(Game)

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == pygame.BUTTON_LEFT:
                mx,my = pygame.mouse.get_pos()
                xi = get_index(mx,True)
                yi = get_index(my,False)
                tt = tiles[xi][yi]
                if not tt.active:
                    tt.active = True
                    if tt not in it:
                        it.append(tt)
                    for n in tt.neighbors:
                        if n not in it:
                            it.append(n)
                else:
                    tt.active = False
            if event.button == pygame.BUTTON_RIGHT:
                mx,False)
                tiles[xi][yi].debug = not tiles[xi][yi].debug

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。