微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

event.type == MOUSEMOTION 无故停止工作

如何解决event.type == MOUSEMOTION 无故停止工作

print('Hello World!') 我正在制作一个游戏,我真的很想实现当我将光标悬停在按钮上时发生的效果,这使得它稍微大一点,但问题是,我的 python 代码没有似乎注意到了我光标的任何移动,所以这是我的一些程序:

def check_for_events():
    for event in pygame.event.get():
        if event.type == VIDEORESIZE:
    #does a certain thing that changes the size of everything
    #appropriately accordingly to the size of the window

def check_if_mouse_is_over_a_button():
    print(0)
    for event in pygame.event.get():
        print(1)
        if event.type == MOUSEMOTION:
            print(2)
            #some code to change size of the button

while True:
    check_for_events()
    check_if_mouse_is_over_a_button()

所以当我运行代码时,我可以在命令提示符中看到一个缓慢的零流,这是意料之中的,但这是诀窍!当我将鼠标移到窗口内时,我也看不到 1 或 2,而是在调整窗口大小时只看到打印了 1!我真的很困惑,因为我之前使用过这个命令并且它工作得很好,但现在它没有!以防万一有人问,是的,我试图对此进行研究,但一无所获,而且我看到很多人写 pygame.MOUSEMOTION 而不是 MOUSEMOTION,所以我不知道 {{ 1}} 部分是必要的,但没有它也能工作,添加它什么都不会改变

解决方法

pygame.event.get() 获取所有消息并将它们从队列中删除。请参阅文档:

这将获取所有消息并将它们从队列中删除。 [...]

如果在多个事件循环中调用 pygame.event.get(),则只有一个循环接收事件,但不会所有循环都接收所有事件。因此,似乎错过了一些事件。

每帧获取一次事件并在多个循环中使用它们或将事件列表传递给处理它们的函数和方法:

def check_for_events(event_list):
    
    for event in event_list:
        if event.type == VIDEORESIZE:
    
    #does a certain thing that changes the size of everything
    #appropriately accordingly to the size of the window

def check_if_mouse_is_over_a_button(event_list):
    print(0)
    
    for event in event_list:
        print(1)
        if event.type == MOUSEMOTION:
            print(2)
            #some code to change size of the button

while True:

    event_list = pygame.event.get()    

    check_for_events(event_list)
    check_if_mouse_is_over_a_button(event_list)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。