1 year ago

#76860

test-img

Marcin Owsiany

How to ignore all pending events? `pygame.event.clear()` does not seem to do anything

My use case

Let's say I have a game which is interacting with the user, but in some circumstances it needs to do the following:

  1. show some feedback,
  2. stop responding to any user input (except maybe the quit event),
  3. pause for a few seconds,
  4. hide that feedback, and then
  5. disregard any user input that happened while the feedback was displayed
  6. go back to regular interaction

My naive solution and the problem

I though I could just use pygame.time.wait(a_few_seconds) and pygame.event.clear(). But this does not work. All keypresses that happened during these few seconds get delivered in a later pygame.event.get(), not straight away.

The example below shows just how many main loop interations happen before a long overdue event is delivered.

Did I misunderstand what clear() does? How can I solve this problem? Perhaps wait() is the wrong solution, and the main loop should continue running, just ignoring all events until enough time passes?

But then, what good is clear() for?

Repro code

Here's a minimal program that illustrates my problem:

#!/usr/bin/python3

import logging
import pygame
import time


def main():
    logging.basicConfig(datefmt='%Y-%m-%dT%H:%M:%S', format='%(name)s | %(asctime)s.%(msecs)03dZ | %(message)s', level=logging.INFO)
    pygame.init()
    _screen_size = (100, 100)
    _screen = pygame.display.set_mode(_screen_size)
    pygame.display.flip()
    while True:
        print('.', end='')
        events = list(pygame.event.get())
        if events:
            print('')
        for event in events:
            logging.info('received event: %s', event)
        for event in events:
            logging.info('processing event: %s', event)
            if event.type == pygame.QUIT:
                raise SystemExit()
            if event.type == pygame.KEYDOWN and event.unicode and event.unicode == 'x':
                logging.info('x pressed, sleeping 3s')
                pygame.time.wait(3*1000)
                logging.info('waking up')
            logging.info('clearing events')
            pygame.event.pump()
            pygame.event.clear()
            logging.info('events supposedly cleared')



if __name__ == '__main__':
    main()

I start it, give the window focus using the mouse, then press x and (while the wait() is in progress), I quickly press a, s, d keys on the keyboard. The events from these keypresses get processed well after the wait, despite the clear() calls before and between them. Each dot represents one iteration of the while True loop.

The relevant output, right when I press x is:

root | 2022-01-22T11:02:14.885Z | received event: <Event(2-KeyDown {'unicode': 'x', 'key': 120, 'mod': 4096, 'scancode': 53, 'window': None})>
root | 2022-01-22T11:02:14.885Z | processing event: <Event(2-KeyDown {'unicode': 'x', 'key': 120, 'mod': 4096, 'scancode': 53, 'window': None})>
root | 2022-01-22T11:02:14.886Z | x pressed, sleeping 3s
root | 2022-01-22T11:02:17.886Z | waking up
root | 2022-01-22T11:02:17.886Z | clearing events
root | 2022-01-22T11:02:17.902Z | events supposedly cleared
.......................................................................................................................................................................................................................................................................................................................................................................................................................................................................
root | 2022-01-22T11:02:17.906Z | received event: <Event(3-KeyUp {'key': 120, 'mod': 4096, 'scancode': 53, 'window': None})>
root | 2022-01-22T11:02:17.906Z | processing event: <Event(3-KeyUp {'key': 120, 'mod': 4096, 'scancode': 53, 'window': None})>
root | 2022-01-22T11:02:17.906Z | clearing events
root | 2022-01-22T11:02:17.906Z | events supposedly cleared
...............................................................................................................................................................
root | 2022-01-22T11:02:17.908Z | received event: <Event(2-KeyDown {'unicode': 'a', 'key': 97, 'mod': 4096, 'scancode': 38, 'window': None})>
root | 2022-01-22T11:02:17.908Z | processing event: <Event(2-KeyDown {'unicode': 'a', 'key': 97, 'mod': 4096, 'scancode': 38, 'window': None})>
root | 2022-01-22T11:02:17.908Z | clearing events
root | 2022-01-22T11:02:17.908Z | events supposedly cleared
....................................................................................................................
root | 2022-01-22T11:02:17.909Z | received event: <Event(3-KeyUp {'key': 97, 'mod': 4096, 'scancode': 38, 'window': None})>
root | 2022-01-22T11:02:17.909Z | processing event: <Event(3-KeyUp {'key': 97, 'mod': 4096, 'scancode': 38, 'window': None})>
root | 2022-01-22T11:02:17.909Z | clearing events
root | 2022-01-22T11:02:17.909Z | events supposedly cleared
..............................................................................................................................................................
root | 2022-01-22T11:02:17.911Z | received event: <Event(2-KeyDown {'unicode': 's', 'key': 115, 'mod': 4096, 'scancode': 39, 'window': None})>
root | 2022-01-22T11:02:17.911Z | processing event: <Event(2-KeyDown {'unicode': 's', 'key': 115, 'mod': 4096, 'scancode': 39, 'window': None})>
root | 2022-01-22T11:02:17.911Z | clearing events
root | 2022-01-22T11:02:17.911Z | events supposedly cleared
.................................................................................................................................................
root | 2022-01-22T11:02:17.912Z | received event: <Event(3-KeyUp {'key': 115, 'mod': 4096, 'scancode': 39, 'window': None})>
root | 2022-01-22T11:02:17.912Z | processing event: <Event(3-KeyUp {'key': 115, 'mod': 4096, 'scancode': 39, 'window': None})>
root | 2022-01-22T11:02:17.912Z | clearing events
root | 2022-01-22T11:02:17.912Z | events supposedly cleared
...................................................................................................................................................
root | 2022-01-22T11:02:17.914Z | received event: <Event(2-KeyDown {'unicode': 'd', 'key': 100, 'mod': 4096, 'scancode': 40, 'window': None})>
root | 2022-01-22T11:02:17.914Z | processing event: <Event(2-KeyDown {'unicode': 'd', 'key': 100, 'mod': 4096, 'scancode': 40, 'window': None})>
root | 2022-01-22T11:02:17.914Z | clearing events
root | 2022-01-22T11:02:17.914Z | events supposedly cleared
.................................................................................................................................................
root | 2022-01-22T11:02:17.915Z | received event: <Event(3-KeyUp {'key': 100, 'mod': 4096, 'scancode': 40, 'window': None})>
root | 2022-01-22T11:02:17.915Z | processing event: <Event(3-KeyUp {'key': 100, 'mod': 4096, 'scancode': 40, 'window': None})>
root | 2022-01-22T11:02:17.916Z | clearing events
root | 2022-01-22T11:02:17.916Z | events supposedly cleared

Versions

  • Debian 11.2
  • Python 3.9.2
  • python3-pygame 1.9.6+dfsg-4+b1
  • libsdl1.2debian 1.2.15+dfsg2-6

python

events

pygame

0 Answers

Your Answer

Accepted video resources