Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Pygame中同时检测鼠标左键和右键点击_Python_Python 2.7_Pygame - Fatal编程技术网

Python 如何在Pygame中同时检测鼠标左键和右键点击

Python 如何在Pygame中同时检测鼠标左键和右键点击,python,python-2.7,pygame,Python,Python 2.7,Pygame,我使用python-2.7和pygame构建了一个扫雷舰。几乎完成了,只剩下一个重要的功能需要实现。当同时按下鼠标左键和右键时,它将扫描周围的块并执行某些操作。但我不知道如何同时检测两次点击。 在pygame的网站上,有 注意,在X11上,一些XServer使用中间按钮模拟。同时单击按钮1和按钮3时,会发出2按钮事件 但这对我不起作用。你应该测试pygame.mouse.get_pressed[0]鼠标左键和pygame.mouse.get_pressed[2]鼠标右键的值 此测试应该与pyga

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

注意,在X11上,一些XServer使用中间按钮模拟。同时单击按钮1和按钮3时,会发出2按钮事件

但这对我不起作用。

你应该测试pygame.mouse.get_pressed[0]鼠标左键和pygame.mouse.get_pressed[2]鼠标右键的值

此测试应该与pygame.event.get for循环处于同一级别,并且不包括在此循环中。您可以测试此脚本以了解我的意思:

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((800, 500), RESIZABLE)
running = True
while(running):
    mousebuttons = pygame.mouse.get_pressed()
    if mousebuttons[0] == 1 and mousebuttons[2] == 1:
        print("Both left and rigth mouse buttons are pressed.")
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

您可以为文档中概述的场景编写代码。按下按钮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
更新-在两个按钮的鼠标上模拟第三次鼠标单击

上面的代码可以工作,但前提是您接受这样一个事实,即单个的right或left操作也将触发,但在comboleft/right操作之前。如果这对你的游戏设计有用,那就试试吧。如果这是不可接受的,那么以下内容将起作用,尽管更深入一些:

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每次在游戏循环中都会检查每个鼠标按钮的状态,因此需要一个标志,以防止在按住按钮的情况下,即使在短时间的单击过程中,您的操作也会多次运行。游戏循环比鼠标点击更快


所以这三个场景都包括在内:左单曲、右单曲、左-右组合。换句话说,它模拟了第三次鼠标点击两个按钮的鼠标……虽然解决方案相当笨拙

如果您只想在按下两个按钮后执行一次代码,只需在运行代码后将鼠标右键和鼠标左键设置为False

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")
                    right_mouse_down = False
                    left_mouse_down = True
            if event.button == 3:
                right_mouse_down = False
                if left_mouse_down:
                    print("perform action")
                    right_mouse_down = False
                    left_mouse_down = False
        elif event.type == pygame.MOUSEBUTTONUP:
            if event.button == 1:
                left_mouse_down = False
            if event.button == 3:
                 right_mouse_down = False
现在,在删除并重新单击两个鼠标按钮之前,此代码将不会再次运行。我建议使用pygame.mouse.get_pressed在任何时候检查按钮的状态,这样,如果您只希望代码第一次出现,就可以设置一个标志

done_the_thing = False
while not done: # Main game loop
    buttons = pygame.mouse.get_pressed()

    if buttons[0] and buttons[2] and not done_the_thing:
        do_the_thing() # This is just your function
        done_the_thing = True

    if not buttons[0] and not buttons[2]:
        done_the_thing = False

这将提供更大的灵活性,因此,如果您想添加更多每帧都会发生的鼠标事件或更改当前正在执行的操作,您可以将所有这些事件放在一起并保持干净。

当左键仍处于按下状态时,您会检测到右键单击,反之亦然。要做到这一点,您必须在每次向下/向上时保存按钮的状态。是否只需单击鼠标右键和左键?还是你把他们按住?非常感谢。我想检测左键和右键的单击,而不是按住。您的代码似乎可以完美地检测这种情况。但我也希望在只有左键或右键单击的情况下执行其他操作。我觉得除了两个点击场景之外,这些是不能区分的,对吗?你指的是类似击键的东西吗?还是仅仅一次左键点击?@orangedietc我编辑了我的代码,以解释单次左键点击和单次右键点击操作。要读取击键,您只需执行以下操作:elif event.type==pygame.keydown我指的是鼠标单击。我认为这行不通。如果我同时单击左键和右键,'对于pygame.event.get中的事件:'一次只接受一个事件,首先左键单击,它将需要进入下一个循环来检测右键单击并执行两次单击操作,但它真正要做的是触发一次左键单击操作,对吗?是的,我明白你现在所说的。代码不知道您想按另一个鼠标按钮,因此将默认为单击一次,我使用“pygame.event.get”一次接受一个事件,因为如果不知道,我无法使右键单击放置一个标志,而另一个取消标志。我假设在这种情况下,“pygame.mouse.get_pressed”中始终只有一个布尔值为真
?您应该将pygame.mouse.get_pressed部分放置在pygame.event.get for循环的外部。。。我现在在办公室工作,今晚给我一点时间,我会给你一些简单的代码,通过编辑我上面的答案来做你想做的事情;这里我有更多的细节。这个剧本在家里效果很好。如果我让你高兴,别忘了验证这个答案。享受