在2D数组中存储2D数组 - Python

时间:2022-09-30 21:30:36

I have two 2D arrays, each containing information that will display a layer:

我有两个2D数组,每个都包含显示图层的信息:

layer0 = [[info], [info]]
layer1 = [[info], [info]]

I would like to contain these two 2D-arrays, within yet another array:

我想在另一个数组中包含这两个2D数组:

map = [[layer0], [layer1]]

However, my program will not display the tiles correctly. My question is: Is it possible to store 2D arrays, within another 2D array? Thank you.

但是,我的程序将无法正确显示切片。我的问题是:是否可以在另一个2D阵列中存储2D数组?谢谢。

I have a certain loop-system for iterating through said arrays and displaying tiles corresponding to the array content:

我有一个循环系统,用于迭代所述数组并显示与数组内容相对应的切片:

for array in maplayer:
            for tile in array:
                if tile == 0:
                    screen.blit(self.tile_dict[0], (self.tileX, self.tileY))
                    self.tileX = self.tileX+16
                if tile == 1:
                    screen.blit(self.tile_dict[1], (self.tileX, self.tileY))
                    self.tileX = self.tileX+16
            self.tileX = self.cameraX
            self.tileY += 16

I have tried adding another simple for loop, to iterate through the actual map array, but PyGame displays a blank screen:

我尝试添加另一个简单的for循环,迭代实际的map数组,但PyGame显示一个空白屏幕:

for maplayer in map:
        for array in maplayer:
            for tile in array:
                if tile == 0:
                    screen.blit(self.tile_dict[0], (self.tileX, self.tileY))
                    self.tileX = self.tileX+16
                if tile == 1:
                    screen.blit(self.tile_dict[1], (self.tileX, self.tileY))
                    self.tileX = self.tileX+16
            self.tileX = self.cameraX
            self.tileY += 16

Here is the full method:

这是完整的方法:

def LoadMap(self, map):
    self.tileX = self.cameraX
    self.tileY = self.cameraY
    for maplayer in map:
        for array in maplayer:
            for tile in array:
                if tile == 0:
                    screen.blit(self.tile_dict[0], (self.tileX, self.tileY))
                    self.tileX = self.tileX+16
                if tile == 1:
                    screen.blit(self.tile_dict[1], (self.tileX, self.tileY))
                    self.tileX = self.tileX+16
            self.tileX = self.cameraX
            self.tileY += 16

Thanks.

谢谢。

1 个解决方案

#1


1  

I have done the same thing with reading 2D arrays and can't actually see why you need to have the array inside of another.. I used this code to create the array from a file:

我在阅读2D数组时也做了同样的事情,实际上无法理解为什么需要将数组放在另一个数组中。我使用此代码从文件创建数组:

gameMap = [list(row.rstrip('\n')) for row in open('Room 1.txt')]

Then this to read it:

然后这个读它:

for i in range(0, len(gameMap)):

    for x in range(0, len(gameMap[i])):
        xC = x * 30
        y = i * 30

        if gameMap[i][x] == "*":
            screen.blit(wallImage, (xC, y))

        elif gameMap[i][x] == ".":
            screen.blit(floorImage, (xC, y))

        elif gameMap[i][x] == "+":

            if playerDirection == "up" or playerDirection == "":
                screen.blit(playerForwardImage, (xC, y))

            elif playerDirection == "right":
                screen.blit(playerRightImage, (xC, y))

            elif playerDirection == "left":
                screen.blit(playerLeftImage, (xC, y))

            elif playerDirection == "down":
                screen.blit(playerDownImage, (xC, y))

        elif gameMap[i][x] == "#":
            pygame.draw.rect(screen, black, (xC, y, 30, 30))

        elif gameMap[i][x] == "=":
            pygame.draw.rect(screen, red, (xC, y, 30, 30))

Hope that helps, am a bit confused about why you even need to put it inside another array.

希望有所帮助,我有点困惑为什么你甚至需要把它放在另一个数组。

#1


1  

I have done the same thing with reading 2D arrays and can't actually see why you need to have the array inside of another.. I used this code to create the array from a file:

我在阅读2D数组时也做了同样的事情,实际上无法理解为什么需要将数组放在另一个数组中。我使用此代码从文件创建数组:

gameMap = [list(row.rstrip('\n')) for row in open('Room 1.txt')]

Then this to read it:

然后这个读它:

for i in range(0, len(gameMap)):

    for x in range(0, len(gameMap[i])):
        xC = x * 30
        y = i * 30

        if gameMap[i][x] == "*":
            screen.blit(wallImage, (xC, y))

        elif gameMap[i][x] == ".":
            screen.blit(floorImage, (xC, y))

        elif gameMap[i][x] == "+":

            if playerDirection == "up" or playerDirection == "":
                screen.blit(playerForwardImage, (xC, y))

            elif playerDirection == "right":
                screen.blit(playerRightImage, (xC, y))

            elif playerDirection == "left":
                screen.blit(playerLeftImage, (xC, y))

            elif playerDirection == "down":
                screen.blit(playerDownImage, (xC, y))

        elif gameMap[i][x] == "#":
            pygame.draw.rect(screen, black, (xC, y, 30, 30))

        elif gameMap[i][x] == "=":
            pygame.draw.rect(screen, red, (xC, y, 30, 30))

Hope that helps, am a bit confused about why you even need to put it inside another array.

希望有所帮助,我有点困惑为什么你甚至需要把它放在另一个数组。