Pygame loop checking velocity rarely

Your problem is that when you call pygame.event.get(), that function not only gets the events, but also removes them from the queue. This means calling it twice per frame (as you do in set_vel and handle) can give weird results.

When I write pygame, I have one for event in pygame.event.get() loop in my while True. Try doing this and move the quit handling and velocity changing into the True loop instead of their own functions.


As mentioned in the other answer pygame.event.get() get all the messages and remove them from the queue. So either the 1st or the 2nd loop gets an event, but never both loops will get all events. That causes that some events seems to be missed.

Ge the list of events once in the main application loop and pass the list to the functions:

def handle(events):
    global x, y
    for event in events:
        if event.type == QUIT:
            pygame.quit()
def set_vel(events):
    for event in events:
        # [...]
while True:
    events = pygame.event.get()
  
    velX, velY = set_vel(events )
    clear(aX, aY)
    handle(events)

    # [...] 

Tags:

Python

Pygame