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,所以我不知道pygame.部分是否必要,但没有它它它就起作用了,添加它不会改变

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)

最新更新