![](/img/trans.png)
[英]`pygame.mouse.get_pressed()` reports clicks when mouse is not clicked
[英]Python pygame pygame.mouse.get_pressed()[0] responses when i move mouse, not when the mouse is in the same position
我正在制作一個可以射擊的 2d 游戲,所以邏輯是在pygame.mouse.get_pressed()[0]
(按下鼠標)子彈時。 問題是,當我將手指放在鼠標上並按下鍵時,我的代碼會在我開始單擊時創建一顆子彈,當我仍然單擊時什么也沒發生,當我移動鼠標時,子彈開始創建。 Pygame 當鼠標在單個 position 時不要創建子彈,我必須移動鼠標。 怎么了。 這是代碼的重要片段。 (它在 def run() 中)
它在一個循環中( https://i.stack.imgur.com/0Y86f.png )
pygame.mouse.get_pressed()
不是事件,而是返回按鈕當前的state。 您必須使用MOUSEBUTTONDOWN
事件。 另請參閱Pygame 鼠標點擊檢測。
pygame.mouse.get_pressed()
返回 Boolean 個值的列表,代表所有鼠標按鈕的 state( True
或False
)。 只要按下按鈕,按鈕的 state 就是True
。 如果您只想檢測鼠標按鈕何時被按下或釋放,則必須實現MOUSEBUTTONDOWN
和MOUSEBUTTONUP
(請參閱pygame.event
模塊):
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
print("clicked", event.button)
if event.type == pygame.MOUSEBUTTONUP:
print("released", event.button)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.