Captain Matt Johnson looked out of the window of his B-58 Hustler. The forest below stretched out like a giant green carpet as far as the eye could see. He studied the fuel gauge again. The dial pointed directly at the letter E.
Empty.
The B-58 Hustler began making its descent…
In the previous chapter, we completed steps 1–4 of our game design and learned how to draw the background and trees onto the screen. In this chapter, we will learn how to draw and move Captain Johnson’s B-58 Hustler plane.
- 5.
Draw the plane.
- 6.
Move the plane.
Step 5: Draw the plane
We can see another if statement has been used here, so we know that the program code is making a decision. It is checking to see if plane_exploded is False. If we look all the way back to line 63, we can see that plane_exploded is a variable and it is initialized to False.
What is False? In programming, there is a variable type known as a Boolean, and it can have one of only two possible values: True or False. Boolean variables are useful in game programming, because they can only be one or the other. There is no in-between. They can only be either True or False. Later in the program, we will set the value of the plane_exploded Boolean variable to True when our plane crashes into a tree. But for now, it’s False.
Given that the value of plane_exploded is False (at least for now), then line 114 will execute. Line 114 uses the blit command to draw the plane image at coordinates (plane.x,plane.y).
plane_image is a variable which stores plane.png (line 45).
plane.x is initialized with the value PLANE_START_X (line 73). PLANE_START_X is initialized with the value 0 (line 32) so plane.x will have the value 0.
plane.y is initialized with the value PLANE_START_Y (line 74). PLANE_START_Y is initialized with the value 54 (line 33) so plane.y will have the value 54.
Our plane will first appear at coordinates (0,54).
Run the program. The plane should appear near the top of the screen, on the far left-hand side.
Key learning
A Boolean variable can store one of two values: True or False.
Step 6: Move the plane
In game programming, we move a sprite around the game screen by changing its coordinates and then redrawing the screen.
Fly across the screen
In order to get the plane to move across the screen, we increase the x coordinate and then redraw the screen. The more we increase the x coordinate by, the faster the plane will fly. We will increase the x coordinate by 5, which means it will move to the right by 5 pixels.
Take the variable which stores the plane’s x coordinate.
Add 5 onto it.
We learned earlier in this chapter that plane.x was initialized with 0. When line 101 is executed, it will take what is stored in plane.x (in this case 0) and add on 5. So the new value of plane.x is 5.
while True means to repeat doing something – forever. Look down at the rest of the program code below line 92. It is all indented. This means that all the indented code will keep repeating, forever (well at least until the user closes the game window). This also means that 5 will be repeatedly added onto the x coordinate of the plane.
Line 122 redraws the screen, while line 123 determines how many times the screen should be redrawn in one second, in this case 30.
So our program now draws the plane, moves it 5 pixels right, redraws the plane, moves it another 5 pixels right, and so on, creating the illusion of the plane moving.
Test this by running the program.
Fantastic, right? Except for one thing. Our plane flies off the end of the screen never to be seen again, and that won’t make for much of a game!
Fly down the screen
If the plane flies off the right-hand side of the screen, move it down the screen and back to the left-hand side.
Down: We add 100 to its y coordinate.
Back to the left: We set its x coordinate back to 0.
Run the program. Now the plane should fly to the right edge of the screen and then drop down by 100 pixels beginning again on the left-hand side, except now it flies all the way to the bottom of the screen and then disappears. We will fix this in a later chapter.
Now we are going to make a couple of minor changes to the code that flies our plane and which will be useful later.
We have added + speed_boost to the end of the line, but why? We want to make the game get a little harder for levels 3 and 4. For levels 1 and 2, speed_boost is 0, so it makes no difference to the speed of the plane. But for levels 3 and 4, we will set speed_boost to 1. This means we will be adding 6 to the plane’s x coordinate for levels 3 and 4 instead of 5. It will make the plane fly slightly faster across the screen and make the game just that little bit harder.
We have not reached the end of the level.
The plane has not exploded.
If either of these events occurs, we do not want to move our plane. We will learn later in the book how we change the values of level_cleared when the level is over and plane_exploded when the plane crashes into a tree.
Key learning
To move a sprite, change its coordinates and then redraw the screen.
while True is used in Python game programming to repeat forever.