python - 如何在 Pygame 中同时检测鼠标左键和右键单击

标签 python python-2.7 pygame

我使用 python-2.7 和 pygame 构建扫雷游戏。快完成了,只剩下一个重要的功能需要实现了。当同时按下鼠标左键和右键时,它会扫描周围的 block 并执行某些操作。但我不知道如何同时检测两次点击。 在pygame的网站上,有

Note, that on X11 some XServers use middle button emulation. When you click both buttons 1 and 3 at the same time a 2 button event can be emitted.

但这对我来说不起作用。

最佳答案

您可以针对文档中概述的场景进行编码(按下按钮 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

更新 - 模拟两键鼠标上的第三次鼠标单击

上面的代码有效,但如果您接受单个向右或向左操作也会触发的事实,但在组合(左/右)操作之前。如果这适合您的游戏设计,请尝试一下。如果这是 Not Acceptable ,那么以下方法将起作用(尽管更深入一些):

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() 每次通过游戏循环都会检查每个鼠标按钮的状态,因此您需要一个标志来防止按住按钮时操作多次运行(即使是在短暂的点击期间)。游戏循环比鼠标点击更快。

因此涵盖了所有三种场景:左单、右单、左右组合。换句话说,它模拟了两键鼠标上的“第三次”鼠标单击......尽管该解决方案相当笨重

关于python - 如何在 Pygame 中同时检测鼠标左键和右键单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42825429/

相关文章:

python - 从与我共享的文件夹中的 Google Drive 下载 zip 文件夹/文件

Python - 如何一次读取文件一个字符?

python - 如何通过 TLS 1.2 运行 django runserver

python-2.7 - 将 mahotas 安装到 python 2.7 时出错

python - 如何从 Python 向游戏发送方向盘/操纵杆输入?

python - 对 Pandas DataFrame 中的不同单词进行分组和计数

Python脚本: Character 1 equal to character n

android - 如何在 android 设备上执行使用 pygame 制作的游戏?

python - 使用循环创建同一图像的多个实例,我可以独立移动图像的每个实例吗?

python - 如何使用类似的驱动程序自动执行网页跨度控制(例如 slider 值)。 findElement_by_xpath (),Ubuntu Linux 和 chromium 自动测试