© Mark Cunningham 2020
M. CunninghamGame Programming with Code Angelhttps://doi.org/10.1007/978-1-4842-5305-2_2

2. Forest Bomber part 1: A first level

Mark Cunningham1 
(1)
Edinburgh, Scotland
 

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?

Before we even begin writing any of the Forest Bomber code, we need to design the game in more detail by taking the idea and breaking it down into smaller steps, covered here and in Chapters 3 to 5 as follows:
  1. 1.

    Set up game environment (Chapter 2).

     
  2. 2.

    Initialize variables (Chapter 2).

     
  3. 3.

    Display the background (Chapter 2).

     
  4. 4.

    Draw the forest (Chapter 2).

     
  5. 5.

    Draw the plane (Chapter 3).

     
  6. 6.

    Move the plane (Chapter 3).

     
  7. 7.

    Drop the bomb (Chapter 4).

     
  8. 8.

    Game over/level up (Chapter 4).

     
  9. 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.

Let’s begin by sketching out how the game screen will look (Figure 2-1). The plane should start in the top left-hand area of the screen, and the forest, which the player must clear in order to land, will run along the bottom of the screen. As this will be the first level, we will start with four trees in the forest, but the number of trees will increase in each level making the game progressively more difficult.
../images/482396_1_En_2_Chapter/482396_1_En_2_Fig1_HTML.jpg
Figure 2-1

Forest Bomber level 1 design

Step 1: Set up the game environment

Before we can even begin to think about planning the main gameplay, we have to start by setting up some basic elements of the game environment. This is fairly boring stuff, and so the plan is to skip by it as quickly as possible so that we can concentrate on the main game where we will be able to develop much more useful and interesting coding skills. OK, here goes. Enter the lines of code exactly as they are shown in code listing 1 in Figure 2-2.
../images/482396_1_En_2_Chapter/482396_1_En_2_Fig2_HTML.jpg
Figure 2-2

Forest Bomber code listing 1

We won’t worry too much about the code at this stage, but if you want to know more, the following explains what it does:
  • 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.

Again, enter the lines of code shown in code listing 2 in Figure 2-3.
../images/482396_1_En_2_Chapter/482396_1_En_2_Fig3_HTML.jpg
Figure 2-3

Forest Bomber code listing 2

  • 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.

When a variable is first given a value, it is being initialized. The score variable is initialized with the value 0 (Figure 2-4).
../images/482396_1_En_2_Chapter/482396_1_En_2_Fig4_HTML.jpg
Figure 2-4

The variable score, which stores the value 0

We will also need to keep track of the game level. Again, we use a variable, but this time we will call it level and initialize it with 1, because the game will begin at level 1 (Figure 2-5).
../images/482396_1_En_2_Chapter/482396_1_En_2_Fig5_HTML.jpg
Figure 2-5

The variable level, which stores the value 1

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.

Enter the code to initialize all of the game variables, as shown in code listing 3 in Figure 2-6.
../images/482396_1_En_2_Chapter/482396_1_En_2_Fig6_HTML.jpg
Figure 2-6

Forest Bomber code listing 3

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:

score = 0
lives = 1

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.

Each forest list stores 12 items, which can be either
  • ‘T’ which represents a tree

  • ‘–’ which represents a space

Figures 2-7 to 2-10 show how the four forests are stored as lists and how each list is then mapped to a tree drawn on the screen. We will learn later in this chapter how we actually draw the trees.
../images/482396_1_En_2_Chapter/482396_1_En_2_Fig7_HTML.jpg
Figure 2-7

Level 1 illustration using the forest_1 list

../images/482396_1_En_2_Chapter/482396_1_En_2_Fig8_HTML.jpg
Figure 2-8

Level 2 illustration using the forest_2 list

../images/482396_1_En_2_Chapter/482396_1_En_2_Fig9_HTML.jpg
Figure 2-9

Level 3 illustration using the forest_3 list

../images/482396_1_En_2_Chapter/482396_1_En_2_Fig10_HTML.jpg
Figure 2-10

Level 4 illustration using the forest_4 list

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

It is time to display some of the graphics for Forest Bomber, and we will begin with the background graphic. Enter lines 91–105 as shown in code listing 4 in Figure 2-11.
../images/482396_1_En_2_Chapter/482396_1_En_2_Fig11_HTML.jpg
Figure 2-11

Forest Bomber code listing 4

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).

Let’s take a closer look at line 102.
../images/482396_1_En_2_Chapter/482396_1_En_2_Figa_HTML.jpg

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.

Pygame uses a coordinate system to draw graphics on screen, and the point (0,0) is in the upper left-hand corner as can be seen in Figure 2-12.
../images/482396_1_En_2_Chapter/482396_1_En_2_Fig12_HTML.jpg
Figure 2-12

Pygame coordinate system

There are a couple of other things to note about the way in which the background image is displayed by line 102:
  • 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.

To draw the forest, code listing 5 should be added between lines 102 and 104 as shown in Figure 2-13.
../images/482396_1_En_2_Chapter/482396_1_En_2_Fig13_HTML.jpg
Figure 2-13

Forest Bomber code listing 5

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

Line 106 does a bit of math to work out how far across the game screen to place each tree.
../images/482396_1_En_2_Chapter/482396_1_En_2_Figb_HTML.jpg
  • 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.

So let’s do the math…
When column is 0:
tree.x = FIRST_TREE + column ∗ TREE_SPACING
       = 140 + 0 ∗ 40
       = 140
When column is 1:
tree.x = FIRST_TREE + column ∗ TREE_SPACING
       = 140 + 1 ∗ 40
       = 180
When column is 2:
tree.x = FIRST_TREE + column ∗ TREE_SPACING
       = 140 + 2 ∗ 40
       = 220

And so on. This is how we work out the x coordinate of each of the 12 trees.

Deciding which trees to draw

But wait – we don’t actually want to display all 12 trees. We only want to display a tree when our forest list has a T. That’s where line 107 comes in.
../images/482396_1_En_2_Chapter/482396_1_En_2_Figc_HTML.jpg

This is an if statement. if statements are used in programming to make a decision.

This code is saying
  • 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.

Line 107 doesn’t actually draw anything though; it is only making the decision. If the forest_item is equal to ‘T’, then line 108 will be executed, and line 108 is the line which draws the tree on the screen.
../images/482396_1_En_2_Chapter/482396_1_En_2_Figd_HTML.jpg

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’.

Line 108 uses the blit command to draw a tree_image onto the game_screen at coordinates (tree.x,tree.y). But where do all of these commands and values come from?
  • 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.

We can summarize lines 105–108 as follows:
  • 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.110.235