2014-04-28 85 views
0

我有兩個2D陣列,將顯示層各自包含信息:2D陣列內存儲的2D陣列 - 的Python

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

我想包含這兩個2D陣列,另一個陣列中:

map = [[layer0], [layer1]] 

但是,我的程序不會正確顯示切片。 我的問題是: 是否有可能將二維數組存儲在另一個二維數組中? 謝謝。

我有一定的環系統,用於通過所述陣列迭代和顯示對應於所述陣列的內容瓦片:

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 

我曾嘗試添加另一簡單的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 

以下是完整的方法:

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 

謝謝。

+0

你指的是什麼瓷磚?你怎麼試圖展示這些東西?發生了什麼事情,它與預期的有什麼不同?但快速回答,是的,可以將二維數組存儲在二維數組中。例如:[[[1,2] [3,4]]] – FrobberOfBits

+0

嘿FrobberOfBits,我編輯的問題更具體。 :) – FrigidDev

回答

1

我也做了同樣的事情讀取二維數組,並不能真正看到你爲什麼需要有另一個內部數組.. 我用這個代碼來創建該文件中的數組:

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

那麼這個來閱讀:

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)) 

希望幫助,感到有點困惑,爲什麼你甚至需要把它放在另一個數組中。

+0

你知道,你是對的,我其實不需要在2D中擁有它,我不知道我爲什麼做... – FrigidDev

+0

這很酷。樂於幫助。你可以去創建一個並閱讀它嗎? – Andy

+0

是的,這不是問題;),謝謝 – FrigidDev