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

3. Forest Bomber part 2: Is it a bird…?

Mark Cunningham1 
(1)
Edinburgh, Scotland
 

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.

Picking up the steps from our original plan
  1. 5.

    Draw the plane.

     
  2. 6.

    Move the plane.

     

Step 5: Draw the plane

Insert code listing 6 between lines 112 and 117 as shown in Figure 3-1.
../images/482396_1_En_3_Chapter/482396_1_En_3_Fig1_HTML.jpg
Figure 3-1

Forest Bomber code listing 6

Let’s take a closer look at lines 113 and 114 in Figure 3-2.
../images/482396_1_En_3_Chapter/482396_1_En_3_Fig2_HTML.jpg
Figure 3-2

Forest Bomber code listing lines 113 and 114

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

We can see from previous lines of code in our program that
  • 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.

Insert code listing 7 at line 101.
../images/482396_1_En_3_Chapter/482396_1_En_3_Fig3_HTML.jpg
Figure 3-3

Forest Bomber code listing 7

Line 101 can be read as
  • 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.

We need to do this repeatedly; otherwise, the plane will only move 5 pixels once, which won’t be much use. It’s time to look back at another line of code that we wrote earlier, line 92.
../images/482396_1_En_3_Chapter/482396_1_En_3_Fig4_HTML.jpg
Figure 3-4

Forest Bomber code listing line 92

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.

In order to see the plane actually move, we need to redraw the screen. Let’s look at some code that we wrote earlier but didn’t pay much attention to, the last two lines of code in the program, lines 122 and 123.
../images/482396_1_En_3_Chapter/482396_1_En_3_Fig5_HTML.jpg
Figure 3-5

Forest Bomber code listing lines 122 and 123

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

Let’s take a moment to consider the logic to make the plane 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.

To move the plane
  • Down: We add 100 to its y coordinate.

  • Back to the left: We set its x coordinate back to 0.

Add the code to fly the plane down the screen shown in code listing 8, Figure 3-6.
../images/482396_1_En_3_Chapter/482396_1_En_3_Fig6_HTML.jpg
Figure 3-6

Forest Bomber code listing 8

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.

First, adapt line 102 so that it reads as in Figure 3-7.
../images/482396_1_En_3_Chapter/482396_1_En_3_Fig7_HTML.jpg
Figure 3-7

Forest Bomber code listing line 102

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.

The second change is to insert an if statement at line 102, Figure 3-8.
../images/482396_1_En_3_Chapter/482396_1_En_3_Fig8_HTML.jpg
Figure 3-8

Forest Bomber code listing line 102 with if statement

The purpose of this line is to make sure we only move the plane if
  • 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.

The code should now look like code listing 9 in Figure 3-9. One very important thing to note – because we added an if statement at line 102, lines 103, 105, 106, and 107 have all had one extra indentation.
../images/482396_1_En_3_Chapter/482396_1_En_3_Fig9_HTML.jpg
Figure 3-9

Forest Bomber code listing 9

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.

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

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