Skip to content
Hystersis edited this page Jan 11, 2022 · 2 revisions

Welcome to the Pygame-Event-Manager wiki!

Contents:

Usage:

Use this program as a wrapper to all for more concurrent use of PyGame events and it allows for multiple events of the same type to be polled from different parts of the code.

Imports:

Pygame is needed obviously and typing allows for type hints in IDEs and code editors - so you can see what types of variables should be passed in.

Functions:

__init__ (Initialization):

This creates all the variables needed for the system to work, e.g.

name_of_var = events_sync()

__call__ (Updating):

This allows for all the events to be processed, and appropriate actions to be taken, e.g.

name_of_var()

config (Key remapping):

This allows for key phrases - a unique string-based identifier - to be mapped to Pygame key, e.g.

name_of_var.config('down', pygame.K_s)

listen (Event and function assigning)

This allows for the python function to be assigned to events, even keyboard events with specific keys! Arguments can be passed into these functions, e.g.

Keyboard event with key

name_of_var.listen(pygame.KEYDOWN, my_func, pygame.K_s)

Keyboard event with key phrase

name_of_var.listen(pygame.KEYDOWN, my_func, 'down')

Event with args

name_of_var.listen(pygame.QUIT, my_quit_func, None, x, y, z, a=b, c=d)

Important None must be put in place of the usual keyboard key if args and kwargs are used.

Errors that can arise:

If a keyphrase is passed in that is never defined using name_of_var.config() an error will be raised looking like this:

Traceback (most recent call last):
  File "c:\path_to_file\name_of_file.py", line 1, in <module>
    e.listen(pygame.KEYDOWN, t, 'hello')
  File "c:\path_to_file\name_of_file.py", line 2, in listen
    raise Exception(
Exception: The provided key phrase: xyz, was not defined or set, did you use *.config('xyz', *)?

This error can just be fixed using name_of_var.config('xyz', pygame.K_a)

latch ('latching' onto certain events):

This allows for events to be latched onto for later action, e.g.

name_of_var.latch(pygame.QUIT)

retrieve (retrieving latched events):

This allows events that have been latched onto be retrieved in order of [least recent -> most recent], so least recent first, e.g.

x = name_of_var.retrieve(pygame.QUIT)