forked from mattboan/Galtron
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathanimations.py
59 lines (48 loc) · 1.85 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
import pygame as pg
class AnimatedSprite:
def __init__(self, sprite, item_width, item_height, item_count):
self.sprite = sprite
self.item_width = item_width
self.item_height = item_height
self.item_count = item_count
self.frames = []
# extract subsurfaces
rect = self.sprite.get_rect()
num_cols = rect.right // item_width
num_rows = rect.bottom // item_height
for idx in range(item_count):
clip_left = (idx % num_cols) * item_width
clip_top = (idx // num_rows) * item_height
clip_rect = pg.Rect(clip_left, clip_top, item_width, item_height)
self.frames.append(self.sprite.subsurface(clip_rect))
def getFrame(self, idx):
return self.frames[idx]
class Animation:
__slots__ = ('x', 'y', 'frame')
def __init__(self, x, y, initial_frame=0):
self.x = x
self.y = y
self.frame = initial_frame
class Explosions:
def __init__(self):
self.sprite = AnimatedSprite(
pg.image.load('gfx/explosion-sheet.png').convert_alpha(),
100, 100, 34)
self.instances = dict()
self.explosion_id = 0
def add(self, x, y):
ani = Animation(x - self.sprite.item_width // 2,
y - self.sprite.item_height // 2)
self.explosion_id += 1
self.instances[self.explosion_id] = ani
def draw(self, screen):
for eid in tuple(self.instances.keys()):
ani = self.instances[eid]
rect = pg.Rect(ani.x, ani.y,
self.sprite.item_width,
self.sprite.item_height)
screen.blit(self.sprite.getFrame(ani.frame), rect)
if ani.frame == self.sprite.item_count - 1:
del self.instances[eid]
else:
ani.frame += 1