通过遍历pygame中的列表来创建地图?

问题描述 投票:0回答:1

我正在用pygame创建我的第一个游戏,并试图通过使用列表创建迷宫,该列表在指定编号所在的位置创建一个图块。索引显示为1的地方,它将打印墙壁,2,门等。现在,我只是拥有它,以便它为每个图块打印相同的图像,但是draw()函数仅在索引达到1时才拾取并打印。

class Maze:
def __init__(self, x=20, y=20):
    self.x = x
    self.y = y
    self.tile = pygame.image.load("assets/redsquare.png")
    self.tile = pygame.transform.scale(self.tile, (20, 20))
    self.screen = pygame.display.set_mode((800, 700))
    self.background = pygame.Surface(self.screen.get_size()).convert()
    self.maze =  """ 
                     1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
                     1 0 1 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 1 
                     1 0 1 0 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 
                     1 0 4 0 1 0 0 0 1 0 0 0 0 0 0 0 0 4 0 1 
                     1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 0 1 
                     1 0 1 0 0 0 1 0 0 0 4 0 0 0 1 0 1 1 0 1 
                     1 4 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 
                     1 0 0 0 1 0 0 0 4 0 0 0 1 0 1 0 4 0 0 1 
                     1 1 1 0 1 0 1 1 1 1 1 0 1 0 1 0 1 1 0 1 
                     1 0 0 0 1 0 1 0 4 0 1 0 1 0 1 0 1 1 0 1 
                     1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 
                     1 0 0 0 1 0 0 0 1 0 1 0 1 0 1 0 1 1 0 1 
                     1 4 1 1 1 1 1 1 1 0 1 0 1 0 0 0 1 1 0 1 
                     1 0 0 0 0 0 4 0 1 0 1 1 1 1 1 1 1 1 1 1 
                     1 1 1 1 1 0 1 0 1 0 1 0 0 0 4 0 0 0 1 1 
                     1 0 0 0 1 0 1 0 1 0 1 0 1 1 1 1 1 0 1 1 
                     1 0 1 1 1 0 1 0 1 0 0 0 1 0 0 0 1 0 0 3 
                     1 0 0 0 0 0 1 0 1 1 1 1 1 0 1 0 1 0 1 1 
                     1 1 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 0 1 1 
                     1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 """
    self.maze = self.maze.splitlines()

这是我的绘画功能。

def draw(self):
    print(self.maze)
    for y, line in enumerate(self.maze):
        for x, c in enumerate(line):
            if c == "1":
                self.screen.blit(self.tile, (x * 20, y * 20))
            if c == "2":
                self.screen.blit(self.tile, (x * 20, y * 20))
            if c == "3":
                self.screen.blit(self.tile, (x * 20, y * 20))
            if c == "4":
                self.screen.blit(self.tile, (x * 20, y * 20))

'''

我正在用pygame创建我的第一个游戏,并试图通过使用列表创建迷宫,该列表在指定编号所在的位置创建一个图块。索引显示为1的地方,它将打印墙壁,2,门,...

python dictionary pygame 2d
1个回答
0
投票

您只需要一个for循环,请尝试:

© www.soinside.com 2019 - 2024. All rights reserved.