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

左上角的精灵消失了,我不知道如何修复它 PyGame 随机生成的精灵迷宫

如何解决左上角的精灵消失了,我不知道如何修复它 PyGame 随机生成的精灵迷宫

所以我正在尝试制作一个迷宫游戏,迷宫是使用精灵随机生成的,但是,左上角的单元格总是空的,我不知道如何在不破坏删除某些单元格的代码的情况下解决这个问题取决于计算机采取的方向。如果有人知道如何解决这个问题,请帮忙。

代码

import pygame,random,time
pygame.init()
SCREEN_WIDTH = 810
SCREEN_HEIGHT = 810
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))


class Cell(pygame.sprite.Sprite):
    def __init__(self,x,y):
        super().__init__()
        self.x = x
        self.y = y
        self.size = 80
        self.N = True
        self.E = True
        self.S = True
        self.W = True
        self.image = pygame.Surface([80,80])
        self.image.fill((255,255,255))
        self.rect = self.image.get_rect()
        # self.rect.x = x
        # self.rect.y = y

    def update(self):
        if self.N:
            pygame.draw.rect(screen,(255,0),[self.x+1,self.y+1,self.size,1])
        if self.W:
            pygame.draw.rect(screen,1,self.size])
        if self.S:
            pygame.draw.rect(screen,self.y+self.size+1,1])
        if self.E:
            pygame.draw.rect(screen,[self.x+self.size+1,self.size+1])

    def clear_wall(self,side):
        if side.lower() == "west":
            self.W = False
        elif side.lower() == "east":
            self.E = False
        elif side.lower() == "north":
            self.N = False
        elif side.lower() == "south":
            self.S = False


def find_cell(cells,current,direction):
    for c in cells:
        if direction == "east":
            if c.x == current.x + 80 and c.y == current.y:
                return c  # found the correct cell,so return it
        if direction == "west":
            if c.x == current.x - 80 and c.y == current.y:
                return c
        if direction == "south":
            if c.x == current.x and c.y == current.y + 80:
                return c
        if direction == "north":
            if c.x == current.x and c.y == current.y - 80:
                return c


def getoppositeDir(direction):
    if direction.lower() == "east":
        return "west"
    elif direction.lower() == "west":
        return "east"
    elif direction.lower() == "north":
        return "south"
    elif direction.lower() == "south":
        return "north"


def generate_maze(cells):
    current = cells.sprites()[random.randint(0,79)] # starting point
    # Set current cell to initial starting point
    direction = "south"
    for x in range(800):
        joining_cell = find_cell(cells,direction)
        current.clear_wall(direction)
        try:
            joining_cell.clear_wall(getoppositeDir(direction))
            current = joining_cell  # move to the new current cell
            direction = random.choice(["north","east","south","west"])
        except:
            pass


def Main():
    cells = pygame.sprite.Group()
    for x in range(10):
        for y in range(10):
            cells.add(Cell(y * 80,x * 80))
    generate_maze(cells)

    clock = pygame.time.Clock()
    onRun = True
    while onRun:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                runOn = False
        screen.fill([255,255])
        cells.update()
        cells.draw(screen)
        pygame.display.flip()
        clock.tick(60)


if __name__ == '__main__':
    pygame.init()
    Main()
    pygame.quit()

解决方法

我想我修好了。

问题在于您没有定位图像,因此所有单元格的白色表面在 (0,0) 处闪烁,这就是左上角单元格从来没有可见线条的原因。

我通过使用 surface.get_rect(top_left=location) 将 rect 设置为位置来解决这个问题。

但随后所有单元格都被覆盖了,所以我颠倒了绘制和更新步骤。

IDK,希望这会有所帮助。

import pygame,random,time
pygame.init()
SCREEN_WIDTH = 810
SCREEN_HEIGHT = 810
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))


class Cell(pygame.sprite.Sprite):
    def __init__(self,x,y):
        super().__init__()
        self.x = x
        self.y = y
        self.size = 80
        self.N = True
        self.E = True
        self.S = True
        self.W = True
        self.image = pygame.Surface([80,80])
        self.image.fill((255,255,255))
        self.rect = self.image.get_rect(topleft=(x,y))
        # self.rect.x = x
        # self.rect.y = y

    def update(self):
        if self.N:
            pygame.draw.rect(screen,(255,0),[self.x+1,self.y+1,self.size,1])
        if self.W:
            pygame.draw.rect(screen,1,self.size])
        if self.S:
            pygame.draw.rect(screen,self.y+self.size+1,1])
        if self.E:
            pygame.draw.rect(screen,[self.x+self.size+1,self.size+1])

    def clear_wall(self,side):
        if side.lower() == "west":
            self.W = False
        elif side.lower() == "east":
            self.E = False
        elif side.lower() == "north":
            self.N = False
        elif side.lower() == "south":
            self.S = False


def find_cell(cells,current,direction):
    for c in cells:
        if direction == "east":
            if c.x == current.x + 80 and c.y == current.y:
                return c  # found the correct cell,so return it
        if direction == "west":
            if c.x == current.x - 80 and c.y == current.y:
                return c
        if direction == "south":
            if c.x == current.x and c.y == current.y + 80:
                return c
        if direction == "north":
            if c.x == current.x and c.y == current.y - 80:
                return c


def getOppositeDir(direction):
    if direction.lower() == "east":
        return "west"
    elif direction.lower() == "west":
        return "east"
    elif direction.lower() == "north":
        return "south"
    elif direction.lower() == "south":
        return "north"


def generate_maze(cells):
    current = cells.sprites()[random.randint(0,79)] # starting point
    # Set current cell to initial starting point
    direction = "south"
    for x in range(800):
        joining_cell = find_cell(cells,direction)
        current.clear_wall(direction)
        try:
            joining_cell.clear_wall(getOppositeDir(direction))
            current = joining_cell  # move to the new current cell
            direction = random.choice(["north","east","south","west"])
        except:
            pass


def main():
    cells = pygame.sprite.Group()
    for x in range(10):
        for y in range(10):
            print(y*80,x*80)
            cells.add(Cell(y * 80,x * 80))
    generate_maze(cells)

    clock = pygame.time.Clock()
    onRun = True
    while onRun:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                onRun = False
        screen.fill([255,255])
        cells.draw(screen)
        cells.update()
        pygame.display.flip()
        clock.tick(60)


if __name__ == '__main__':
    pygame.init()
    main()
    pygame.quit()

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