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

10. Alien Invasion part 2: Missile launch is Go

Mark Cunningham1 
(1)
Edinburgh, Scotland
 

Commander Fisher stood inside Hangar 21, watching intently as her team of mechanics made the final adjustments to the MM-44 Missile Launch Vehicle (MLV). Her crew had assembled and were awaiting further instruction.

‘Get the MLV started Corporal Garcia,’ Fisher shouted above the noise that filled the hangar. ‘This time we go for real.’

The crew boarded the MLV, the engine burst into life, and Corporal Garcia accelerated through the hangar doors and into the Arizona night.

In this chapter, we will complete the following steps from our game design:
  1. 4.

    Drive the base.

     
  2. 5.

    Launch missile.

     

Step 4: Drive the base

The Missile Launch Vehicle is referred to as the base in the game code. The base will move left or right, and movement will be controlled by the arrow keys.
../images/482396_1_En_10_Chapter/482396_1_En_10_Fig1_HTML.jpg
Figure 10-1

Alien Invasion code listing 5

Add the code shown in Figure 10-1 at line 112. We have already seen how Pygame stores a list of key press events which can be retrieved by using pygame.key.get_pressed(). As before, we store the key press events in our variable key_pressed.

Looking at this code block
  • Line 113 tests to see if the left arrow key was pressed.

  • If the left arrow was pressed, line 114 takes the BASE_SPEED constant away from the base’s x coordinate. Remember BASE_SPEED is the number of pixels that the base will move in one frame (in our game, BASE_SPEED is set to 6). Also remember we use -= to subtract a value from a variable.

  • Line 115 tests to see if the base’s x coordinate base_x has gone below 0. If base_x is less than 0, this means the base will have moved off the left-hand edge of the screen. This is not good.

  • To resolve this issue, if base_x is less than 0, line 116 sets base_x to 0. This means that the base can never go beyond the left-hand side of the screen.

Lines 119–122 do virtually the same thing as in the preceding text, but instead move the base right instead of left:
  • Line 119 tests to see if the right arrow key was pressed.

  • If it was, line 120 adds BASE_SPEED onto base_x.

  • Line 121 tests to see if the base has gone beyond the right-hand edge of the screen.

  • If the base has gone beyond the right-hand edge of the screen, line 122 sets the base_x coordinate so that the base is flush with the right-hand screen edge.

Of course we can’t see anything happening yet because we have not drawn the base on the screen. Let’s do that now. Add the code shown in Figure 10-2.
../images/482396_1_En_10_Chapter/482396_1_En_10_Fig2_HTML.jpg
Figure 10-2

Alien Invasion code listing 6

Now run Alien Invasion. The base will appear at the foot of the screen and can be moved left or right using the arrow keys.

Comparison operators

In Python, we can compare two values using comparison operators . We have already encountered some comparison operators in this book. For example, the code which tests if the base has gone beyond the left-hand side of the screen uses the less than (<) comparison operator, while the code which checks if the base has gone beyond the right-hand side of the screen uses the greater than (>) comparison operator.

This is a good opportunity to review all of the comparison operators that can be used in Python (Table 10-1).
Table 10-1

Comparison operators

Comparison Operator

Definition

==

Is equal to

!=

Is not equal to

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

Step 5: Launch missile

Now that the player is able to drive the base, the next step is to write the code to launch a missile. In the game, a missile will be launched when the spacebar is pressed. Once a missile has been launched, it should not be possible to launch a second missile until the first missile has either disappeared off the top of the screen or hit a UFO. Enter the code shown in Figure 10-3.
../images/482396_1_En_10_Chapter/482396_1_En_10_Fig3_HTML.jpg
Figure 10-3

Alien Invasion code listing 7

Let’s go through this code block line by line:
  • Line 125 uses an elif command as an extension of the key_pressed if statement. This time we are testing to see if the key pressed was SPACE. Let’s look at the line in detail. There are three separate conditions which all must be met in order for a missile to be fired:
    • The SPACE key has been pressed.

    • missile_firing is False, which means a missile has not already been launched.

    • game_over is False, which means the game is not over.

If all three of these conditions are met, we fire a missile with the code between lines 126 and 132:
  • Line 126: Sets the Boolean variable missile_firing to True (this will prevent any other missiles being fired at the same time).

  • Line 127: Sets the x coordinate of the missile to start a few pixels to the right of the base_x coordinate (we add on MISSILE_PLATFORM so that the missile launch location is in line with the missile platform).

  • Line 128: Sets the y coordinate of the missile so that its starting position is on top of the base.

  • Line 129: Reduces the number of missiles in the game by 1.

  • Line 130: Plays a fire missile sound effect.

  • Lines 131 and 132: Test if the number of missiles has dropped to zero and if so set the game_over Boolean variable to True.

Move the missile

Once the missile has been launched, we need to keep its y coordinate changing; otherwise, it will not actually move. We subtract MISSILE_SPEED from the missile’s y coordinate (missile_y).

The code in Figure 10-4 is the code that will do that.
../images/482396_1_En_10_Chapter/482396_1_En_10_Fig4_HTML.jpg
Figure 10-4

Alien Invasion code listing 8

  • At line 140, we test if the missile_firing variable is True . If it is True, then we know that a missile has been launched and so needs to be moved. The code between lines 141 and 143 will move the missile.

  • Line 141 moves the missile upward by the number of pixels held in the constant MISSILE_SPEED.

  • Line 142 checks to see if the missile’s y coordinate is less than zero. If missile_y < 0, the missile has gone off the top edge of the screen. If that happens, we set missile_firing to False so that the player can launch a new missile.

Display missile

Finally, in order to see the effect of the missile being fired, we need to draw it on the screen. Add code listing 9 (Figure 10-5) to your program.
../images/482396_1_En_10_Chapter/482396_1_En_10_Fig5_HTML.jpg
Figure 10-5

Alien Invasion code listing 9

There are actually two missile images:
  • The first, missile_fired_image, is drawn when missile_firing is True and the missile has been launched.

  • The second, missile_image, is drawn when missile_firing is False and the missile is sitting on the top of the base waiting to be launched.

Logical operators

Logical operators are used with conditions (if statements, while loops) to build more complex conditions.

There are three logical operators:
  • and

  • or

  • not

Let’s look at a simple if condition for a moment, one that we used back in the Snapper program:
if lives == 0:
    display_game_over()

In this example, lives == 0 is known as an operand. An operand will evaluate to either true (if lives equal 0) or false (if lives are not equal to 0).

We can use logical operators to join more than one operand to make a complex condition.

For example, we used the and logical operator when deciding whether or not to launch a missile. Look back to line 125, the elif statement
elif key_pressed[pygame.K_SPACE] and
missile_firing is False and
game_over is False:
The and logical operator is used here to ensure that each of the three operands evaluates to true before launching the missile. The missile can only be launched if
  • The space key has been pressed:

    key_pressed[pygame.K_SPACE]

  • The missile has not already been fired:

    missile_firing is False

  • The game is not over:

    game_over is False

Table 10-2 shows each of the three logical operators.
Table 10-2

Logical operators

Operator

Definition

Example

and

True if both operands evaluate to true

a and b

or

True if either of the operands evaluates to true

a or b

not

True if the operand evaluates to false

not a

We will see an example of the or logical operator in the next chapter.

But for now, let’s get back to Alien Invasion. We have a missile launcher which we can move left and right, and we can fire missiles, but there don’t seem to be any aliens invading planet earth. At least not yet anyway…

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

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