event.type == MOUSEMOTION stopped working for no reason

Question:

print('Hello world!') I am making a game and I REALLY want to implement an effect that occurs when I hover my cursor over a button, which makes it slightly bigger, but the problem is, my python code just doesn’t seem to notice any movement of my cursor, so here is a bit of my program:

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()

So when I run the code, I can see a slow stream of zeroes in the command prompt, which is to be expected, but here is the trick! When I move my mouse inside of the window, nor do I see 1 nor 2, instead I only see 1 printed when I resize the window! I am really confused, as I used this command before and it worked perfectly, but now it just doesn’t! And just in case anyone asks, yes I tried to research into this, but found nothing and I see a lot of people write pygame.MOUSEMOTION instead of just MOUSEMOTION, so I don’t know if the pygame. part is necessary, but it worked without it, and adding it changes nothing

Asked By: hellwraiz

||

Answers:

pygame.event.get() get all the messages and remove them from the queue. See the documentation:

This will get all the messages and remove them from the queue. […]

If pygame.event.get() is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.

Get the events once per frame and use them in multiple loops or pass the list of events to functions and methods where they are handled:

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)
Answered By: Rabbid76
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.