[英]Storing 2D arrays within a 2D array - Python
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?
我的问题是:是否可以在另一个2D数组中存储2D数组? Thank you.
谢谢。
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循环,以遍历实际的地图数组,但是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. 谢谢。
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. 希望对您有所帮助,让您有些困惑,为什么您甚至需要将其放入另一个数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.