HW15: Event Handling

25 points; due Mon 5/26 @ 9am.

Goals

The goal of this assignment is to work with event handling, which forms the basis of most large applications with graphical user interaces.

Setup and Requirements

You may work with a partner on this assignment.

Download ball.gif and sampleevent.py. Run sampleevent.py. The program should have a ball bouncing around in a small window. When you press the Left arrow key, it should increase the ball’s leftward velocity. When you press the Q button, the program should exit.

Before you begin, read these two pygame tutorials:

Task 1: Understand the Program

The pygame library handles user input in a way that is fundamentally different from what we’ve seen up to this point. In our code, when we’ve wanted mouse input from the user, we have called the getMouse() method of a GraphWin object. This method would stop our entire program, waiting for the user to click the mouse somewhere in the window. As long as the user didn’t click in the window, the program could not advance.

This is not a reasonable approach for large programs. In many programs, you want the program running and able to react to user input. This is called event-driven programming. The idea is that any time a user does anything, such as click the mouse, press a key, move the mouse, etc., the program creates an Event object with information about the event, and adds that Event to a Queue (a data structure that is similar to a list). Then, in your program, you will get everything from the queue, and act on events that you want to act on, and ignore other events.

That is what is happening in this section of the code:

for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
    elif event.type == KEYDOWN:
        if event.key == K_q:
            pygame.quit()
            sys.exit()
        elif event.key == K_LEFT:
            speed[0] -= 1

The first line gets all the events from the queue, and iterates over them. It is important to use pygame.event.get() because it not only returns a list of those events, it also clears the queue of events that are waiting to be processed. Because there are events generated for every user action, it is important to clear the queue often.

The body of the for loop simply checks each event, and tries to see if it’s something that we want to react to. Currently, there are two event types that we care about: QUIT and KEYDOWN. The QUIT event is generated when the program is signalled to stop running. (For example, by the user clicking on the window’s X button.)

The KEYDOWN event is generate when the user presses a button on the keyboard. When a KEYDOWN event is seen, the program examines the event to try and see which key was pressed. It then reacts appropriately.

The different event types and how to get information from them can be found in the pygame documentation.

Task 2: Altering the Program

Rename you file event.py. Alter the program in the following ways:

For examples on how to deal with mouse button click events, check out the pygame documentation.

Note that pygame, despite its many advantages, is not very good at drawing simple shapes. Doing so is a lot more complicated than you might expect. In general, pygame expects you to work with sprites: pre-rendered images that you will load and manipulate in your program. So before you go forward assuming that pygame is best for your final project, think about whether you can use images for everything in your game or not.

Task 3: Additional Work

Add a background image to your program. Use the technique described in the “Moving Images in Pygame” article to move the ball around. This may require some larger changes to the sampleevent.py program.

Feel free to also alter the image that is bouncing around; it doesn’t have to be a beach ball.

Grading and Submission

Submit your files (zipped up into hw15.zip) on Moodle: event.py and all images you use.

This assignment was originally designed by Andy Exley. Thanks for sharing, Andy!