pygame - 如何在pygame中制作一系列坐标?
问题描述
这是一个非常简单的问题,但我想编写一些代码,以便当你在某个范围内时,你点击某个东西,它会改变背景。到目前为止一切正常,但我只知道如何对其进行编码,以便当您处于确切的 X 和 Y 坐标时,您就可以点击。如果你在 100-300 X(例如)和 500 - 600 Y 范围内,我该如何做到这一点,而不是 100 X 和 600 Y 的精确坐标?
(顺便说一下,这是我正在使用的代码片段,如果你愿意,我可以提供完整的代码。)
if 120+75 > mouse[0] > 120 and 50 + 125 > mouse[1] > 125 and x == 110 and y == 60:
print('Click to change')
解决方案
您必须定义矩形区域的左上角 ( x
, y
) 和区域的大小 ( width
, height
):
x = 120
y = 125
width = 75
height = 50
评估鼠标是否在该区域:
if x < mouse[0] x + width and y < mouse[1] y + height:
print('Click to change')
我建议使用pygame.Rect
和.collidepoint()
。通过(x, y, width, height)定义一个矩形对象并评估鼠标位置是否在矩形区域内:
rect = pygame.Rect(120, 125, 75, 50)
if rect.collidepoint(mouse):
print('Click to change')
推荐阅读
- r - 如何使用 IF_ELSE 添加列
- realex-payments-api - Global Payments 托管支付页面集成 - 沙盒和 prod POST 回商家Url 延迟
- html - 使用脚本的 SharePoint 目录 (TOC) 不会转到 TOC 的引用位置
- powershell - 通过 Powershell 重命名计划任务
- wordpress - 如何使用 acf_form() 提交来更新我的帖子标题和永久链接?
- python - 如何阻止 texttable 将我的字符串转换为数字?
- angular - 如何在 Angular 10 中编辑和更新数据
- javascript - React-sound soundmanager 不适用于 useHistory 钩子
- discord - 如何在我的命令中实现 wait_for 函数?
- javascript - Vue,如何处理深度嵌套对象的反应性