Table of Contents
Step 019 - Homing missiles
Bullets have a short range, but steal hitpoints if hitting a player or a monster.
There are 2 different kind of rockets:
- a “light”, weak damage, fast flying variant. Short lifetime. Can be shot down.
- a “heavy”, heavy damage, slow flying “sliding” variant. Longer lifetime. Can not be shot down, but it's flightpath can be disturbed if hit.
Both types of rockets aim automatically toward the target. While the “light” rockets move in the direction of their heading (boring), the “heavy” rockets “slide” like in open space, making more interesting flight patterns.
A player can store a maximum ammo of 32 rockets (see class Player, self.rocketsmax). The first 16 rockets are light rockets, the others are heavy rockets.
Not shooting increase the stock of missiles of each player (see the blue rocketbar above each player)
If no more monsters are present, the players can fight each other (duel mode). New monsters can be created my pressing M.
If both players are shot down by monsters, the monsters start fighting themselves (Overtime). You can press O to get more overtime and watch the monsters fighting each other.
Each 15 seconds, (see class Monster, self.hunttime) the monster has a chance to select a new target.
Each monster has a primitive State_machine, cycling between the three states “do nothing”, “shoot bullets at target” and “shoot rockets at target”. Each “state” is exactly 3 seconds long. This is the code line to see in what “state” or “phase” a monster currently is (inside class Monster, def update):
self.phase = self.phases[int(self.alivetime) % 3 ]
As you can see in this example, the remainder cycles between 0 , 1 and 2 if the divisor is 3:
for x in range(31): # python2.x print x, "divided by 3 =", x/3, "remainder:", x%3,"float result:", x/3.0
0 divided by 3 = 0 remainder: 0 float result: 0.0 1 divided by 3 = 0 remainder: 1 float result: 0.333333333333 2 divided by 3 = 0 remainder: 2 float result: 0.666666666667 3 divided by 3 = 1 remainder: 0 float result: 1.0 4 divided by 3 = 1 remainder: 1 float result: 1.33333333333 5 divided by 3 = 1 remainder: 2 float result: 1.66666666667 ...
source code on github
To run this example you need:
|019_homing_missiles.py|| || Download the whole Archive with all files from Github:
View/Edit/Download the file directly in Github: https://github.com/horstjens/ThePythonGameBook/blob/master/pygame/019_homing_missiles.py
click reload in your browser if you see no code here: