您可以为文档中概述的场景编写代码(在按钮 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() 每次通过游戏循环时都会检查每个鼠标按钮的状态,因此您需要一个标志来防止您的操作在按住按钮时多次运行(即使在短暂的点击期间)。游戏循环比鼠标点击更快。
因此涵盖了所有三种情况:左单、右单、左右组合。换句话说,它模拟了在两键鼠标上的“第三次”鼠标点击......虽然解决方案相当笨拙