def _update_screen(self):
"""Update images on the screen, and flip to the new screen."""
[Link]([Link].bg_color)
[Link]()
[Link]()
We moved the code that draws the background and the ship and flips
the screen to _update_screen(). Now the body of the main loop in run_game()
is much simpler. It’s easy to see that we’re looking for new events, updating
the screen, and ticking the clock on each pass through the loop.
If you’ve already built a number of games, you’ll probably start out by
breaking your code into methods like these. But if you’ve never tackled a
project like this, you probably won’t know exactly how to structure your
code at first. This approach gives you an idea of a realistic development pro-
cess: you start out writing your code as simply as possible, and then refactor
it as your project becomes more complex.
Now that we’ve restructured the code to make it easier to add to, we can
work on the dynamic aspects of the game!
TRY IT YOURSELF
12-1. Blue Sky: Make a Pygame window with a blue background.
12-2. Game Character: Find a bitmap image of a game character you like or
convert an image to a bitmap. Make a class that draws the character at the
center of the screen, then match the background color of the image to the back-
ground color of the screen or vice versa.
Piloting the Ship
Next, we’ll give the player the ability to move the ship right and left. We’ll
write code that responds when the player presses the right or left arrow key.
We’ll focus first on movement to the right, and then we’ll apply the same
principles to control movement to the left. As we add this code, you’ll learn
how to control the movement of images on the screen and respond to user
input.
Responding to a Keypress
Whenever the player presses a key, that keypress is registered in Pygame as
an event. Each event is picked up by the [Link]() method. We need
to specify in our _check_events() method what kinds of events we want the
game to check for. Each keypress is registered as a KEYDOWN event.
238 Chapter 12