-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.py
56 lines (39 loc) · 1.4 KB
/
animation.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
import pathlib
import pygame
from settings import SPRITES_PATH, FPS
class Animation:
def __init__(self, name, frame_amount, fps, scale):
self.fps = fps / FPS # 60 FPS
self.iterable = 0
self.name = name
self.frame_amount = frame_amount
self.frames = []
self.mirrored_frames = []
self.scale = scale
self.mirrored = False
self.initialize_animation()
def initialize_animation(self):
"""Initialize the animation frames"""
for i in range(self.frame_amount):
frame = pygame.image.load(
pathlib.Path(SPRITES_PATH, f"{self.name}{(i + 1)}.png")
)
frame = pygame.transform.scale(frame, (self.scale, self.scale))
mirrored_frame = pygame.transform.flip(frame, -1, 0)
self.mirrored_frames.append(frame)
self.frames.append(mirrored_frame)
def speed_up(self):
"""Speed up the animation"""
self.fps += 1
def speed_down(self):
"""Speed down the animation"""
self.fps -= 1
def play(self, object):
"""Play the animation"""
if self.iterable >= self.frame_amount:
self.iterable = 0
if self.mirrored:
object.image = self.mirrored_frames[int(self.iterable)]
else:
object.image = self.frames[int(self.iterable)]
self.iterable += self.fps