【发布时间】:2021-03-22 10:57:35
【问题描述】:
colliderect() 函数不会返回True 和False;相反,它返回0 和1。当我使用函数类型时,它显示int 而不是bool。如果我使用 0 和 1 而不是布尔值进行编码,代码可以工作,但为什么会这样呢? 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