尝试从一个文件中创建一个自定义地图装载器,我得到了以下问题:问题在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