mouse.get_pressed()不一致/返回(0,0,0)



编辑(已解决(:使用event.button似乎已经完成了任务。当它返回0,0,0时,它返回正确的鼠标按钮(1=左,3=右(

我试着寻找解决方案,但在每一个答案中,似乎都有人不知道或忘记在pg.event.get((中包含事件。pygame中的鼠标检测已经停止可靠工作,我不确定是硬件错误、我的代码不好还是其他原因。以下是我的鼠标游戏循环的简化版本:

while running:
for event in pg.event.get():
pos = pg.mouse.get_pos()
if event.type == pg.MOUSEBUTTONDOWN:
if grid_space.get_rect(x=(adj_x), y=(adj_y)).collidepoint(pos):
if pg.mouse.get_pressed()[2]:
do_thing()
elif event.button == 4:
do_thing()
elif event.button == 5:
do_thing()
else:
print(pg.mouse.get_pressed())
do_thing()

我将鼠标主按钮移到了else,因为这是目前使最重要的操作更可靠的唯一方法,但通过打印else结果,我还发现每4或5次点击中就有一次返回(0,0,0(,而不是(1,0,O(。我尝试了不同的方法来编写表达式,简化结构,增加Pygame时钟,但都不起作用。

有人遇到过这种情况吗?有解决方案吗?

edit:我已经运行了另一个测试,将get_pressed结果立即保存到一个变量中,它仍然返回0,0,0。所以我确信它的状态从MOUSEBUTTONDOWN到调用它的时候没有改变。

pygame.mouse.get_pressed()获取鼠标按钮的当前状态。由于鼠标事件发生,按钮的状态可能已更改。请注意,事件存储在队列中,稍后您将通过pygame.event.get()在应用程序中接收存储的事件。同时,按钮的状态可能因此而改变,导致MOUSEBUTTONDOWN事件的按钮立即存储在pygame.event.Event对象的button属性中。在事件循环中,当您获得事件时,event.buttonpygame.mouse.get_pressed()的状态可能不同
pygame.mouse.get_pos()也是如此。鼠标的位置存储在属性pos中使用event.buttonevent.pos而不是pygame.mouse.get_pressed()pygame.mouse.get_pos():

while running:
for event in pg.event.get():

if event.type == pg.MOUSEBUTTONDOWN:
print(event.button)
if grid_space.get_rect(topleft=(adj_x, adj_y)).collidepoint(event.pos):
if event.button == 2:
do_thing()
elif event.button == 4:
do_thing()
elif event.button == 5:
do_thing()
else:
do_thing()

CCD_ 14和CCD_。这些函数应该直接在应用程序循环中使用。

最新更新