-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectiles.py
262 lines (213 loc) · 8.28 KB
/
projectiles.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import math
import pygame
SPRITE_POOL = {}
class RedLaser(pygame.sprite.Sprite):
"""
- Player_Projectile -
Base class for all player projectiles
"""
def __init__(self, main_game, offset_x=0, offset_y=0):
super().__init__()
self.main_game = main_game
self.image = pygame.image.load("images/weapons/Red_Laser.png")
self.rect = self.image.get_rect()
self.rect.center = self.main_game.player1.rect.center
self.offset_x = offset_x
self.offset_y = offset_y
self.rect.move_ip(self.offset_x, self.offset_y)
self.speed = 10
self.main_game.projectile_group.add(self)
self.main_game.all_sprites_group.add(self)
self.strength = 1
self.is_dead = False
def reset(self, offset_x=0, offset_y=0):
self.is_dead = False
self.rect = self.image.get_rect()
self.offset_x = offset_x
self.offset_y = offset_y
self.rect.center = self.main_game.player1.rect.center
self.rect.move_ip(self.offset_x, self.offset_y)
def move(self):
if self.is_dead:
return
self.rect.move_ip(0, self.speed * -1)
if self.rect.bottom < 0:
self.die()
def die(self):
self.is_dead = True
self.rect.center = (0, self.main_game.screen_height * -1)
class SpreadLaser(pygame.sprite.Sprite):
"""
- Player_Projectile -
Base class for all player projectiles
"""
def __init__(self, main_game, spread=0.0):
super().__init__()
self.main_game = main_game
self.image = pygame.image.load("images/weapons/Green_Laser.png")
self.rect = self.image.get_rect()
self.rect.center = self.main_game.player1.rect.center
self.spread = spread
self.speed = 10
self.main_game.projectile_group.add(self)
self.main_game.all_sprites_group.add(self)
self.strength = 1
self.is_dead = False
def reset(self, spread=0.0):
self.is_dead = False
self.spread = spread
self.rect = self.image.get_rect()
self.rect.center = self.main_game.player1.rect.center
def move(self):
if self.is_dead:
return
self.rect.move_ip(self.speed * self.spread, self.speed * -1)
if self.rect.bottom < 0:
self.die()
def die(self):
self.is_dead = True
self.rect.center = (0, self.main_game.screen_height * -1)
class TrailDrop(pygame.sprite.Sprite):
def __init__(self, main_game, parent):
super().__init__()
self.image = pygame.image.load("images/weapons/Trail.png")
self.rect = self.image.get_rect()
self.rect.center = (parent.rect.center[0], parent.rect.center[1])
self.life_span = 300
self.spawned_time = pygame.time.get_ticks()
self.is_dead = False
self.main_game = main_game
def die(self):
self.is_dead = True
self.rect.center = (0, self.main_game.screen_height * -1)
def reset(self, parent):
self.is_dead = False
self.spawned_time = pygame.time.get_ticks()
self.rect = self.image.get_rect()
self.rect.center = parent.rect.center
def update(self):
if (pygame.time.get_ticks() - self.spawned_time) > self.life_span:
self.die()
def move(self):
if self.is_dead:
return
class HomingMissile(pygame.sprite.Sprite):
"""
- Player_Projectile -
Base class for all player projectiles
"""
def __init__(self, main_game, offset_x=0, offset_y=0):
super().__init__()
self.time_since_launch = 0
self.missile_pos = pygame.math.Vector2(0, 0)
self.main_game = main_game
self.offset_x = offset_x
self.offset_y = offset_y
self.image = pygame.image.load("images/weapons/Missile.png")
self.rect = self.image.get_rect()
self.rect.center = self.main_game.player1.rect.center
self.rect.move_ip(self.offset_x, self.offset_y)
self.speed = 15
self.main_game.projectile_group.add(self)
self.main_game.all_sprites_group.add(self)
self.strength = 1
self.is_dead = False
self.attraction_distance = 200
self.locked_onto = None
self.locked_on = False
self.velocity = pygame.math.Vector2(0, -50)
self.launched_time = pygame.time.get_ticks()
self.damping_constant = 0.2
self.attraction_force = 0.2
self.lifetime = 2000
self.dropped_trail = pygame.time.get_ticks()
def reset(self, offset_x=0, offset_y=0):
#self.image = pygame.image.load("images/weapons/Missile.png")
self.offset_x = offset_x
self.offset_y = offset_y
self.launched_time = pygame.time.get_ticks()
self.time_since_launch = 0
self.velocity = pygame.math.Vector2(0, -50)
self.locked_onto = None
self.locked_on = False
self.is_dead = False
self.rect = self.image.get_rect()
self.rect.center = self.main_game.player1.rect.center
self.rect.move_ip(self.offset_x, self.offset_y)
self.strength = 1
def update(self):
current_time = pygame.time.get_ticks()
if (current_time - self.dropped_trail) > 10:
self.make_trail()
self.dropped_trail = current_time
def make_trail(self):
global SPRITE_POOL
if "MissileTrail" not in SPRITE_POOL:
SPRITE_POOL["MissileTrail"] = []
trail_pool = SPRITE_POOL["MissileTrail"]
# If there's a projectile we can use in the pool, try to use that
if len(trail_pool) > 0:
for p in trail_pool:
# Look for one that's dead so you don't end up using a live one
if p.is_dead:
p.reset(self)
return True
# If there's no
new_projectile = TrailDrop(self.main_game, self)
self.main_game.projectile_group.add(new_projectile)
self.main_game.all_sprites_group.add(new_projectile)
trail_pool.append(new_projectile)
def find_enemy(self):
self.time_since_launch = (pygame.time.get_ticks() - self.launched_time)
if self.time_since_launch > self.lifetime:
self.die()
for enemy in self.main_game.enemy_group:
enemy_pos = pygame.math.Vector2(enemy.rect.center)
player_pos = pygame.math.Vector2(self.main_game.player1.rect.center)
if enemy_pos.y > player_pos.y:
self.locked_on = False
continue
if enemy.is_dead:
self.locked_on = False
continue
if pygame.math.Vector2(enemy.rect.center).y > self.main_game.screen_height:
self.locked_on = False
continue
missile_pos = pygame.math.Vector2(self.rect.center)
delta = enemy_pos - missile_pos
if delta.length() < self.attraction_distance:
self.locked_onto = enemy
self.locked_on = True
break
def move(self):
if self.is_dead:
return
if self.locked_on:
if self.locked_onto.is_dead:
self.locked_on = False
self.locked_onto = None
# if not self.locked_on:
self.find_enemy()
self.missile_pos = pygame.math.Vector2(self.rect.center)
if self.time_since_launch < 100:
return
if self.locked_on:
enemy_pos = pygame.math.Vector2(self.locked_onto.rect.center)
delta = enemy_pos - self.missile_pos
if delta == 0:
self.die()
return
delta.normalize()
force = pygame.math.Vector2(delta) * self.attraction_force
force -= self.velocity * self.damping_constant
self.velocity += force * 0.17
angle = math.atan2(self.velocity.x, self.velocity.y) * (180 / math.pi)
self.image = pygame.image.load("images/weapons/Missile.png")
self.image = pygame.transform.rotate(self.image, angle)
self.missile_pos += self.velocity * 0.17
self.rect.center = (self.missile_pos.x, self.missile_pos.y)
if self.rect.bottom < 0:
self.die()
def die(self):
self.is_dead = True
self.rect.center = (0, self.main_game.screen_height * -1)