首页 > 解决方案 > Pygame colliderect() 函数返回 0(零)和 1(一)而不是 True 和 False?

问题描述

colliderect()函数不会返回TrueFalse;相反,它正在返回0and 1。当我使用它说的函数类型int而不是bool. 如果我使用0and1而不是布尔值进行编码,则代码可以工作,但为什么会发生这种情况?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

标签: pythonpython-3.xpygamepygame2

解决方案


如果 pygame 文档没有说明任何内容,这是很正常的,因为1and0非常常用来替换Trueand False

你可以做

if testcube.colliderect(rect):
   # Your code

没有==.

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


推荐阅读