python - TypeError : tuple indices must be integers

标签 python integer tuples pygame indices

我不明白哪里出了问题。我将发布相关的代码部分。

错误:

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/

相关文章:

python - Ubuntu 18.04 上的 JModelica

binary - Rust 中有什么东西可以将二进制字符串转换为整数吗?

c# - 字符串和数字条件的最佳实践

c - 在c中将 float 除以整数时得到整数结果

python - 如何从生成器中获取元组?最佳实践

python - 如何将列表作为新行附加到 pandas 数据框

python - 在python/numpy中生成多对不相等的随机整数

python - 如何在 Python 中制作 float ?

Python列表-按第一项进行分组,对第二项进行计数和求和

function - 是否可以在 Haskell 中的列表上映射函数元组?