2017-01-02 62 views
0

嘗試從一個文件中創建一個自定義地圖裝載器,我得到了以下問題:問題在Python代碼,無法找到問題

"D:\Python 3.4\python.exe" "D:/Games in Python/Game1/game.py" 
Traceback (most recent call last): 
    File "D:/Games in Python/Game1/game.py", line 60, in <module> 
    game = Game() 
    File "D:/Games in Python/Game1/game.py", line 4, in __init__ 
    world = World() 
    File "D:/Games in Python/Game1/game.py", line 23, in __init__ 
    self.load() 
    File "D:/Games in Python/Game1/game.py", line 54, in load 
    self.map_data[a][z][y][x] = self.tmp4[x] 
IndexError: list index out of range 

Process finished with exit code 1 

我曾嘗試通過打印列表的某些部分調試它自己,並逐步完成課程,但仍然沒有成功。下面的代碼:

self.map = open("Data/Saves/test.txt" , "r") 
    self.map_dat = self.map.read() 
    self.map.close 

    self.tmp1 = self.map_dat.split("-") 
    self.map_data = [None] * 2 
    for a in range(0, 2): 
     self.tmp2 = self.tmp1[a].split(">") 
     self.map_data[a] = [None] * 4 
     for z in range(0, 4): 
      self.tmp3 = self.tmp2[z].split("\n") 
      self.map_data[a][z] = [None] * 100 
      for y in range(0, 100): 
       self.tmp4 = self.tmp3[y].split(",") 
       self.map_data[a][z][y] = [None] * 100 
       for x in range(0, 100): 
        self.map_data[a][z][y][x] = self.tmp4[x] 

映射文件如下:

map_lvl0 
> 
map_lvl1 
> 
map_lvl2 
> 
map_lvl3 
- 
map_lvl0_properties 
> 
map_lvl1_properties 
> 
map_lvl2_properties 
> 
map_lvl3_properties 



where map_lvl(0 to 3) a 100*100 grid of zeros seperated by "," 

對不起給了地圖文件的這樣一個奇怪的描述,因爲我無法上傳這裏的實際文件

我真的很感激在這個問題上的任何幫助,使其成爲更清潔的任何方式,縮短等

+0

使用如果您正在使用PyCharm,按'shift' +'F9 '然後鼠標移動'自我.map_data'來查看它的內容。 –

+0

您嘗試訪問的列表中包含的元素少了您期望的元素。在進行實際分配之前,嘗試打印'len(self.map_data [a] [z] [y])'和'len(self.tmp4)'。你會看到哪個列表的元素少於預期。 –

+0

嗯,就在錯誤occures打印(LEN(self.tmp4))顯示1,而不是100,讓我嘗試重新制作的地圖文件 – Madworks

回答

0

你的錯誤可能是在網格上的外邊緣線,也叫做tmp2條目類似於「\ nthe actual data \ n」,由於未對空白進行修剪,因此不以行開頭。或者可能是屬性字段不存在於數字矩陣之外。

但是這個地圖格式可能不是存儲數據的最佳方式。 就我個人而言,我會去效力或JSON文件的二進制格式(爲了便於閱讀,並簡單地用json解析)。喜歡的東西:

[ 
    { 
     "map_name" : "some name", 
     "map_prop2" : "some value", 
     ... 
     "map_layout" : [ 
      [0,...,0], 
      ..., 
      [0,...,0] 
     ] 
    }, 
    ... 
] 

如果你堅持與當前格式我會去像這樣(一個功能:你沒有告訴我們的屬性的格式,所以我只是保持它作爲一個單串,解析它們在實際的解決方案):

# Returns a dictionairy 
# lvls[map_name] -> { id : int, properties : string, grid : int[][] } 
def read_map(filename): 
    lvls = {} 

    with open(filename, "r") as f: 
     lvls_data, lvls_props = f.read().split("-") 

     lvls_data = [ s.strip() for s in lvls_data.split(">") ] 
     lvls_props = [ s.strip() for s in lvls_props.split(">") ] 

     assert(len(lvls_data) == len(lvls_props)) 

     for i in range(len(lvls_data)): 
      # I had no idea how to parse the properties, since I don't know the 
      # format. So I kept it plaintext, do actually parse them 

      data, properties = lvls_data[i], lvls_props[i] 
      grid = [ [ int(point) for point in line.split(",") ] for line in data.split("\n") ] 

      # assert all dimensions are in order 
      assert(len(grid) == 100) 
      assert(all(len(grid[i]) == 100 for i in range(100))) 

      lvls["map%i" % i] = { 'id' : i, 'properties': properties, 'grid' : grid} 
    return lvls 

注意我沒有辦法正確地測試這一點,所以謹慎

+0

好吧,我發現我的問題,是的,這只是你說的,空白,糾正似乎解決它,thx。順便說一句,我正在創造一種矮人堡壘遊戲,試圖用我目前的知識來看看如何做一個類似的遊戲。所以有什麼建議? – Madworks

+0

對不起,不太瞭解製作遊戲。 (也沒玩過那個遊戲) –