-
Notifications
You must be signed in to change notification settings - Fork 1
/
animations.py
398 lines (337 loc) · 14.4 KB
/
animations.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
390
391
392
393
394
395
396
397
398
import pygame
from stgs import *
class RotAnimation:
#### Intializes first by grabbing sprite, sprite imgsheet data, and calculating a dir str ####
def __init__(self, sprite, imgSheet):
self.sprite = sprite
self.framex = 0
self.delay = 120
self.scalex, self.scaley = 1,1
self.imgSheet = Spritesheet(imgSheet)
self.tileSize = self.imgSheet.height
self.tileWidth, self.tileHeight = self.tileSize, self.tileSize
self.lastTick = pygame.time.get_ticks()
self.freeze = False # New feature to maintain framerate when sprites are not needed.
self.imageEffects = pygame.sprite.Group()
def update(self):
if not self.freeze:
if pygame.time.get_ticks() - self.lastTick >= self.delay:
self.framex += self.tileWidth
self.lastTick = pygame.time.get_ticks()
if self.framex > int(self.imgSheet.width - self.tileWidth):
self.framex = 0
self.sprite.image = self.imgSheet.get_image(self.framex, 0, self.tileWidth, self.tileHeight)
if self.scalex != 1 or self.scaley != 1:
self.sprite.image = pygame.transform.scale(self.sprite.image, (self.tileWidth*self.scalex, self.tileHeight*self.scaley))
self.sprite.rotCenter()
self.applyFx()
def scale(self, x, y=None):
if y==None:
self.scalex, self.scaley = x,x
else:
self.scalex, self.scaley = x,y
def applyFx(self):
for fx in self.imageEffects:
fx.update(self.sprite.image)
def fx(self, fx):
self.imageEffects.add(fx)
class PlayerAnimation:
#### Intializes first by grabbing sprite, sprite imgsheet data, and calculating a dir str ####
def __init__(self, sprite):
self.sprite = sprite
self.framex = 0
self.delay = 120
self.scalex, self.scaley = 1,1
self.imgSheet = sprite.imgSheet
self.mode = "default"
self.lastTick = pygame.time.get_ticks()
self.imageEffects = pygame.sprite.Group()
for k, v in self.imgSheet.items():
self.imgSheet[k] = Spritesheet(v)
self.tileSize = self.imgSheet[self.mode].height
def update(self):
if pygame.time.get_ticks() - self.lastTick >= self.delay:
self.framex += self.tileSize
self.lastTick = pygame.time.get_ticks()
if self.framex > int(self.imgSheet[self.mode].width - self.tileSize):
self.framex = 0
if self.mode != "default":
self.setMode()
self.sprite.image = self.imgSheet[self.mode].get_image(self.framex, 0, self.tileSize, self.tileSize)
if self.scalex != 1 or self.scaley != 1:
self.sprite.image = pygame.transform.scale(self.sprite.image, (self.tileSize*self.scalex, self.tileSize*self.scaley))
self.sprite.rotCenter()
self.applyFx()
def scale(self, x, y=None):
if y==None:
self.scalex, self.scaley = x,x
else:
self.scalex, self.scaley = x,y
def applyFx(self):
for fx in self.imageEffects:
fx.update(self.sprite.image)
def fx(self, fx):
self.imageEffects.add(fx)
def setMode(self, mode="default"):
if mode in self.imgSheet:
self.mode = mode
else:
print(f"mode {mode} does not exist for this sprite")
self.tileSize = self.imgSheet[self.mode].height
class Animation:
"""Basic animation"""
def __init__(self, sprite):
self.sprite = sprite
self.imageEffects = pygame.sprite.Group()
#self.delay = 12
#self.lastTick = pygame.time.get_ticks()
def fx(self, fx):
"""Add an effect to the animation"""
self.imageEffects.add(fx)
def update(self):
#if pygame.time.get_ticks() - self.lastTick >= self.delay:
for fx in self.imageEffects:
if isinstance(fx, HurtFx):
fx.update(self.sprite.image, self.sprite.origImage)
else:
fx.update()
class BasicAnimation:
#### Intializes first by grabbing sprite, sprite imgsheet data, and calculating a dir str ####
cache = {}
def __init__(self, sprite):
self.sprite = sprite
self.framex = 0
self.delay = 120
self.scalex, self.scaley = 1,1
self.imgSheet = sprite.imgSheet
self.mode = 'main'
self.lastTick = pygame.time.get_ticks()
self.imageEffects = pygame.sprite.Group()
if type(sprite) in self.cache:
self.imgSheet = self.cache[type(sprite)]
else:
print("caching imgSheet")
for k, v in self.imgSheet.items():
self.imgSheet[k] = Spritesheet(v)
self.cache[type(sprite)] = self.imgSheet
self.tileSize = self.imgSheet[self.mode].height
def getFirstFrame(self):
return self.imgSheet[self.mode].get_image(0, 0, self.tileSize, self.tileSize)
def update(self):
if pygame.time.get_ticks() - self.lastTick >= self.delay:
self.framex += self.tileSize
self.lastTick = pygame.time.get_ticks()
if self.framex > int(self.imgSheet[self.mode].width - self.tileSize):
self.framex = 0
self.sprite.image = self.imgSheet[self.mode].get_image(self.framex, 0, self.tileSize, self.tileSize)
if not self.scalex == 1 or not self.scaley == 1:
self.sprite.image = pygame.transform.scale(self.sprite.image, (self.tileSize*self.scalex, self.tileSize*self.scaley))
self.applyFx()
def scale(self, x, y=None):
if y==None:
self.scalex, self.scaley = x,x
else:
self.scalex, self.scaley = x,y
def applyFx(self):
for fx in self.imageEffects:
fx.update(self.sprite.image)
def fx(self, fx):
self.imageEffects.add(fx)
def setMode(self, mode="default"):
if mode in self.imgSheet:
self.mode = mode
else:
print(f"mode {mode} does not exist for this sprite")
self.tileSize = self.imgSheet[self.mode].height
class HurtFx(pygame.sprite.Sprite):
def __init__(self, duration = 300):
pygame.sprite.Sprite.__init__(self)
self.start = pygame.time.get_ticks()
self.duration = duration
def update(self, image, restoreImage = None):
time = pygame.time.get_ticks() - self.start
if time < self.duration:
darkness = min(255, max(0, round(255 * (time/self.duration))))
image.fill((255, darkness, darkness), special_flags = pygame.BLEND_MULT)
else:
# Restore image
if restoreImage:
image = restoreImage
self.kill()
# class animation:
# #### Intializes first by grabbing sprite, sprite imgsheet data, and calculating a dir str ####
# ## Full image sheet can contain: 'active', 'tileWidth', 'tileHeight', 'r', 'l', 'idleR', 'idleL','flyR', 'flyL'
# def __init__(self, sprite):
# self.sprite = sprite
# self.loadSheet()
# self.framex = 0
# self.delay = 120
# self.imageEffects = pygame.sprite.Group()
# self.rotation = False
# self.scalex, self.scaley = 1,1
# self.dir = 'idleR'
# self.lastTick = pygame.time.get_ticks()
# def loadSheet(self): #Very long tedious function
# self.imgSheet = self.sprite.imgSheet
# for k, v in self.imgSheet.items():
# if not k == 'active' and not k == 'tileWidth' and not k == 'tileHeight':
# self.imgSheet[k] = Spritesheet(v)
# self.tileWidth = self.imgSheet['tileWidth']
# self.hasIdle = True
# self.hasFly = True
# try:
# self.imgSheet['l']
# except:
# self.imgSheet['l'] = Spritesheet(pygame.transform.flip(self.imgSheet['r'].image, True, False), True)
# try:
# self.imgSheet['idleL']
# except:
# try:
# self.imgSheet['idleL'] = Spritesheet(pygame.transform.flip(self.imgSheet['idleR'].image, True, False), True)
# except:
# self.hasIdle = False
# try:
# self.imgSheet['flyL']
# except:
# try:
# self.imgSheet['flyL'] = Spritesheet(pygame.transform.flip(self.imgSheet['flyR'].image, True, False), True)
# except:
# self.hasFly = False
# try:
# self.tileHeight = self.imgSheet['tileHeight']
# except:
# self.tileHeight = self.tileWidth
# def update(self):
# self.getStrDir()
# if pygame.time.get_ticks() - self.lastTick >= self.delay:
# self.framex += self.tileWidth
# self.lastTick = pygame.time.get_ticks()
# if self.framex > int(self.imgSheet[self.dir].width - self.tileWidth):
# self.framex = 0
# try:
# if self.sprite.imgSheet['active']:
# if self.rotation:
# self.sprite.image = pygame.transform.scale(self.imgSheet[self.dir].get_image(self.framex, 0, self.tileWidth, self.tileHeight), (self.tileWidth*self.scalex, self.tileHeight*self.scaley))
# self.sprite.rotCenter(self.sprite.angle)
# else:
# self.sprite.image = self.imgSheet[self.dir].get_image(self.framex, 0, self.tileWidth, self.tileHeight)
# if not self.scalex == 1 or not self.scaley == 1:
# self.sprite.image = pygame.transform.scale(self.sprite.image, (self.tileWidth*self.scalex, self.tileHeight*self.scaley))
# self.applyFx()
# except KeyError: ## This is deprecated
# self.sprite.image = self.imgSheet[self.dir].get_image(self.framex, 0, self.tileWidth, self.tileHeight)
# self.sprite.image.set_colorkey((0,0,0))
# def getStrDir(self):
# ### Get the last specific direction
# lastDir = ''
# fullLastDir = self.dir
# if len(self.dir) == 1:
# lastDir = self.dir
# elif self.dir[0:4] == 'idle':
# lastDir = self.dir[4]
# else:
# lastDir = self.dir[3]
# if self.hasFly:
# if self.sprite.ground:
# if self.sprite.dir.x > 0:
# self.dir = 'r'
# elif self.sprite.dir.x < 0:
# self.dir = 'l'
# elif self.hasIdle:
# self.dir = 'idle' + lastDir.capitalize()
# else:
# self.dir = lastDir.lower()
# else:
# if self.sprite.dir.x > 0:
# self.dir = 'flyR'
# elif self.sprite.dir.x < 0:
# self.dir = 'flyL'
# else:
# self.dir = 'fly' + lastDir.capitalize()
# else:
# if self.sprite.dir.x > 0:
# self.dir = 'r'
# elif self.sprite.dir.x < 0:
# self.dir = 'l'
# elif self.hasIdle:
# self.dir = 'idle' + lastDir.capitalize()
# else:
# self.dir = lastDir.lower()
# if not self.dir == fullLastDir:
# self.framex = 0
# def applyFx(self):
# for fx in self.imageEffects:
# fx.update(self.sprite.image)
# def fx(self, fx):
# self.imageEffects.add(fx)
# def scale(self, x, y=None):
# if y==None:
# self.scalex, self.scaley = x,x
# else:
# self.scalex, self.scaley = x,y
# class animation2:
# #### Intializes first by grabbing sprite, sprite imgsheet data, and calculating a dir str ####
# def __init__(self, sprite):
# self.sprite = sprite
# self.loadSheet()
# self.framex = 0
# self.delay = 120
# self.rotation = False
# self.scalex, self.scaley = 1,1
# self.dir = 'idleR'
# self.lastTick = pygame.time.get_ticks()
# self.imageEffects = pygame.sprite.Group()
# def loadSheet(self):
# self.imgSheet = self.sprite.imgSheet
# for k, v in self.imgSheet.items():
# if not k == 'active' and not k == 'tileWidth' and not k == 'tileHeight':
# self.imgSheet[k] = Spritesheet(v)
# self.tileWidth = self.imgSheet['tileWidth']
# self.hasIdle = True
# self.hasFly = True
# try:
# self.imgSheet['l']
# except:
# self.imgSheet['l'] = Spritesheet(pygame.transform.flip(self.imgSheet['r'].image, True, False), True)
# try:
# self.imgSheet['u']
# except:
# self.imgSheet['u'] = Spritesheet(pygame.transform.flip(self.imgSheet['d'].image, False, True), True)
# try:
# self.tileHeight = self.imgSheet['tileHeight']
# except:
# self.tileHeight = self.tileWidth
# def update(self):
# self.getStrDir()
# if pygame.time.get_ticks() - self.lastTick >= self.delay:
# self.framex += self.tileWidth
# self.lastTick = pygame.time.get_ticks()
# if self.framex > int(self.imgSheet[self.dir].width - self.tileWidth):
# self.framex = 0
# self.sprite.image = self.imgSheet[self.dir].get_image(self.framex, 0, self.tileWidth, self.tileHeight)
# if not self.scalex == 1 or not self.scaley == 1:
# self.sprite.image = pygame.transform.scale(self.sprite.image, (self.tileWidth*self.scalex, self.tileHeight*self.scaley))
# self.applyFx()
# def getStrDir(self):
# ### Get the last specific direction
# lastDir = self.dir
# if self.sprite.dir.x > 0:
# self.dir = 'r'
# elif self.sprite.dir.x < 0:
# self.dir = 'l'
# elif self.sprite.dir.y > 0:
# self.dir = 'd'
# elif self.sprite.dir.y < 0:
# self.dir = 'u'
# else:
# self.dir = 'r'
# def scale(self, x, y=None):
# if y==None:
# self.scalex, self.scaley = x,x
# else:
# self.scalex, self.scaley = x,y
# def applyFx(self):
# for fx in self.imageEffects:
# fx.update(self.sprite.image)
# def fx(self, fx):
# self.imageEffects.add(fx)