Python Forum

Full Version: Pyevent counterintuitive behaviour
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So, I'll make this very quick ( I picked up on pygame this week to try it out and also prototype some physics quickly ).

This does not close the window upon pressing q

while runner:
    screen.fill(color="black")

    keys = pg.key.get_pressed()

    if keys[pg.K_q]:
        runner = False
However, this stops the window upon pressing q

while runner:
    screen.fill(color="black")

    keys = pg.key.get_pressed()

    if keys[pg.K_q]:
        runner = False

    for event in pg.event.get():
        continue
Obviously, the event for loop is checking all events that pygame is getting from the OS, and only then knows to quit it?

In fact, the for loop is not even needed

while runner:
    screen.fill(color="black")

    keys = pg.key.get_pressed()

    if keys[pg.K_q]:
        runner = False
    print("Print and exit",pg.event.get())
Output:
Output:
.... Print and exit [] Print and exit [] Print and exit [] Print and exit [] Print and exit [<Event(768-KeyDown {'unicode': 'q', 'key': 113, 'mod': 0, 'scancode': 20, 'window': None})>, <Event(771-TextInput {'text': 'q', 'window': None})>] Print and exit []
I'm looking for someone to explain why does it not stop as soon as the conditional for the while loop is switched to false, and it actually needs to touch that get function. It feels redundant.
You need to call pygame.event.pump() to update the key, or any event, info. Pygame.event.get() does this.