mouse.get_pressed() inconsistent/returning (0, 0, 0)
mouse.get_pressed() inconsistent/returning (0, 0, 0)
编辑(已解决):使用 event.button 似乎已经成功了。当它 returns 0, 0, 0 它 returns 正确的鼠标按钮(1 = 左,3 = 右)
我试过寻找解决方案,但在每个答案中,似乎这个人都不知道或忘记在 pg.event.get() 中包含事件 .. [=26 中的鼠标检测=] 已经停止可靠地工作,我不确定这是硬件错误、我的代码有问题还是其他原因。这是我的鼠标游戏循环的简化版本:
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 次点击就有一个 returns (0, 0, 0) 而不是 (1, 0, 0)。我尝试了不同的方式来编写表达式、简化结构、增加 Pygame 时钟但没有任何效果。
有没有人遇到过这种情况,有解决办法吗?
编辑:我已经 运行 另一个测试立即将 get_pressed 结果保存到一个变量,它仍然是 returns 0, 0, 0 所以我很确定它的状态从 MOUESBUTTONDOWN 到它被调用的时间没有改变。
pygame.mouse.get_pressed()
get the current state of the mouse buttons. The state of the buttons may have been changed, since the mouse event occurred. Note that the events are stored in a queue and you will receive the stored events later in the application by pygame.event.get()
. Meanwhile the state of the button may have been changed because of this he button which causes the MOUSEBUTTONDOWN
event is stored in the button
attribute of the pygame.event.Event
立即对象。在事件循环中,当您获得事件时,event.button
和 pygame.mouse.get_pressed()
的状态可能不同。
pygame.mouse.get_pos()
也是如此。鼠标的位置存储在属性pos
使用 event.button
和 event.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()
pygame.mouse.get_pos()
和 pygame.mouse.get_pressed()
不打算在事件循环中使用。这些函数应该直接在应用程序循环中使用。
编辑(已解决):使用 event.button 似乎已经成功了。当它 returns 0, 0, 0 它 returns 正确的鼠标按钮(1 = 左,3 = 右)
我试过寻找解决方案,但在每个答案中,似乎这个人都不知道或忘记在 pg.event.get() 中包含事件 .. [=26 中的鼠标检测=] 已经停止可靠地工作,我不确定这是硬件错误、我的代码有问题还是其他原因。这是我的鼠标游戏循环的简化版本:
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 次点击就有一个 returns (0, 0, 0) 而不是 (1, 0, 0)。我尝试了不同的方式来编写表达式、简化结构、增加 Pygame 时钟但没有任何效果。
有没有人遇到过这种情况,有解决办法吗?
编辑:我已经 运行 另一个测试立即将 get_pressed 结果保存到一个变量,它仍然是 returns 0, 0, 0 所以我很确定它的状态从 MOUESBUTTONDOWN 到它被调用的时间没有改变。
pygame.mouse.get_pressed()
get the current state of the mouse buttons. The state of the buttons may have been changed, since the mouse event occurred. Note that the events are stored in a queue and you will receive the stored events later in the application by pygame.event.get()
. Meanwhile the state of the button may have been changed because of this he button which causes the MOUSEBUTTONDOWN
event is stored in the button
attribute of the pygame.event.Event
立即对象。在事件循环中,当您获得事件时,event.button
和 pygame.mouse.get_pressed()
的状态可能不同。
pygame.mouse.get_pos()
也是如此。鼠标的位置存储在属性pos
使用 event.button
和 event.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()
pygame.mouse.get_pos()
和 pygame.mouse.get_pressed()
不打算在事件循环中使用。这些函数应该直接在应用程序循环中使用。