我不明白哪里出了问题。我将发布相关的代码部分。
错误:
Traceback (most recent call last):
File "C:\Python\pygame\hygy.py", line 104, in <module>
check_action()
File "C:\Python\pygame\hygy.py", line 71, in check_action
check_portal()
File "C:\Python\pygame\hygy.py", line 75, in check_portal
if [actor.x - 16, actor.y - 16] > portal[i][0] and [actor.x + 16, actor.y + 16] < portal[i][0]:
TypeError: tuple indices must be integers
功能:
def check_portal():
for i in portal:
if [actor.x - 16, actor.y - 16] > portal[i][0] and [actor.x + 16, actor.y + 16] < portal[i][0]:
if in_portal == False:
actor.x,actor.y=portal[i][1]
in_portal = True
elif [actor.x - 16, actor.y - 16] > portal[i][1] and [actor.x + 16, actor.y + 16] < portal[i][1]:
if in_portal == False:
actor.x,actor.y=portal[i][1]
in_portal = True
else:
in_portal = False
初始化 Actor :
class xy:
def __init__(self):
self.x = 0
self.y = 0
actor = xy()
初始化门户:
portal = [[100,100],[200,200]],[[300,300],[200,100]]
最佳答案
给定portal
的初始化,循环
for i in portal:
...
只会进行两次迭代。在第一次迭代中,i
将是 [[100,100],[200,200]]
。尝试执行 portal[i]
将等同于 portal[[[100,100],[200,200]]]
,这没有意义。您可能只想使用 i
而不是 portal[i]
。 (您可能还想将其重命名为比 i
更有意义的名称。)
关于 python - TypeError : tuple indices must be integers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9570675/