2017-01-02 70 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

对不起,不太了解制作游戏。 (也没玩过那个游戏) –