2
homing_missle - contains the HomingMissle class, a sample sublcass
8
from python_name.base_sprite import BaseSprite
10
from python_name import python_nameconfig
12
class HomingMissle(BaseSprite):
14
HomingMissle - A useful sample class that derives from BaseSprite and
15
has some very simple AI.
19
def __init__(self, target, img):
21
Create a HomingMissle. Useful as an emeny or a as weapon
25
target - A sprite that the homing missle atempts to track down
28
img - A path to an image to use for the missle
32
BaseSprite.__init__(self, img)
35
self._rotating_left = False
36
self._rotating_right = False
38
#the missle starts out moving
39
self._accelerating = True
41
#the missle starts out not knowing if it is
42
#pointed at the target
43
self.targetted = False
44
self.explode_stage = 0
45
self.exploding = False
46
self.explosion_sound = pygame.mixer.Sound(project_nameconfig.enemy_explode_sound)
50
"""update - update internal data and position.
51
Typically called by the game controller each game
56
BaseSprite.update(self)
58
#do an explosion image for each tick
59
self.explode_stage += 1
60
e = self.explode_stage
63
img_name = project_nameconfig.enemy_explode_stage + e + ".png"
64
self.master_image = pygame.image.load(img_name)
68
else:#explosion is done
70
self.exploding = False
74
self.targetted = False
75
#calculate target angle
76
targx = self.target.x - self.x
77
targy = self.target.y - self.y
78
target_angle = ((math.atan2(targy,targx)) * 180)/ math.pi
80
target_angle = 360 + target_angle
82
#translate the orientation to the
83
# same system as the target angle
84
fire_angle = 360 - self.orientation
91
#rotate to face the target
92
delta = fire_angle - target_angle
94
self._rotating_left = True
95
self._rotating_right = False
97
self._rotating_left = False
98
self._rotating_right = True
100
self._rotating_left = False
101
self._rotating_right = False
102
self.targetted = True
105
"""explode - called when the homing missle is destroyed in
106
game play. Sets the alive flag to False, and sets a flag to
107
start animating an explosion in the update function. Also
108
plays the explosion sound.
113
self.explosion_sound.play()
115
self.exploding = True