【问题标题】:Pygame colliderect() function returning 0 (zeros) and 1 (ones) instead of True and False? [duplicate]Pygame colliderect() 函数返回 0(零)和 1(一)而不是 True 和 False? [复制]
【发布时间】:2021-03-22 10:57:35
【问题描述】:

colliderect() 函数不会返回TrueFalse;相反,它返回01。当我使用函数类型时,它显示int 而不是bool。如果我使用 01 而不是布尔值进行编码,代码可以工作,但为什么会这样呢? Pygame 文档没有提及它。

def collisions():
    tileHitsList = []
    for rect in tileList:
        print(testcube.colliderect(rect)) #it weirdly prints 0 and 1 instead of False and True

        #tileList is a list which contains the rects of the tiles being rendering on the display
        #testcube is a rect that hits the tiles

        if testcube.colliderect(rect) == True:
            tileHitsList.append(rect)
    return tileHitsList

【问题讨论】:

  • True == value > 0 或 value 和 False == 0 所以你会得到这些结果。
  • 觉得很奇怪,因为文档没有提到任何关于返回 int 值的内容,我就这样使用它,ty。
  • if X == True: 几乎从来都不是正确的说法,无论如何。只需使用if X:(同样使用if not X:,而不是与False 进行显式比较)。

标签: python python-3.x pygame pygame2


【解决方案1】:

如果 pygame 文档没有说明这很正常,因为 10 非常常用来替换 TrueFalse

你可以这样做

if testcube.colliderect(rect):
   # Your code

没有==

这是有关此事的文档:https://docs.python.org/3/reference/datamodel.html#index-10

【讨论】:

    猜你喜欢
    • 2014-10-26
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2020-03-15
    相关资源
    最近更新 更多