Events


Events! They're how the user interacts with the program -- the clicks, the keyboard inputs, mouse hovers, etc.

We saw a glimpse of how events worked in the previous section. In general, every event handler is going to look like this:

for event in pygame.event.get():
    if event == EVENT_A:
        #Do things
    elif event == EVENT_B:
        #do other things:
    .
    .
    .
    elif event == EVENT_X:
        #do even more other things

Pygame events come in many flavors. We've already seen pygame.QUIT, which tells us if the user pressed the x-button on the top of the window. There's a lot more than just that, and depending on what kind of event we're handling, it holds different attributes. Take a look at the following chart:

event type attributes
QUIT none
KEYDOWN unicode, key, mod
KEYUP mod
MOUSEBUTTONDOWN pos, button
MOUSEBUTTONUP pos, button
MOUSEMOTION pos, rel, buttons

And here's what each attribute means/holds:

Attribute value
unicode the string representation of the key pressed. If "d" is pressed, event.unicode will be "d".
key the code of the key pressed. If "d" is pressed, event.unicode will be 100. Note that for lowercase strings, this is the same as ord('d), but for other keys like the arrow keys, they're somewhat arbitary.
mod Gives information about any modifier keys that are active, such as shift, caps lock, etc.
pos Gives the position of the mouse as a tuple: (x,y).
button Gives information about what mouse button was pressed. Left click, for example, is 1.
rel position change since last mouse movement
buttons a tuple containing which mouse buttons are being held.

One important thing to note here is that, while all keys on the keyboard are assigned numerical values, we don't ever need to use them, because pygame stores them in readable variable names. That is, doing:

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN and event.key == 8:
        # do stuff

and

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN and event.key == pygame.K_BACKSPACE:
        #do stuff

is exactly the same thing, because pygame.K_BACKSPACE is equal to 8. But of course, we prefer the latter form because it's much easier to read. Also note that it's important to put the check for the event's type before the check for the event's key. Not all pygame events have the key attribute, so we want it to short-circuit if such is the case.


Mousepressed in Pygame

The approach above works, but it's a bit clunky. If we have to check for multiple keys, we have to do something like this:

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_BACKSPACE:
            #. . .
        elif event.key == pygame.K_D:
            #. . .
        .
        .
        .
        elif event.key == ...

This can get messy. So let's do what we did in tkinter -- make helper functions to divide up our code, namely mousepressed and keypressed:

def keypressed(event):
    pass
def mousepressed(event):
    pass

while running:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            keypressed(event)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mousepressed(event)

In fact, we take this approach in our framework. It makes our code clean and very clear as to what we want to happen with specific user inputs.

For a full reference on events, click here, and for a reference on the key object, click here.

results matching ""

    No results matching ""