我使用 python-2.7 和 pygame 来构建扫雷艇。差不多完成了,只剩下一个重要的功能需要实现。当同时按下鼠标左键和右键时,它会扫描周围的块并执行某些操作。但我无法弄清楚如何同时检测两次点击。在 pygame 的网站上,有
请注意,在 X11 上,某些 XServer 使用中键模拟。当您同时单击按钮 1 和 3 时,可以发出 2 按钮事件。
但这对我不起作用。
您可以为文档中概述的场景编写代码(按下按钮 1 和按下按钮 3 时会触发鼠标中键 2)。
mouse_pressed = pygame.mouse.get_pressed()
if (mouse_pressed[0] and mouse_pressed[2]) or mouse_pressed[1]:
print("left and right clicked")
请记住,使用 pygame.mouse.get_pressed() 通常会非常快地触发,这意味着 if 语句中的任何代码都会在您释放鼠标键之前发生几次。这是因为 get_pressed() 始终返回所有鼠标按钮的状态。如果您想跟踪被按住的按钮,此方法非常有用。
我会将解决方案编码到 pygame.event.get() 中。这只会在事件发生时触发,因此只会触发一次:
left_mouse_down = False
right_mouse_down = False
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
left_mouse_down = True
if right_mouse_down:
print("perform action")
else:
print("action for single left click")
if event.button == 3:
right_mouse_down = True
if left_mouse_down:
print("perform action")
else:
print("action for single right click")
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
left_mouse_down = False
if event.button == 3:
right_mouse_down = False
更新- 模拟第三次鼠标点击两键鼠标
上面的代码有效,但前提是您接受单个右或左动作也会触发的事实,但在组合(左/右)动作之前。如果这适用于您的游戏设计,那就试试吧。如果这是不可接受的,那么以下将起作用(尽管更深入一些):
left_mouse_down = False
right_mouse_down = False
left_click_frame = 0 # keeps track of how long the left button has been down
right_click_frame = 0 # keeps track of how long the right button has been down
left_right_action_once = True # perform the combo action only once
left_action_once = True # perform the left action only once
right_action_once = True # perform the right action only once
max_frame = 2 # The frames to wait to see if the other mouse button is pressed (can be tinkered with)
performing_left_right_action = False # prevents the off chance one of the single button actions running on top of the combo action
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
mouse_pressed = pygame.mouse.get_pressed()
if mouse_pressed[0]: # left click
left_click_frame += 1
left_mouse_down = True
else:
left_action_once = True
left_mouse_down = False
left_click_frame = 0
if mouse_pressed[2]: # right click
right_click_frame += 1
right_mouse_down = True
else:
right_action_once = True
right_mouse_down = False
right_click_frame = 0
if not mouse_pressed[0] and not mouse_pressed[2]:
left_right_action_once = True
performing_left_right_action = False
if left_mouse_down and right_mouse_down and abs(left_click_frame - right_click_frame) <= max_frame:
if left_right_action_once:
print("performing right/left action")
left_right_action_once = False
performing_left_right_action = True
elif left_mouse_down and left_click_frame > max_frame and not performing_left_right_action and not right_mouse_down:
if left_action_once:
print("perform single left click action")
left_action_once = False
elif right_mouse_down and right_click_frame > max_frame and not performing_left_right_action and not left_mouse_down:
if right_action_once:
print("perform single right click action")
right_action_once = False
关于这个问题的难点在于一次只能发生一个事件,因此您必须有一种方法知道左键单击是否被按下,除非您确定右键单击也不会被按下,否则不要对其进行评估。您可以通过跟踪帧来实现这一点,如果左右发生在一定数量的帧 (max_frames) 内,那么您可以右击/左击。否则你要么左要么右。
action_once 变量很重要,因为 pygame.mouse.get_pressed() 每次通过游戏循环都会检查每个鼠标按钮的状态,因此您需要一个标志来防止您的操作在按住按钮时多次运行(即使在点击的短暂持续时间)。游戏循环比鼠标点击快。
因此涵盖了所有三个场景:左单、右单、左右组合。换句话说,它模拟了在双键鼠标上的“第三次”鼠标点击......虽然解决方案相当笨重
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句