2 回答

TA貢獻1795條經驗 獲得超7個贊
pygame.mouse.get_pressed()
當處理事件時,將評估返回的坐標。pygame.event.pump()
您需要通過或 來處理事件pygame.event.get()
。
參見pygame.event.get()
:
對于游戲的每一幀,您都需要對事件隊列進行某種調用。這確保您的程序可以在內部與操作系統的其余部分進行交互。
pygame.mouse.get_pressed()
返回代表所有鼠標按鈕狀態的布爾值序列。因此,您必須評估any
按鈕是否被按下(any(buttons)
)或者是否通過訂閱按下了特殊按鈕(例如buttons[0]
)。
例如:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 800))
run = True
while run:
? ? for event in pygame.event.get():
? ? ? ? if event.type == pygame.QUIT:
? ? ? ? ? ? run = False
??
? ? buttons = pygame.mouse.get_pressed()
? ? # if buttons[0]:? # for the left mouse button
? ? if any(buttons):? # for any mouse button
? ? ? ? print("You are clicking")
? ? else:
? ? ? ? print("You released")
? ? pygame.display.update()
如果您只想檢測鼠標按鈕何時按下或釋放,那么您必須實現MOUSEBUTTONDOWN
and?MOUSEBUTTONUP
(參見pygame.event
模塊):
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 800))
run = True
while run:
? ? for event in pygame.event.get():
? ? ? ? if event.type == pygame.QUIT:
? ? ? ? ? ? run = False
? ? ? ? if event.type == pygame.MOUSEBUTTONDOWN:
? ? ? ? ? ? print("You are clicking", event.button)
? ? ? ? if event.type == pygame.MOUSEBUTTONUP:
? ? ? ? ? ? print("You released", event.button)
? ? pygame.display.update()
Whilepygame.mouse.get_pressed()返回按鈕的當前狀態,而 MOUSEBUTTONDOWN和MOUSEBUTTONUP僅在按下按鈕后發生。

TA貢獻1848條經驗 獲得超10個贊
函數 pygame.mouse.get_pressed 返回一個包含 true 或 false 的列表,因此對于單擊,您應該使用-
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 800))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
mouse = pygame.mouse.get_pressed()
if mouse[0]:
print("You are clicking")
else:
print("You released")
添加回答
舉報