Pygamecolliderect()函数返回0(零)和1(一个)而不是True和False?

colliderect()函数不会返回TrueFalse;相反,它正在返回01。当我使用函数类型时,它说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

回答

如果 pygame 文档没有对此进行任何说明,这是很正常的,因为1并且0非常常用来替换TrueFalse

你可以做

if testcube.colliderect(rect):
   # Your code

没有==.

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


以上是Pygamecolliderect()函数返回0(零)和1(一个)而不是True和False?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>