【问题标题】:MOUSEBUTTONDOWN event not responding [pygame]MOUSEBUTTONDOWN 事件没有响应 [pygame]
【发布时间】:2021-02-27 10:48:13
【问题描述】:

鼠标按钮按下时,我无法检测到该事件。

在问这个问题之前我已经试过了:

import pygame, sys
pygame.init()
win = pygame.display.set_mode((1260, 960))
pygame.display.set_caption("PONG")

finish = False
fps = 60

clock = pygame.time.Clock()

while not finish:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.MOUSEBUTTONDOWN:
                print('MOUSE BUTTON PRESSED DOWN')
    clock.tick(fps)
    pygame.display.update()

代码有什么问题?

另外,我的终端没有收到任何错误。

【问题讨论】:

  • 看来您的编码是为了检查 MOUSEBUTTONDOWN 只有在同时发生事件 KEYDOWN 时才会发生

标签: python python-3.x pygame


【解决方案1】:

MOUSEBUTTONDOWN 不是按键事件

这应该可行

import pygame, sys
pygame.init()
win = pygame.display.set_mode((1260, 960))
pygame.display.set_caption("PONG")

finish = False
fps = 60

clock = pygame.time.Clock()

while not finish:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        
        if event.key == pygame.MOUSEBUTTONDOWN:
            print('MOUSE BUTTON PRESSED DOWN')
    clock.tick(fps)
    pygame.display.update()

【讨论】:

    【解决方案2】:

    看看documentation

    QUIT              none
    ACTIVEEVENT       gain, state
    KEYDOWN           key, mod, unicode, scancode
    KEYUP             key, mod
    MOUSEMOTION       pos, rel, buttons
    MOUSEBUTTONUP     pos, button
    MOUSEBUTTONDOWN   pos, button
    JOYAXISMOTION     joy (deprecated), instance_id, axis, value
    JOYBALLMOTION     joy (deprecated), instance_id, ball, rel
    JOYHATMOTION      joy (deprecated), instance_id, hat, value
    JOYBUTTONUP       joy (deprecated), instance_id, button
    JOYBUTTONDOWN     joy (deprecated), instance_id, button
    VIDEORESIZE       size, w, h
    VIDEOEXPOSE       none
    USEREVENT         code
    

    您正在寻找MOUSEBUTTONDOWN 事件,而不是KEYDOWN

    for event in pygame.event.get():
        ...
        if event.type == pygame.MOUSEBUTTONDOWN:
            print('MOUSE BUTTON PRESSED DOWN')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多