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

4. Forest Bomber part 3: Bombs away…

Mark Cunningham1 
(1)
Edinburgh, Scotland
 

Captain Johnson looked down at the flight control panel of his B-58 Hustler jet. He knew what he had to do. His finger hovered over the button marked ‘Release.’ Timing was all-important now. He could not afford to miss his target. A bead of sweat slipped slowly down his right temple.

Wait…Wait…Wait…Now!

He hit the button, and a bomb dropped out of the B-58 and began hurtling toward the forest below.

In the last chapter, we learned how to make the plane fly across the screen. Next, we will learn how to write the code which will drop a bomb, and then we will develop the end of level and game over code.

From our plan
  1. 7.

    Drop the bomb.

     
  2. 8.

    Game over/level up.

     

Step 7: Drop the bomb

Key presses

So far in Forest Bomber, there is not much for the player to do other than sit back and watch the plane fly across and down the screen until it eventually disappears off the bottom right-hand corner. Not much of a game! We want our player to be able to interact with Forest Bomber so that when they hit the spacebar, a bomb is dropped.

Pygame has a way of capturing or storing any key presses.

The code will look as in Figure 4-1.
../images/482396_1_En_4_Chapter/482396_1_En_4_Fig1_HTML.jpg
Figure 4-1

Key press code

  • The first line assigns any key presses to a variable key_pressed.

  • The second line checks to see if the key_pressed variable holds a specific key, in this case the spacebar.

Let’s build the key press code into Forest Bomber. Insert code listing 10 (Figure 4-2) at line 96.
../images/482396_1_En_4_Chapter/482396_1_En_4_Fig2_HTML.jpg
Figure 4-2

Forest Bomber code listing 10

We already know that lines 97 and 98 check to see if the spacebar was pressed. Lines 99–102 is the code which will run when the spacebar is pressed.

Let’s examine line 99 more closely. It is another if statement . This statement checks for three things. It checks if
  • bomb_dropped is False

  • level_cleared is False

  • plane_exploded is False

In Chapter 3, we learned about Boolean variables that can store either True or False. Let’s look again at the if statement to work out exactly what we are checking for:
  • bomb_dropped is False

Is there a bomb already dropping? If so, we do not want to allow another bomb to be dropped because it would make the game too easy. We have to wait until any previous bombs have exploded or disappear off-screen before allowing the player to drop the next one.
  • level_cleared is False

We can’t drop a bomb if the level has cleared because the game is waiting to level up.
  • plane_exploded is False

We can’t drop a bomb if the plane has exploded because it is game over.

We need all three of these statements to be False before we can drop a bomb. To check that they are all False, we place and between each statement.

If all three statements are False, then lines 100 and 102 are executed:
  • Line 100 sets the Boolean variable bomb_dropped to True. This will prevent another bomb being dropped until bomb_dropped is set back to False again.

  • Line 101 sets the x coordinate of the bomb to the value of the x coordinate of the plane, plus 15. This means that the bomb will be displayed horizontally in the middle of the plane.

  • Line 102 sets the y coordinate of the bomb to the value of the y coordinate of the plane plus 10. This means that the bomb will be displayed 10 pixels below the plane.

We now need some code to display the bomb on screen. Remember how we wrote the code to display the plane by using the blit command? We will use very similar code to display the bomb.

Insert code listing 11 (Figure 4-3) at line 135.
../images/482396_1_En_4_Chapter/482396_1_En_4_Fig3_HTML.jpg
Figure 4-3

Forest Bomber code listing 11

Key learning

pygame.key.get_pressed() checks any key presses.

The and keyword can be used to join multiple conditions within a single if statement.

When and is used, all the statements have to work out as being true for the if statement to execute.

Now run the program and try hitting the spacebar. A bomb should appear where the plane is – it just doesn’t move yet.

Move the bomb

In Chapter 3, we learned how to move the plane by changing its coordinates and then redrawing the screen. We will move the bomb in exactly the same way.

Add code listing 12 (Figure 4-4) at line 117.
../images/482396_1_En_4_Chapter/482396_1_En_4_Fig4_HTML.jpg
Figure 4-4

Forest Bomber code listing 12

This code can be read as
  • If a bomb has been dropped

  • Move it down 3 pixels.

  • Move it right 5 pixels.

Test the game by running it. If we press the spacebar, the bomb drops. The only problem is it keeps going off the screen and we can never launch another bomb. This is because the bomb_dropped Boolean variable is set to True when the bomb is launched and never changed back to False.

