-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.py
389 lines (310 loc) · 14.8 KB
/
enemy.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import pygame
import math
import random
from stgs import *
from animations import *
from player import *
from objects import *
import fx
import colors
class enemy(pygame.sprite.Sprite):
pos = pygame.Vector2((0, 0))
moveType = 1
health = 20
damage = 5
points = 5
color = False
imgSheet = {'tileWidth': 32 ,'r': pygame.surface.Surface((32,32))}
vel = 5
startDir = (0, 0)
def __init__(self, game, startPos, **kwargs):
self.groups = game.sprites, game.enemies, game.layer2
self.game = game
for k, v in kwargs.items():
self.__dict__[k] = v
pygame.sprite.Sprite.__init__(self, self.groups)
self.loadAnimations()
self.width = self.imgSheet['tileWidth']
self.height = self.imgSheet['tileWidth']
self.pos = pygame.Vector2((startPos[0], startPos[1]))
self.rect = pygame.Rect(self.pos.x, self.pos.y, self.imgSheet['tileWidth'], self.imgSheet['tileWidth'])
self.dir = pygame.Vector2(self.startDir)
def loadAnimations(self):
self.animations = animation(self)
def update(self):
self.move()
self.rect = pygame.Rect(self.pos.x, self.pos.y, self.width, self.height)
if self.health <= 0:
self.kill()
self.animations.update()
## Vertical or horizontal movement
def move(self):
testVec = pygame.Vector2((self.pos.x, self.pos.y))
if self.collideCheck(testVec + (self.dir*self.vel)):
if not self.dir.x == 0:
self.dir = pygame.Vector2((-self.dir.x, 0))
elif not self.dir.y == 0:
self.dir = pygame.Vector2((0, -self.dir.y))
self.pos += self.dir *self.vel
def collideCheck(self, vector):
returnVal = False
testRect = pygame.Rect(round(vector.x), round(vector.y), self.rect.width, self.rect.height)
for obj in self.game.colliders:
if testRect.colliderect(obj.rect):
returnVal = True
return returnVal
class bit01(enemy):
def __init__(self, game, pos, vertical):
imgSheet = {'tileWidth': 32, 'r': asset('enemies/bit01.png')}
if vertical:
startDir = (0, random.randrange(-1, 1+1, 2))
else:
startDir = (random.randrange(-1, 1+1, 2), 0)
super().__init__(game, pos, health = 2, imgSheet = imgSheet, startDir = startDir)
def collideCheck(self, vector):
returnVal = False
testRect = pygame.Rect(round(vector.x), round(vector.y), self.rect.width, self.rect.height)
for obj in self.game.colliders:
if not isinstance(obj, (turret1, mPlatform)):
if testRect.colliderect(obj.rect):
returnVal = True
return returnVal
def kill(self):
fx.particles(self.game, self.rect, lifeSpan = 400, tickSpeed=2, size = 12).setParticleKwargs(speed=1.5, shrink=0.4, life=140, color=colors.lightGreen)
super().kill()
class turret1(enemy):
# Okay PLEASE READ THIS WHEN DEALING WITH THIS SPRITE:
# The Turret bases its fire direction based on its animation frame
# It is important you define both directions in the animation sheet or one side will invert and it will confuse it all up
# Pleas be careful when aligning the animation with the firing - Luke Mistake 2021
def __init__(self, game, pos, vertical):
imgSheet = {'tileWidth': 64, 'r': asset('enemies/Turret1.png'), 'l': asset('enemies/Turret1.png')} # Animation Sheet
if vertical:
startDir = (0, random.randrange(-1, 1+1, 2))
else:
startDir = (random.randrange(-1, 1+1, 2), 0)
super().__init__(game, pos, health = 8, imgSheet = imgSheet, startDir = startDir, groups = (game.sprites, game.enemies, game.layer2, game.colliders))
self.animations.delay = 90*6
self.fireAngle = 90
self.fireDelay = 90*4
self.lastFire = -self.fireDelay
self.animations.framex += random.randint(0,8)*64
self.rect.width = 40
self.rect.height = 40
def update(self):
super().update()
self.fireAngle = ((self.animations.framex/self.animations.tileWidth)/8)*360
if pygame.time.get_ticks() - self.lastFire >= self.fireDelay:
self.fire()
self.lastFire = pygame.time.get_ticks()
def fire(self):
angleVector = pygame.Vector2((1, 0)).rotate(self.fireAngle)
buls = [enemyBullet(self.game, self.rect.center, angleVector, -self.fireAngle),
enemyBullet(self.game, self.rect.center, angleVector, -self.fireAngle, offset = 4),
enemyBullet(self.game, self.rect.center, angleVector, -self.fireAngle, offset = -4)]
for bul in buls:
while bul.rect.colliderect(self.rect):
bul.move()
def move(self):
self.player = self.game.player
testVec = pygame.Vector2((self.pos.x, self.pos.y))
if self.collideCheck(pygame.Vector2(testVec.x+(self.dir.x*self.vel), testVec.y)):
if self.dir.x > 0:
self.dir = self.dir.reflect((-1,0))
else:
self.dir = self.dir.reflect((1,0))
self.pos.x += self.dir.x*self.vel
# If we hit player move the player
testRect = pygame.Rect(self.pos.x, self.pos.y, self.rect.width, self.rect.height)
if testRect.colliderect(self.player.rect):
if self.dir.x < 0:
self.player.rect.right = testRect.left
else:
self.player.rect.left = testRect.right
if self.collideCheck(pygame.Vector2(testVec.x, testVec.y+(self.dir.y*self.vel))):
if self.dir.y > 0:
self.dir = self.dir.reflect((0, -1))
else:
self.dir = self.dir.reflect((0, 1))
self.pos.y += self.dir.y*self.vel
testRect = pygame.Rect(self.pos.x, self.pos.y, self.rect.width, self.rect.height)
if testRect.colliderect(self.player.rect):
if self.dir.y < 0:
self.player.rect.bottom = self.rect.top
else:
self.player.rect.top = self.rect.bottom
def collideCheck(self, vector):
returnVal = False
testRect = pygame.Rect(round(vector.x), round(vector.y), self.rect.width, self.rect.height)
for obj in self.game.colliders:
if not obj == self:
if testRect.colliderect(obj.rect):
returnVal = True
for obj in self.game.level.platWalls:
if testRect.colliderect(obj.rect):
returnVal = True
return returnVal
def kill(self):
fx.particles(self.game, self.rect, lifeSpan = 400, tickSpeed=2, size = 14).setParticleKwargs(speed=1.5, shrink=0.4, life=140, color=colors.red)
super().kill()
class megaTurret(enemy):
# Okay PLEASE READ THIS WHEN DEALING WITH THIS SPRITE:
# The Turret bases its fire direction based on its animation frame
# It is important you define both directions in the animation sheet or one side will invert and it will confuse it all up
# Pleas be careful when aligning the animation with the firing - Luke Mistake 20201
def __init__(self, game, pos, vertical):
imgSheet = {'tileWidth': 128, 'r': asset('enemies/megaTurret.png'), 'l': asset('enemies/megaTurret.png')} # Animation Sheet
startDir = pygame.Vector2(1, 0).rotate(25)
super().__init__(game, pos, health = 8, imgSheet = imgSheet, startDir = startDir, groups = (game.sprites, game.enemies, game.layer2, ))#game.colliders))
self.animations.delay = 90*6
self.animations.frameReset = False
self.fireAngle = 90
self.fireDelay = 90*4
self.lastFire = -self.fireDelay
self.animations.framex += random.randint(0,8)*128
self.width = 100
self.height = 100
self.vel = 10
def update(self):
super().update()
self.fireAngle = ((self.animations.framex/self.animations.tileWidth)/8)*360 - 90
if pygame.time.get_ticks() - self.lastFire >= self.fireDelay:
self.fire()
self.lastFire = pygame.time.get_ticks()
def fire(self):
angleVector = pygame.Vector2((1, 0)).rotate(self.fireAngle)
buls = [enemyBullet(self.game, self.rect.center, angleVector, -self.fireAngle),
enemyBullet(self.game, self.rect.center, angleVector, -self.fireAngle, offset = 4),
enemyBullet(self.game, self.rect.center, angleVector, -self.fireAngle, offset = -4)]
for bul in buls:
while bul.rect.colliderect(self.rect):
bul.move()
def move(self):
self.player = self.game.player
testVec = pygame.Vector2((self.pos.x, self.pos.y))
if self.collideCheck(pygame.Vector2(testVec.x+(self.dir.x*self.vel), testVec.y)):
if self.dir.x > 0:
self.dir = self.dir.reflect((-1,0))
else:
self.dir = self.dir.reflect((1,0))
self.pos.x += self.dir.x*self.vel
# If we hit player move the player
# testRect = pygame.Rect(self.pos.x, self.pos.y, self.rect.width, self.rect.height)
# if testRect.colliderect(self.player.rect):
# if self.dir.x < 0:
# self.player.rect.right = testRect.left
# else:
# self.player.rect.left = testRect.right
if self.collideCheck(pygame.Vector2(testVec.x, testVec.y+(self.dir.y*self.vel))):
if self.dir.y > 0:
self.dir = self.dir.reflect((0, -1))
else:
self.dir = self.dir.reflect((0, 1))
self.pos.y += self.dir.y*self.vel
# And again move the player on collision
# testRect = pygame.Rect(self.pos.x, self.pos.y, self.rect.width, self.rect.height)
# if testRect.colliderect(self.player.rect):
# if self.dir.y < 0:
# self.player.rect.bottom = self.rect.top
# else:
# self.player.rect.top = self.rect.bottom
def collideCheck(self, vector):
returnVal = False
testRect = pygame.Rect(round(vector.x), round(vector.y), self.rect.width, self.rect.height)
for obj in self.game.colliders:
if not obj == self:
if testRect.colliderect(obj.rect):
returnVal = True
return returnVal
class bit02(enemy):
def __init__(self, game, pos):
imgSheet = {'tileWidth': 32, 'r': asset('enemies/bit02.png')}
vel = 10
startDir = pygame.Vector2(1, 0).rotate(random.randint(1, 360))
super().__init__(game, pos, health = 5, imgSheet = imgSheet, vel = vel, startDir = startDir)
self.points = 10
def move(self):
testVec = pygame.Vector2((self.pos.x, self.pos.y))
if self.collideCheck(pygame.Vector2(testVec.x+(self.dir.x*self.vel), testVec.y)):
if self.dir.x > 0:
self.dir = self.dir.reflect((-1,0))
else:
self.dir = self.dir.reflect((1,0))
self.pos.x += self.dir.x*self.vel
if self.collideCheck(pygame.Vector2(testVec.x, testVec.y+(self.dir.y*self.vel))):
if self.dir.y > 0:
self.dir = self.dir.reflect((0, -1))
else:
self.dir = self.dir.reflect((0, 1))
self.pos.y += self.dir.y*self.vel
def collideCheck(self, vector):
returnVal = False
testRect = pygame.Rect(round(vector.x), round(vector.y), self.rect.width, self.rect.height)
for obj in self.game.colliders:
if not isinstance(obj, (turret1, mPlatform)):
if testRect.colliderect(obj.rect):
returnVal = True
return returnVal
def kill(self):
fx.particles(self.game, self.rect, lifeSpan = 400, tickSpeed=2, size = 14).setParticleKwargs(speed=1.5, shrink=0.4, life=140, color=colors.red)
super().kill()
class bossyBit(enemy):
def __init__(self, game, pos):
imgSheet = {'tileWidth': 128, 'r': asset('enemies/bossyBit.png')}
vel = 5
startDir = pygame.Vector2(1, 0)
super().__init__(game, pos, health = 30, imgSheet = imgSheet, vel = vel, startDir = startDir)
self.reloadTime = 1000
self.lastFire = pygame.time.get_ticks()
self.points = 30
def move(self):
testVec = pygame.Vector2((self.pos.x, self.pos.y))
if self.collideCheck(pygame.Vector2(testVec.x+(self.dir.x*self.vel), testVec.y)):
if self.dir.x > 0:
self.dir = self.dir.reflect((-1,0))
else:
self.dir = self.dir.reflect((1,0))
self.pos.x += self.dir.x*self.vel
if self.collideCheck(pygame.Vector2(testVec.x, testVec.y+(self.dir.y*self.vel))):
if self.dir.y > 0:
self.dir = self.dir.reflect((0, -1))
else:
self.dir = self.dir.reflect((0, 1))
self.pos.y += self.dir.y*self.vel
def update(self):
super().update()
self.attack()
def attack(self):
if pygame.time.get_ticks() - self.lastFire >= self.reloadTime:
pPos = pygame.Vector2(self.game.cam.apply(self.game.player).center)
myPos = self.game.cam.apply(self)
pPos.x -= myPos.centerx
pPos.y -= myPos.centery
angle = math.degrees(math.atan2(-pPos.normalize().y, pPos.normalize().x))
enemyBullet(self.game, self.rect.center, pPos, angle, offset = 5)
enemyBullet(self.game, self.rect.center, pPos, angle)
enemyBullet(self.game, self.rect.center, pPos, angle, offset = -5)
self.lastFire = pygame.time.get_ticks()
class enemyBullet(pygame.sprite.Sprite):
pos = pygame.Vector2((0,0))
image = pygame.image.load(asset('objects/bullet2.png'))
vel = 20
offset = 0
damage = 5
friendly = False
def __init__(self, game, pos, target, angle, **kwargs):
self.groups = game.sprites, game.eBullets, game.layer2
pygame.sprite.Sprite.__init__(self, self.groups)
for k, v in kwargs.items():
self.__dict__[k] = v
self.pos = pygame.Vector2(pos)
self.dir = pygame.Vector2(target).normalize()
self.dir = self.dir.rotate(self.offset)
self.image = pygame.transform.rotate(self.image, angle - self.offset)
self.rect = pygame.Rect(0, 0, self.image.get_width(), self.image.get_height())
self.rect.center = self.pos
def update(self):
self.move()
def move(self):
self.pos += self.dir *self.vel
self.rect.center = self.pos