Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1
Timers and Animation: What Would Disney Do? 181

• The game loop is always used in Pygame. In our earlier pro-
grams, we used a loop only if we wanted to keep playing or go
back and do something again, but Pygame requires the game
loop to keep updating the screen and handling events (even if
the only event we handle is closing the window).


• We handle events in Pygame by calling pygame.event.get() to
fetch a list of events that the user has performed. These events
could be mouse clicks, key presses, or even window events
like the user closing the window. We use a for loop to handle
everything in this list of events from pygame.event.get(). In our
turtle programs, we used callback functions to handle events.
In Pygame, we can still create functions and call them in our
event handler code, but we can process events just using if
statements for those events that we care to listen for.
These differences make Pygame a new way of solving prob-
lems, and that’s what we’re always looking for! The more tools we
have, the more problems we can solve.


The Parts of a Game


In this section, we’ll change our ShowDot.py program to display a
smiley face image instead of a green circle, as shown in Figure 8-4.


Figure 8-4: ShowPic.py draws the image CrazySmile.bmp on the
screen.

Free download pdf