Dusty sunbeams cut eerie shadows across the forest floor. Dawn was breaking.
The radio message was faint at first, but became gradually stronger. 'Mayday…Mayday…' The distant hum of an airplane engine could be heard above the trees.
Captain Matt Johnson knew he was in trouble. The fuel gauge in his B-58 Hustler was well into the red, indicating that he would have no more than two minutes before his plane went down. He glanced out of the jet fighter window. Where could he land? There was nothing but a sea of trees beneath him.
He knew he had only one option remaining. Captain Johnson would have to clear a landing strip using the bombs fitted to the Hustler.
Getting started
We have an idea for a game, Forest Bomber. In Forest Bomber, an airplane is running out of fuel and has to land safely at the bottom of the screen. However, there are trees in the forest which will cause the plane to explode if it crashes into one. So the pilot of the plane – our player – has to try and bomb the trees to clear a landing strip.
OK, so we have an idea for a game, and it seems simple enough. That’s a good start. Now what?
- 1.
Set up game environment (Chapter 2).
- 2.
Initialize variables (Chapter 2).
- 3.
Display the background (Chapter 2).
- 4.
Draw the forest (Chapter 2).
- 5.
Draw the plane (Chapter 3).
- 6.
Move the plane (Chapter 3).
- 7.
Drop the bomb (Chapter 4).
- 8.
Game over/level up (Chapter 4).
- 9.
Display scoreboard and messages (Chapter 5).
I’ve split the steps over four chapters so we could gradually build up the game. The first two steps might seem a little odd, but the remaining seven steps should make some sense.
By the end of step 4, we will have designed and coded the layout for the first level of Forest Bomber.
Step 1: Set up the game environment
Lines 1–3 are called comments. They are ignored by the computer.
Lines 5–8 tell the computer that we are going to use some additional Python code which has to be imported.
Lines 11–15 are some of the colors we will be using later in the program.
Lines 18–32 set up some values which will be used throughout the program. For example, SCREEN_WIDTH and SCREEN_HEIGHT will be used to draw the actual game screen at 640 × 480 pixels, TREE_SPACING will be used to keep a 40-pixel space between each of the trees, and PLANE_START_Y will be used to start the plane 54 pixels down from the top of the screen.
Lines 36–43 set up the window in which the game will be displayed.
Lines 46–51 load the graphics which will be used in the game.
Lines 54–55 load the audio files which will be used in the game.
Step 2: Initialize variables
Now that all of the boring stuff is out of the way, we can start to write the game.
Score and lives
We will begin by gaining an understanding of variables. A variable stores a piece of information or data to be used in a program. Think of a variable as being like a box. Each variable must have its own unique name.
In Forest Bomber, we need to keep track of the score. To do this, we use a variable. We will create a box (a variable), give it the name score and place the number 0 in it.
Variables can store different types of data. Our score and level variables are both integers. An integer is a whole number which can be positive, negative, or zero.
Line 57 initializes the level variable with the value 1.
Line 58 initializes the score variable with the value 0.
Key learning
A variable is like a box which can store some data or information. Each variable has its own unique name and is initialized as follows:
Forest list variable
Lines 85–88 use a special type of variable to set up the tree formation for each level.
The variables forest_1, forest_2, forest_3, and forest_4 are lists. A list is a special kind of variable because it can store more than one item of data. There are four different forest lists because there will be four levels in the game.
‘T’ which represents a tree
‘–’ which represents a space
Notice that as we move from forest 1 through to forest 4, there are more trees. This is so that each level is harder than the one before. If you wish, you can customize Forest Bomber by changing the combination of ‘T’s and ‘-’s in the list to change the formation of trees in a level (although probably best not to do that just yet).
Key learning
A list is a special type of variable which can be used to store multiple items of data.
So far, we have entered almost 90 lines of code, and nothing is happening in our game yet!
Step 3: Display the background
Notice that line 94 and lines 102–105 are indented (which means they are spaced in from the left of the page). Also notice that line 97 has a double indent, and lines 98–99 have three levels of indentation. Python is very specific about each level of indentation. Each level of indentation should be exactly four spaces (or one tab).
This is the line of code which draws our game background. It uses the blit command to display the background image in the top left-hand corner of the game window. The background image itself is 640 × 480 pixels which is exactly the same size as the Forest Bomber window, so it fits in place perfectly.
game_screen is a variable which was initialized in line 40, and it is a representation of the Forest Bomber game window.
background_image is a variable which was initialized in line 46. It stores the image file background.png.
Now it’s time to test our program. Run Forest Bomber, and if you have entered the code correctly, the background image should be displayed. If you get an error, carefully check that all code has been entered correctly. Even the slightest mistake will cause the program to not run.
Key learning
blit draws an image onto a Pygame screen.
Pygame uses a coordinate system, where the point (0,0) is the top-left corner of the game.
Each level of indentation is four spaces or one tab.
Step 4: Draw the forest
Now that we have our game background, it’s time to draw the forest.
We have already seen that the layout of the forest for each level is stored in a list. For level 1, the forest layout is held in the list forest_1. The next block of code will draw the forest on top of the game background.
Some lines of code listing 5 in Figure 2-13 are quite complicated, and so the techniques used will be picked up later in the book.
For now, we will concentrate on lines 106–108 because they actually draw the trees in our forest.
Using math to calculate tree position
The variable tree.x will store the x coordinate of the tree (how far across the screen it should be displayed).
FIRST_TREE is a variable which has already been given the value of 140 in line 28 of the program.
column will be 0 to begin with but will then become 1, then 2, then 3, and all the way up to 11. This is because there can be 12 different positions at which a tree can be displayed from left to right.
TREE_SPACING is also a variable which has its value assigned earlier in the code – if you check back to line 27, you will see it was set to 40. This variable is the number of pixels between each tree in the forest.
The ∗ sign in Python means multiply.
And so on. This is how we work out the x coordinate of each of the 12 trees.
Deciding which trees to draw
This is an if statement. if statements are used in programming to make a decision.
Only draw a tree if the forest_item is equal to ‘T’.
Notice the use of the double equals (==) in line 107. In Python, we use == to check if two things are equal.
Notice that line 108 is indented. If the forest_item is a ‘T’, then all indented lines directly below the line will be executed. In this case, the only indented line is 108, so that will be executed if the forest_item is a ‘T’.
We learned about blit earlier in this chapter when we drew the background image.
We also saw that game_screen is a variable which stores the Forest Bomber game window.
tree_image is a variable used to store the image of a tree. It was initialized in line 47 of the program where it was loaded with the file tree.png – the picture of a tree.
We have already seen how tree.x is initialized by some math which calculates how far horizontally across the screen each tree should be placed.
tree.y is a variable initialized in line 77 of the program. It uses some math to work out where to place the tree vertically, a few pixels above the bottom of the game screen.
Let’s ignore lines 109–110. They will display the image of a tree on fire if it has been hit by a bomb. As we have not written the code to drop a bomb yet, these lines won’t actually do anything.
Go through each forest item in our forest list.
Use math to calculate the x coordinate of the tree.
If the forest item is a tree, then draw a tree in the correct location.
Run the program. The four trees of Forest Bomber level 1 should be displayed along the bottom of the game window.
Key learning
if is used to make a decision.
Double equals (==) are used to check if two things are equal.
The indented lines below an if statement will be executed if the statement works out to be true.
Summary
Phew! No one said learning to code would be easy. We have covered a lot of complex programming in this first chapter because we prefer to dive right in. Don’t worry if it all seems difficult to understand. It is, but it will get easier, and we will revisit many of these topics as we work through the book.
So far, we have written lots of lines of code, and all we have to show for it is our game background and some trees. The Forest Bomber game will begin to take a bit more shape in the next chapter.