2017-03-16 45 views
1

檢測左,右鼠標點擊我使用Python-2.7和pygame的建設掃雷艇。這幾乎完成,只有一個重要的功能需要執行。當鼠標左鍵和右鍵被按下時,它會掃描周圍的塊並做一些事情。但我無法弄清楚如何同時檢測兩次點擊。 在pygame的網站,有如何在同一時間在Pygame的

注意,上X11一些Xservers中使用鼠標中鍵模擬。當您同時點擊按鈕1和3時,可以發出2個按鈕事件。

但它不爲我工作了。

+0

您檢測到右鍵單擊,而左鍵仍然處於關閉狀態,而另一種方式則是周圍。您必須在每次向下/向上時保存按鈕的狀態才能執行此操作。 –

+0

這是一個單擊左右兩個?或者你把他們放下? – The4thIceman

回答

3

您可以爲在文檔中列出的場景代碼(按鈕2鼠標中被觸發按鈕1和3點按)。

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 

UPDATE - 在兩個鍵鼠標模擬第三鼠標點擊

如果接受上面的代碼工作,但事實上,單左或右的行動將觸發爲好,但在組合前(左/右)動作。如果這適用於你的遊戲設計,那就去做吧。如果這是不能接受的,那麼下面的工作(雖然有點多深):

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()每次通過遊戲循環都檢查每個鼠標按鈕的狀態,因此需要一個標誌來防止您的操作在按鈕被保持時運行多次向下(即使在短時間內點擊)。遊戲循環比點擊鼠標更快。

因此,所有三種情況都包括在內:左單,右單,左右組合。換句話說,它模擬了在兩個鍵鼠標「第三」鼠標點擊....雖然該解決方案是非常笨拙

+0

非常感謝。我想要檢測左右單擊而不是按住。看起來你的代碼可以完美地檢測到這種情況。但我也希望在只有左鍵或右鍵點擊時執行其他操作。我覺得這些不能單獨告訴兩個點擊情況嗎? – orangedietc

+0

你指的是類似擊鍵的東西嗎?或者只是單擊左鍵? – The4thIceman

+0

@orangedietc我編輯了我的代碼來解釋單個左鍵單擊操作。閱讀擊鍵,你只需要做:elif event.type == pygame.KEYDOWN – The4thIceman

0

您應該同時測試pygame.mouse.get_pressed()[0](鼠標左鍵)和pygame.mouse.get_pressed()[2](鼠標左鍵)的值。

http://www.pygame.org/docs/ref/mouse.html#pygame.mouse.get_pressed

這個測試應該在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 
+0

我使用'pygame.event.get()'來一次接受一個事件,因爲如果沒有,我不能讓它工作,一個右鍵點擊一個標誌,另一個取消標誌。我假設在這種情況下,'pygame.mouse.get_pressed()'中總是隻有一個布爾值是真的? – orangedietc

+0

你應該把'pygame.mouse.get_pressed()'部分放在'pygame.event.get()'for loop之外......我現在正在我的辦公室工作,今天晚上給我點時間,我會告訴你一些簡單的代碼,通過編輯上面的答案來做你想做的事情;) – PurpleJo

+0

這裏我有更多的細節。該劇本在家完美運作。不要忘記驗證這個答案,如果我讓你的一天。請享用 ! – PurpleJo

0

如果你只是想你的代碼按兩個按鈕只需設置right_mouse_downleft_mouse_downFalse後執行一次在你運行你的代碼之後。

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 

這將允許更多的靈活性,所以如果你想在確實發生每一幀或更改當前你在做什麼更多的鼠標事件增加,你可以讓他們的一起,乾淨。