Module 4 - Section 2 - Graphics
Module 4 - Section 2 - Graphics
- identify the commands inside the Pygame basic template
- experiment with various graphics commands
- draw shapes using Python commands
- draw text using Python commands
4.2.4 - User interaction and event processing
With just the code written so far, the program would create a window and immediately hang. The user can't interact with the window, even to close it. All of this interaction needs to be programmed. Code needs to be added so that the program waits in a loop until the user clicks “exit.”
This is the most complex part of the program, and a complete understanding of it isn't needed yet. But it is necessary to have an idea of what it does, so spend some time studying it and asking questions.
As you can see in lines 11-13 the script checks if the user clicked QUIT. If this event is TRUE then variable done becomes TRUE and the scripts gets out of the loop terminating the program.
There are many other events that we can process :
The events (like pressing keys) all go together in a list. The program uses a for loop to loop through each event. Using a chain of if statements, the script figures out what type of event occurred. The code to handle that event goes in the if statement.
All the if statements should go together, in one for loop. A common mistake when doing copy and pasting code is to not merge loops from two programs, but to have two event loops.