1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
import pygame, random
from baseSprite import BaseSprite
import math
import crashteroidsconfig
class HomingMissle(BaseSprite):
def __init__(self, target, img):
BaseSprite.__init__(self, img)
self.init_position()
self.target = target
self._rotating_left = False
self._rotating_right = False
self._accelerating = True
self.targetted = False
self.explodestage = 0
self.exploding = False
self.alive = True
self.explosionSound = pygame.mixer.Sound(crashteroidsconfig.enemy_explode_sound)
self.explosionSound.set_volume(.2)
self.points = 1
def update(self):
BaseSprite.update(self)
if self.exploding:
#do an explosion image for each tick
self.explodestage += 1
e = self.explodestage
if e < 8:#there are 7 explosion images
e = str(e)
self.master_image = pygame.image.load(crashteroidsconfig.enemy_explode_stage + e + ".png")
self.update_image()
return
else:#explosion is done
self.visible = False
self.exploding = False
self.kill()
return
self.targetted = False
#calculate target angle
targx = self.target.x - self.x
targy = self.target.y - self.y
target_angle = ((math.atan2(targy,targx)) * 180)/ math.pi
if target_angle < 0:
target_angle = 360 + target_angle
#translate the orientation to the same system as the target angle
fire_angle = 360 - self.orientation
if fire_angle == 360:
fire_angle = 0
fire_angle += 270
if fire_angle >= 360:
fire_angle -= 360
#rotate to face the target
delta = fire_angle - target_angle
if delta > 5:
self._rotating_left = True
self._rotating_right = False
elif delta < -5:
self._rotating_left = False
self._rotating_right = True
else:
self._rotating_left = False
self._rotating_right = False
self.targetted = True
def explode(self):
if self.alive:
self.explosionSound.play()
self.alive = False
self.exploding = True
def init_position(self):
#leave room in the center for the guy
quad = random.randint(0,3)
if quad == 0:
self.x = random.randint(0,300)
self.y = random.randint(0,140)
if quad == 1:
self.x = random.randint(500,800)
self.y = random.randint(0,140)
if quad == 2:
self.x = random.randint(0,200)
self.y = random.randint(340,480)
if quad == 3:
self.x = random.randint(500,800)
self.y = random.randint(340,480)
|