簡體   English   中英

pygame.mouse.get_pressed() 沒有響應

[英]pygame.mouse.get_pressed() not responding

代碼如下

import pygame
pygame.init()
info = pygame.display.Info()
win = pygame.display.set_mode(((info.current_h//10)*10,(info.current_w//10)*10))
running = True
while running:
    if pygame.mouse.get_pressed() == (1,0,0):
        Mouse = pygame.mouse.get_pos()
        X = (Mouse[0]//10)*10
        Y = (Mouse[1]//10)*10
        print(X)
        print(Y)
        pygame.draw.rect(win,(255,255,255),(X,Y,10,10))
    pygame.display.update()

問題是 pygame window 本身在我運行程序並單擊它時沒有響應,甚至沒有打印 X 或 Y 我嘗試添加一些延遲,認為可能 pygame 不喜歡它有多快

您必須實現一個事件循環並使用pygame.event.get()獲取事件消息。 至少你必須調用pygame.event.pump()

對於游戲的每一幀,您都需要對事件隊列進行某種調用。 這確保您的程序可以在內部與操作系統的 rest 交互。 如果您沒有在游戲中使用其他事件函數,則應調用pygame.event.pump()以允許 pygame 處理內部動作。

由於pygame.mouse.get_pressed()返回一系列布爾值,您必須使用訂閱來評估按鈕的 state:

buttons = pygame.mouse.get_pressed()
if buttons[0]:
    # [...]

我建議在應用程序中添加一個事件循環:

import pygame
pygame.init()
info = pygame.display.Info()
win = pygame.display.set_mode(((info.current_h//10)*10,(info.current_w//10)*10))
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    buttons = pygame.mouse.get_pressed()
    if buttons[0]:
        Mouse = pygame.mouse.get_pos()
        X = (Mouse[0]//10)*10
        Y = (Mouse[1]//10)*10
        print(X)
        print(Y)
        pygame.draw.rect(win,(255,255,255),(X,Y,10,10))
    pygame.display.update()

這里有幾個問題。

首先是pygame.mouse.get_pressed()返回按鈕狀態的元組,例如( True, False, False ) 但它只在pygame.event.get()被調用返回一個有效的 state 。 參考: https://www.pygame.org/docs/ref/mouse.html#pygame.mouse.get_pressed

實現您想要做的最簡單的方法是首先等待MOUSEBUTTONDOWN (或MOUSEBUTTONUP )事件,然后檢查單擊的位置和按鈕的 state 的位置。

# Main loop
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True
        elif ( event.type == pygame.MOUSEBUTTONUP ):
            # On mouse-click
            mouse_x, mouse_y = event.pos
            mouse_buttons    = pygame.mouse.get_pressed()

            if ( mouse_buttons[0] ):         # ( True, ... )
                X = ( mouse_x // 10 ) * 10
                Y = ( mouse_y // 10 ) * 10
                print( "Mouse-click at %d, %d -> %d, %d" % ( mouse_x, mouse_y, X, Y ) )
                pygame.draw.rect( win, (255,255,255), (X,Y,10,10) )

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

如您所見,按下 mouse.get 返回 boolean 變量序列,而不是您評估它的整數元組。

在運行 while 循環之前,打印pygame.mouse.get_pressed()以查看它是如何工作的以及它在您的程序中具體返回的內容。

The pygame.mouse.get_pressed() returns Boolean values such as True or False but doesn't store actual mouse cursor press position. 所以試着在你的主循環中使用它:

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_q:
            pygame.quit()
            sys.exit()
    elif event.type == pygame.MOUSEBUTTONDOWN:
        mouse_x, mouse_y = pygame.mouse.get_pos()

希望這可以幫助。 更多關於pygame.mouse命令在這里

pygame.mouse.get_pressed() 返回一個 boolean 的元組,類似於 (False,True,False)。 因此,例如,如果您想檢查是否按下了左鍵,您會這樣做

    mouse=pygame.mouse.get_pressed() #(True,False,False)
    if mouse[0]: #If first mouse button pressed AKA left click.
        #your code

關於屏幕凍結,您需要在 main while 內有一個事件循環來獲取事件消息。

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False

沒有它,pygame 屏幕將凍結。

希望這可以幫助您修復代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM