嘗試從一個文件中創建一個自定義地圖裝載器,我得到了以下問題:問題在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 ","
對不起給了地圖文件的這樣一個奇怪的描述,因爲我無法上傳這裏的實際文件
我真的很感激在這個問題上的任何幫助,使其成爲更清潔的任何方式,縮短等
使用如果您正在使用PyCharm,按'shift' +'F9 '然後鼠標移動'自我.map_data'來查看它的內容。 –
您嘗試訪問的列表中包含的元素少了您期望的元素。在進行實際分配之前,嘗試打印'len(self.map_data [a] [z] [y])'和'len(self.tmp4)'。你會看到哪個列表的元素少於預期。 –
嗯,就在錯誤occures打印(LEN(self.tmp4))顯示1,而不是100,讓我嘗試重新制作的地圖文件 – Madworks