-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameEnemies.py
More file actions
342 lines (293 loc) · 10.9 KB
/
GameEnemies.py
File metadata and controls
342 lines (293 loc) · 10.9 KB
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
import pygame, sys, random
from pygame.locals import *
from VectorClass import Vector
class Enemy(pygame.sprite.Sprite):
def __init__(self, name, x, y, images, health=10, attackDamage=5, sound=None):
'''images: 4 image string tuple
images is (stand, stand2, attack stand, stun stand)'''
pygame.sprite.Sprite.__init__(self)
self.name = name
self.loadImages(images)
self.currentImageNum = 0
self.image = self.i0
self.rect = self.image.get_rect()
self.rect.x=x
self.rect.y=y
self.realWidth = self.rect.width
self.realHeight = self.rect.height
self.playerX = self.rect.x
self.playerY = self.rect.y
self.picDelay = 20
self.picNum = self.picDelay
self.left = False
self.stunned = False
self.stunDelay = 10
self.stunNum = self.stunDelay
self.attacking = False
self.attackDelay = 40
self.attackNum = self.attackDelay
self.attackAni = self.attackDelay/2
self.health=health
self.attackDamage = attackDamage
self.loadSound(sound)
def get_name(self):
return self.name
def get_x(self):
return self.rect.x
def get_y(self):
return self.rect.y
def get_rect(self):
return self.rect
def get_bottom(self):
return self.rect.bottom
def get_top(self):
return self.rect.top
def get_left(self):
return self.rect.left
def get_right(self):
return self.rect.right
def get_width(self):
return self.rect.width
def get_height(self):
return self.rect.height
def get_image(self):
return self.image
def loadSound(self, sound):
if sound != None:
self.sound = pygame.mixer.Sound(sound)
else:
self.sound = None
def loadImages(self, images):
'''images: 4 image string tuple
images is (stand, stand2, attack stand, stun stand)'''
self.i0 = pygame.image.load(images[0]).convert_alpha()
self.i1 = pygame.image.load(images[1]).convert_alpha()
self.i2 = pygame.image.load(images[2]).convert_alpha()
self.i3 = pygame.image.load(images[3]).convert_alpha()
self.images = (self.i0,self.i1,self.i2,self.i3)
def playSound(self):
if self.sound != None:
self.sound.play()
def scaleSprite(self, ScaleVal):
self.image = pygame.transform.scale(self.image, (int(self.realWidth*ScaleVal), int(self.realHeight*ScaleVal)))
imageRect = self.image.get_rect()
imageRect.bottom = self.rect.bottom
imageRect.left = self.rect.left
self.rect = imageRect
def update(self):
self.isStunned()
self.isAttacking()
self.isLeft()
self.chooseImageNum()
self.render()
def render(self):
self.image = self.images[self.currentImageNum]
if self.left:
self.image = pygame.transform.flip(self.image, True, False)
def chooseImageNum(self):
if self.stunned:
self.currentImageNum = 3
elif self.attacking and self.attackNum < self.attackAni:
self.currentImageNum = 2
else:
if self.picNum >= self.picDelay:
self.picNum = 0
if self.currentImageNum == 0:
self.currentImageNum = 1
else:
self.currentImageNum = 0
else:
self.picNum += 1
def inflictDamage(self, damage, knockBack):
self.playSound()
if self.health - damage > 0:
self.health -= damage
self.stunNum = 0
else:
self.kill()
def collPlayer(self):
if self.attacking == False:
self.attackNum = 0
return self.attackDamage
else:
return 0
def isAttacking(self):
if self.attackNum >= self.attackDelay:
self.attacking = False
else:
self.attacking = True
self.attackNum += 1
def isLeft(self):
if self.playerX < self.rect.centerx:
self.left = True
else:
self.left = False
def isStunned(self):
if self.stunNum >= self.stunDelay:
self.stunned = False
else:
self.stunned = True
self.stunNum += 1
def recievePlayerCoords(self, xCoord, yCoord):
self.playerX = xCoord
self.playerY = yCoord
class PlayerChaser(Enemy):
def __init__(self, name, x, y, images, health=10, attackDamage=5, sound=None, moveSpeed=3):
'''images: 5 image string tuple
images is (stand, stand attack, left ft step, right ft step, stun stand)'''
pygame.sprite.Sprite.__init__(self)
self.name = name
self.loadImages(images)
self.currentImageNum = 0
self.image = self.i0
self.rect = self.image.get_rect()
self.rect.x=x
self.rect.y=y
self.trueX = x
self.trueY = y
self.realWidth = self.rect.width
self.realHeight = self.rect.height
self.playerX = self.rect.x
self.playerY = self.rect.y
self.target = (self.playerX, self.playerY)
self.realSpeed = moveSpeed # Real movement speed of the sprite
self.moveSpeed = moveSpeed # movement speed of the sprite
self.left = False
self.stunned = False
self.stunDelay = 10
self.stunNum = self.stunDelay
self.attacking = False
self.attackDelay = 40
self.attackNum = self.attackDelay
self.attackAni = self.attackDelay/2
self.stepDelay = 10
self.stepNum = self.stepDelay
self.health=health
self.attackDamage = attackDamage
self.loadSound(sound)
def loadImages(self, images):
'''images: 5 image string tuple
images is (stand, stand attack, left ft step, right ft step, stun stand)'''
self.i0 = pygame.image.load(images[0]).convert_alpha()
self.i1 = pygame.image.load(images[1]).convert_alpha()
self.i2 = pygame.image.load(images[2]).convert_alpha()
self.i3 = pygame.image.load(images[3]).convert_alpha()
self.i4 = pygame.image.load(images[4]).convert_alpha()
self.images = (self.i0,self.i1,self.i2,self.i3,self.i4)
def update(self):
self.isStunned()
self.isAttacking()
self.moveSprite()
self.chooseImageNum()
self.render()
def direction(self, target):
'''
Function:
takes total distance from sprite
to the sprites target
(gets direction to move)
Returns:
a normalized vector
Parameters:
- self
- target
x,y coordinates of the sprites target
can be any x,y coorinate pair in
brackets [x,y]
or parentheses (x,y)
'''
if self.target: # if the sprite has a target
position = Vector(self.rect.x, self.rect.y) # create a vector from x,y value
target = Vector(target[0], target[1]) # and one from the target x,y
self.dist = target - position # get total distance between target and position
direction = self.dist.normalize() # normalize so its constant in all directions
return direction
def distance_check(self, dist):
'''
Function:
tests if the total distance from the
sprite to the target is smaller than the
ammount of distance that would be normal
for the sprite to travel
(this lets the sprite know if it needs
to slow down. we want it to slow
down before it gets to it's target)
Returns:
bool
Parameters:
- self
- dist
this is the total distance from the
sprite to the target
can be any x,y value pair in
brackets [x,y]
or parentheses (x,y)
'''
dist_x = dist[0] ** 2 # gets absolute value of the x distance
dist_y = dist[1] ** 2 # gets absolute value of the y distance
t_dist = dist_x + dist_y # gets total absolute value distance
speed = self.moveSpeed ** 2 # gets aboslute value of the speed
if t_dist < (speed): # read function description above
return True
def moveSprite(self):
'''
Function:
gets direction to move then applies
the distance to the sprite.center
()
Parameters:
- self
'''
if self.stunned == False and self.attackNum >= self.attackAni:
self.limitSpeed()
self.target = (self.playerX, self.playerY)
oldx = self.rect.x
oldy = self.rect.y
self.dir = self.direction(self.target) # get direction
if self.dir: # if there is a direction to move
if self.distance_check(self.dist): # if we need to stop
self.rect.x = self.target[0] # center the sprite on the target
self.rect.y = self.target[1]
else: # if we need to move normal
self.trueX += (self.dir[0] * self.moveSpeed) # calculate speed from direction to move and speed constant
self.trueY += (self.dir[1] * self.moveSpeed)
self.rect.x = round(self.trueX) # apply values to sprite rect
self.rect.y = round(self.trueY)
self.isLeft(oldx, oldy)
def chooseImageNum(self):
if self.stunned:
self.currentImageNum = 4
elif self.attacking and self.attackNum < self.attackAni:
self.currentImageNum = 1
else:
if self.stepNum >= self.stepDelay:
self.stepNum = 0
if self.currentImageNum == 2:
self.currentImageNum = 3
else:
self.currentImageNum = 2
else:
self.stepNum += 1
def inflictDamage(self, damage, knockBack):
self.playSound()
if self.health - damage > 0:
self.health -= damage
self.trueX += knockBack
self.rect.x += knockBack
self.stunNum = 0
else:
self.kill()
def limitSpeed(self):
if self.rect.y < 360 and self.realSpeed - 3 > 0:
self.moveSpeed = self.realSpeed - 3
elif self.rect.y < 430 and self.realSpeed - 2 > 0:
self.moveSpeed = self.realSpeed - 2
elif self.rect.y < 500 and self.realSpeed - 1 > 0:
self.moveSpeed = self.realSpeed - 1
else:
self.moveSpeed = self.realSpeed
def isLeft(self, oldx, oldy):
if oldx > self.rect.x:
self.left = True
elif oldx < self.rect.x:
self.left = False