1
\$\begingroup\$

I want to have the type of an event from the event queue to decide certain actions (i.e. if it is a joystick event or a keyboard one), but the dir of events is an empty list:

for e in pygame.event.get():
    print dir(e)

Although in the documentation i read:

The queue is a regular queue of pygame.event.EventTypepygame object for representing SDL events event objects

i can't access a __dict__ attribute. The above code prints a bunch of empty lists: [] [] [] [] etc. In pygame 1.9.4 installed from pip i could run:

if e.type == pygame.JOYBUTTONDOWN:
    e.__dict__["joy"]

and i could get the number of joystick button pressed, but this is not working in 1.9.4 installed with sudo apt install (on Debian):

print event.__dict__
AttributeError: event member not defined
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You should do if-elif-elif with e.type. First import the event constants you need to check from pygame.constants For example

from pygame.constants import QUIT, MOUSEBUTTONDOWN
while true:
    for e in pygame.event.get():
        if e.type == QUIT:
            #close the game
        elif e.type == MOUSEBUTTONDOWN:
            #mouse click

The dir() function gives the directory where a module is located, that doesn't work as you think

\$\endgroup\$

You must log in to answer this question.