Remember the code that tests if a bomb can be dropped (Figure 4-5)?
../images/482396_1_En_4_Chapter/482396_1_En_4_Fig5_HTML.jpg
Figure 4-5

Code which tests if a bomb can be launched

A bomb can only be launched if bomb_dropped is False. Let’s fix our program so that when a bomb disappears off-screen, bomb_dropped is reset to False. Insert code listing 13 (Figure 4-6) at line 122 (note that line 122 has two indentations – eight spaces).
../images/482396_1_En_4_Chapter/482396_1_En_4_Fig6_HTML.jpg
Figure 4-6

Forest Bomber code listing 13

  • Line 122 tests to see if the bomb has gone below the bottom of the screen.

  • Line 125 tests to see if the bomb has gone beyond the right-hand side of the screen.

If the bomb has disappeared off the bottom, or off the right of the screen, bomb_dropped will be set to False which means we can now use the spacebar to drop a new bomb.

Let’s take a closer look at line 122. It compares the y coordinate of the bomb (bomb.y) with the SCREEN_HEIGHT . But how does the program know what SCREEN_HEIGHT is?

Look back to lines 18 and 19 of the program. SCREEN_WIDTH is set to 640 and SCREEN_HEIGHT is set to 480. We don’t have to do this, but it makes our code easier to read. And if we decided we wanted a bigger screen for our game, say 800 × 600, then we would just have to set SCREEN_WIDTH and SCREEN_HEIGHT to 800 and 600, respectively, at the start of the program.

You may be also wondering why we use capital letters for some variable names, like SCREEN_WIDTH and SCREEN_HEIGHT. This is because they are constants. A constant is assigned a value at the start of the program, and it doesn’t change. Python’s naming convention suggests we should use capital letters to indicate the use of constants. That way we know that we are dealing with a constant value, and therefore its value should not be changed by the program.

There is one other part of line 122 that merits a closer look. Previously when comparing values, we have used the double equals sign (==) to check whether two values are equal. In line 122 (and also in line 125), we use the greater than (>) symbol. So line 122 actually reads
  • if the y coordinate of the bomb is greater than the screen width

The full list of Python’s comparison operators is as follows:

Comparison Operator

Meaning

==

Is equal to

>

Is greater than

>=

Is greater than or equal to

<

Is less than

<=

Is less than or equal to

!=

Is not equal to

Key learning

The Python naming convention suggests using capital letters to indicate a constant value.

Python has a range of comparison operators: ==, >, >=, <, <=, and !=.

Run the program to test that a new bomb can be dropped once the previous bomb has disappeared off the edge of the screen.

Exploding trees

The code to blow up a tree is quite complex, and given we are only just beginning to learn how to code, we will only focus on a small part of it. Let’s begin by adding code listing 14 (Figure 4-7) at line 128.
../images/482396_1_En_4_Chapter/482396_1_En_4_Fig7_HTML.jpg
Figure 4-7

Forest Bomber code listing 14

Let’s take a brief overview of lines 128–139 to see what they do:
  • Check each of the trees in the forest to see if the bomb has hit it.

  • If it has
    • We increase the score.

    • We play an explosion sound.

    • We set a timer to keep a burning tree graphic on screen for ten frames.

Let’s take a moment to focus on two of the more straightforward lines of code that have been used in this block, the code that is run when a bomb has hit a tree:
  • Line 135 sets the Boolean variable bomb_dropped to False. This means that a new bomb can be dropped when the player hits the spacebar.

  • Line 138 increases the value held in the score variable. If you remember back to the beginning of Chapter 2, we set score to 0 and level to 1. Line 138 adds 10 × the current level to score:
    • As level is currently 1, the value 10 (10 × 1) will be added to the current score.

    • As score was initialized with the value 0, score will become 10 when a first tree is hit.

    • When a second tree is hit, score will become 20 and so on.

We will learn how to display the score and level in the next chapter.

The code between lines 142 and 147 uses a timer to display the burning tree image for a series of ten frames before removing it from the game altogether. The code is beyond the scope of this chapter, so we will skip past it.

Test the game again. You should find that if the bomb hits a tree, it explodes.

Ground level

Our game of Forest Bomber is starting to take shape. The big problem now is that when our plane reaches the ground level, it flies through the trees and off the screen.

Code listing 15 (Figure 4-8) addresses this. Insert the code at line 149.
../images/482396_1_En_4_Chapter/482396_1_En_4_Fig8_HTML.jpg
Figure 4-8

Forest Bomber code listing 15

