4-7 Responding to Basic Events

The basic event handling structure in pygame is very straightforward.  Inside the main game loop, in the Events section (ALTER), you collect events with the pygame.event.get() function.  You then check each event to see what type it is, and then respond accordingly in your code.

Below is an example that demonstrates both keyboard and mouse events.  The only output from this program will appear in the Python shell, since we are using the print() function when an event occurs.  While displaying information in the Python shell is useful for testing and debugging purposes, you would never do this in a final version of a game.

The line:

for event in pygame.event.get():

iterates through a list of events (objects) that have happened during the current frame.  For each event object we compare the type instance variable to see what kind of event it is.

pygame.QUIT occurs when the user closes the game window.

pygame.KEYDOWN occurs when the user presses a key on the keyboard.  In this case, the event object will also include a keyinstance variable that is an integer representing which key has been pressed.  To get the name of the key that was pressed (as a string) call the pygame.key.name() function with event.key as an argument.

The most common thing you’ll do with keyboard input is check if certain keys have been pressed.  For readability, Pygame has a constant for every key on the keyboard.  Use:

>>> import pygame
>>> help(pygame)

in the Python shell to see a complete list of them.  Each of the key constants begins with K_ followed by the name of the key.

pygame.MOUSEBUTTONDOWN and pygame.MOUSEBUTTONUP events occur when the user presses the mouse button or releases it, respectively.  To get the (x, y) tuple coordinate of the mouse when this occurs, call the pygame.mouse.get_pos() function.

Let’s Make a pyPaint Program!

Let’s create a very simple paint program using what we have learned so far.  The user will use the mouse to draw lines by clicking, dragging, and releasing the mouse.

In the code above, when the mouse button is pressed, we get the (x, y) starting coordinate of the mouse.  Then when the mouse button is released we get the ending (x, y) coordinate of the mouse and draw a black line between the two points with a thickness of 3 pixels.

If you run this you’ll see that the line doesn’t actually get drawn until the mouse button is released.  Let’s revise the code so that a “preview” line is shown while the mouse button is down and the mouse is being moved around.

In the Assign values to key variables (ALTER) section of the code, I’ve added a Boolean variable to keep track of whether we should draw a preview of the current line or not.  I also define two tuples to keep track of the lineStart and lineEnd coordinates of the current line.

In the Event handling (ALTER) section, if the mouse button is pressed I store the (x, y) coordinate of the mouse in lineStart and set preview to True.  If the mouse button is released, the code works the same as in the previous example.  I store the (x, y)coordinate of the mouse in lineEnd and then draw a black line with a thickness of 3 pixels.  Once we have finished drawing the final line, there is no need to draw a preview of it anymore, so I set preview to False.

The last event occurs any time the mouse is moved.  When it is, I store the current (x, y) coordinate of the mouse in lineEnd.

In the Refresh (ALTER) section, if preview is True (i.e., the mouse button has been pressed, but not released) I draw a “preview” of the line in light grey with the pixel width of 1.  All of these small changes make for a much more user-friendly experience!

You Try!

  1. Start a new page in your Learning Journal titled “4-7 Responding to Basic Events”.  Carefully read the notes above and in your own words summarize the key ideas from each section.