python - Pygame colliderect() 函数返回 0(零)和 1(一)而不是 True 和 False?
问题描述
colliderect()
函数不会返回True
和False
;相反,它正在返回0
and 1
。当我使用它说的函数类型int
而不是bool
. 如果我使用0
and1
而不是布尔值进行编码,则代码可以工作,但为什么会发生这种情况?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
and0
非常常用来替换True
and False
。
你可以做
if testcube.colliderect(rect):
# Your code
没有==
.
这是有关此事的文档:https ://docs.python.org/3/reference/datamodel.html#index-10
推荐阅读
- next.js - 在 Next.js Hot Reload 发生之前是否有一个钩子?
- python - 使用 python (matplotlib) 复制 Stata 的 psgraph 的输出
- c# - 根据时间戳组织文件列表
- python - 如何使用 Python Selenium 在下拉菜单中激活复选框
- java - 混合 java 和 scala maven 项目不运行
- javascript - 数组元素未更新到与其先前状态相反的状态
- css - 选择器为真时的CSS条件设置
- websockify - 引发处理程序异常时退出 websockify
- sql - 无法向 COUNT 引入子查询
- javascript - clearInterval() 函数不会清除 React 中的 Interval