-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameCharacters.py
More file actions
417 lines (343 loc) · 13.6 KB
/
GameCharacters.py
File metadata and controls
417 lines (343 loc) · 13.6 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import pygame, sys, random
from pygame.locals import *
from VectorClass import Vector
class Char(pygame.sprite.Sprite):
def __init__(self, name, x, y, images, interactFunc, left=False):
'''images: 5 image string tuple
images is (stand, stand movement1, stand movement2, stand movement3, 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.interactFunc = interactFunc
self.picChangeChance = 0.05 #must be less than 1.0 smaller num less likely-default:0.05
self.picDelay = 20
self.picNum = self.picDelay
self.left = left
self.stunned = False
self.stunDelay = 10
self.stunNum = self.stunDelay
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 loadImages(self, images):
'''images: 5 image string tuple
images is (stand, stand movement1, stand movement2, stand movement3, 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 interact(self):
self.interactFunc(self.rect.x,self.rect.y,self.name)
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.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 = 4
else:
if self.picNum >= self.picDelay:
randfloat = random.random()
if randfloat <= self.picChangeChance:
self.currentImageNum = random.randint(0,3)
self.picNum = 0
else:
self.picNum += 1
def inflictDamage(self, damage, knockBack):
self.stunNum = 0
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 MovChar(Char):
def __init__(self, name, x, y, images, interactFunc, target, moveSpeed=3):
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.target = target
self.startPoint = (x, y)
self.realSpeed = moveSpeed # Real movement speed of the sprite
self.moveSpeed = moveSpeed # movement speed of the sprite
self.realWidth = self.rect.width
self.realHeight = self.rect.height
self.interactFunc = interactFunc
self.left = False
self.stunned = False
self.stunDelay = 10
self.stunNum = self.stunDelay
self.stepDelay = 10
self.stepNum = self.stepDelay
def loadImages(self, images):
'''images: 4 image string tuple
images is (stand, 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.images = (self.i0,self.i1,self.i2,self.i3)
def update(self):
self.isStunned()
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:
self.limitSpeed()
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)
else:
self.target,self.startPoint = self.startPoint,self.target
self.isLeft(oldx, oldy)
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
def chooseImageNum(self):
if self.stunned:
self.currentImageNum = 3
else:
if self.stepNum >= self.stepDelay:
self.stepNum = 0
if self.currentImageNum == 1:
self.currentImageNum = 2
else:
self.currentImageNum = 1
else:
self.stepNum += 1
class MovChar_Pauses(MovChar):
def __init__(self, name, x, y, images, interactFunc, target, moveSpeed=3, pauseDelay = 50):
'''images: 7 image string tuple
images is (stand, stand movement1, stand movement2, stand movement3, 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.target = target
self.startPoint = (x, y)
self.realSpeed = moveSpeed # Real movement speed of the sprite
self.moveSpeed = moveSpeed # movement speed of the sprite
self.realWidth = self.rect.width
self.realHeight = self.rect.height
self.interactFunc = interactFunc
self.left = False
self.stunned = False
self.stunDelay = 10
self.stunNum = self.stunDelay
self.stepDelay = 10
self.stepNum = self.stepDelay
self.paused = False
self.pauseChance = 0.005
self.pauseDelay = pauseDelay
self.pauseNum = pauseDelay
self.picChangeChance = 0.05 #must be less than 1.0 smaller num less likely-default:0.05
self.picDelay = 20
self.picNum = self.picDelay
def loadImages(self, images):
'''images: 7 image string tuple
images is (stand, stand movement1, stand movement2, stand movement3, 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.i5 = pygame.image.load(images[5]).convert_alpha()
self.i6 = pygame.image.load(images[6]).convert_alpha()
self.images = (self.i0,self.i1,self.i2,self.i3,self.i4,self.i5,self.i6)
def update(self):
self.isStunned()
self.isPaused()
self.moveSprite()
self.chooseImageNum()
self.render()
def pauseImageNum(self):
self.currentImageNum = 0
def isPaused(self):
if self.pauseNum >= self.pauseDelay:
randfloat = random.random()
if randfloat <= self.pauseChance:
self.paused = True
self.pauseNum = 0
self.pauseImageNum()
else:
self.paused = False
else:
self.pauseNum += 1
def chooseImageNum(self):
if self.stunned:
self.currentImageNum = 6
elif self.paused:
if self.picNum >= self.picDelay:
randfloat = random.random()
if randfloat <= self.picChangeChance:
self.currentImageNum = random.randint(0,3)
self.picNum = 0
else:
self.picNum += 1
else:
if self.stepNum >= self.stepDelay:
self.stepNum = 0
if self.currentImageNum == 4:
self.currentImageNum = 5
else:
self.currentImageNum = 4
else:
self.stepNum += 1
def moveSprite(self):
'''
Function:
gets direction to move then applies
the distance to the sprite.center
()
Parameters:
- self
'''
if self.stunned == False and self.paused == False:
self.limitSpeed()
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)
else:
self.target,self.startPoint = self.startPoint,self.target
self.isLeft(oldx, oldy)