Chapter 18. A New Kind of Input—Events
Up until now, we have had very simple kinds of inputs for our programs. The user either typed in strings using raw_input(), or we got numbers and strings from EasyGui (in chapter 6). I also showed you how you could use the mouse to close a Pygame window, but I didn’t really explain how that worked.
In this chapter, you’ll learn about a different kind of input called events. Along the way, we’ll look at exactly what the exit code for the Pygame window is doing and how it works. We’ll also get input from the mouse and make our programs react immediately to a key being pressed, without having to wait for the user to press Enter.
If I asked you, “What’s an event?” in real life, you might say that it’s “something that happens.” That’s a pretty good definition, and that same definition is true in programming. Many programs need to react to “things that happen,” like
- the mouse being moved or clicked.
- keys being pressed.
- a certain amount of time passing.
Most of the programs we have written so far have followed a fairly predictable path from beginning to end, maybe with some loops or conditions in the middle. But there’s another whole class of programs, called event-driven programs, that don’t work that way. Event-driven programs basically sit there and do nothing, waiting until something—an event—happens. When an event does happen, they spring into action, doing whatever is necessary to handle the event.