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.
- 4.
Drive the base.
- 5.
Launch missile.
Step 4: Drive the base
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.
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.
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.
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.
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
- 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.
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).
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
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.
and
or
not
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.
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
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…