【问题标题】:IndexError: string index out of range errorIndexError:字符串索引超出范围错误
【发布时间】:2016-03-12 22:36:31
【问题描述】:

我正在尝试使用 pygame 制作地图,但我得到了 IndexError: string index out of range 每次启动程序时都会出错。

这是出现错误的行:return self.data[y][x] in

def__getitem__(self, xy):
    x = xy[0]
    y = xy[1]
    return self.data[y][x]

当我将我的地图定义为

hallway_entrance_lilbrary =\
["xxxxx...........xxxxx",
 "xxxxx...........xxxxx",
 "xxxxx...........xxxxx",
 "xxxxx...........xxxxx",
 "xxxxx...........xxxxx",
 "xxxxx...........xxxxx",
 "xxxxx...........xxxxx",
 "xxxxx...........xxxxx",
 "xxxxx...........xxxxx",
 "xxxxx...........xxxxx",
 "xxxxx...........xxxxx",
 "xxxxx...........xxxxx",
 "xxxxx...........xxxxx",
 "xxxxx...........xxxxx",
 "xxxxx...........xxxxx",
 "xxxxx.....s.....xxxxx",]

其中x 是墙,. 是地砖,s 是起始方格。

当我将地图更改为时,错误开始出现

hallway_entrance_lilbrary =\
["xxxxxxx...xxxxxxx"
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xx....x...x....x",
 "xxxxxxx.s.xxxxxx",]

我尝试将新地图缩短,使其长度与第一个相当,但仍然会产生错误。

感谢您的帮助。

【问题讨论】:

    标签: python-2.7 pygame


    【解决方案1】:

    语法错误

    我看起来您在秒示例的第一行缺少逗号。

    hallway_entrance_lilbrary =\
    ["xxxxxxx...xxxxxxx" # <--- no comma?
     "xx....x...x....x",
     ...
    

    【讨论】:

      猜你喜欢
      • 2012-02-01
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多