We won’t go into this code in too much detail, but in summary
  • Line 150 uses a bit of math to work out if the y coordinate of the plane is at the ground level.

  • If it is, line 151 calculates the position of the front of the plane.

  • Line 152 says if the plane has got beyond the right-hand side of the screen, then the level has been cleared, so set the Boolean variable level_cleared to True.

  • Lines 158–163 check to see if the plane has hit a tree and, if it has, then set the Boolean variable plane_exploded to True.

Test the game. Now the plane will come to a halt if the player manages to clear all of the trees from the forest, or it will explode if it hits a tree.

Step 8: Game over/level up

If the plane hits a tree, then it is game over.

If the plane reaches the right-hand side of the screen on the ground level, then the game needs to move on to the next level.

In either case, we will display a message to the user to tell them what has happened and invite them to hit the return key to continue.

There are two Boolean variables which will have been set depending on what has happened previously:
  • plane_exploded will have been set to True if the plane has hit a tree.

  • level_cleared will have been set to True if the player has cleared all of the trees from the forest.

We will use these variables to determine what to do next.

Add code listing 16 (Figure 4-9) at line 104.
../images/482396_1_En_4_Chapter/482396_1_En_4_Fig9_HTML.jpg
Figure 4-9

Forest Bomber code listing 16

Notice the use of the keyword elif at line 105 (Figure 4-10).
../images/482396_1_En_4_Chapter/482396_1_En_4_Fig10_HTML.jpg
Figure 4-10

Forest Bomber code listing line 105

It is short for else if and goes with the if statement at line 98. Together, these lines can be read as
  • if the spacebar is pressed

  • drop a bomb

  • else if the return key is pressed

  • start a new level/game

Also notice that the way in which we test if the return key is pressed is just the same as the way in which we tested if the space key was pressed.

Looking at the first part of line 108 (Figure 4-11), we are checking to see if the plane has exploded.
../images/482396_1_En_4_Chapter/482396_1_En_4_Fig11_HTML.jpg
Figure 4-11

Forest Bomber code listing line 108 (first part)

If the plane has exploded, then we know it’s game over. Lines 109–117 reset all the variables back to their start-of-game values.

There is a second part to line 108 (Figure 4-12).
../images/482396_1_En_4_Chapter/482396_1_En_4_Fig12_HTML.jpg
Figure 4-12

Forest Bomber code listing line 108 (second part)

This checks to see if we have cleared the level and also if the current level is equal to TOTAL_LEVELS , which is 4. In other words, if we have cleared level 4, we go back to the beginning of the game and start again at level 1.

Now that we have dealt with the game over functionality, let’s add the functionality to move up a level. Add code listing 17 (Figure 4-13) at line 119 (making sure to check the indentation very carefully).
../images/482396_1_En_4_Chapter/482396_1_En_4_Fig13_HTML.jpg
Figure 4-13

Forest Bomber code listing 17

Line 120 is another elif statement, this time paired with the if statement at line 108. Together they can be read like this:
  • if the plane has exploded and it’s game over

    restart the game

    else if level has been cleared

    level up

Let’s take a closer look at some of the code used to level up (Figures 4-14, 4-15 and 4-16).
../images/482396_1_En_4_Chapter/482396_1_En_4_Fig14_HTML.jpg
Figure 4-14

Forest Bomber code listing line 120

  • Line 120 checks to see if the level has been cleared. If it has

  • Line 121 adds 1 onto the level variable.
    ../images/482396_1_En_4_Chapter/482396_1_En_4_Fig15_HTML.jpg
    Figure 4-15

    Forest Bomber code listing line 121

  • Line 122 sets the level_cleared to False so that the game can start the next level.
    ../images/482396_1_En_4_Chapter/482396_1_En_4_Fig16_HTML.jpg
    Figure 4-16

    Forest Bomber code listing line 122

  • Lines 124–131 load the forest for the new level into the forest variable. Also note that the speed_boost variable is set to 1 for levels 3 and 4 to make the plane fly faster, as discussed in Chapter 3.

  • Finally, lines 133 and 134 reset the x and y coordinates of the plane so that it restarts at the top left of the game screen.

Key learning

The elif statement is used along with an if statement and means else if.

To add a value onto a variable, use +=.

Now test the game:
  • When the plane crashes, the user can press the return key to begin a new game.

  • When all the trees are cleared, the user can press return to move onto the next level.

All that’s missing now is a scoreboard and some feedback for the user at the end of each level and the end of the game.

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

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