This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
animate.py
40 lines (35 loc) · 1.48 KB
/
animate.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
from pygame import image, time
class Animator:
"""Handles simple animations based on multiple images"""
def __init__(self, image_list, delay=150, repeat=True):
self.images = []
for image_file in image_list:
if isinstance(image_file, str): # needs to be loaded
self.images.append(image.load(image_file))
else: # already loaded
self.images.append(image_file)
self.image_index = 0
self.last_frame = time.get_ticks()
self.frame_delay = delay
self.repeat = repeat
self.done = False
def reset(self):
"""Resets the animation back to its first frame"""
self.image_index = 0
def is_animation_done(self):
"""Return True if the animation is done or not (always returns True if repeating)"""
if self.repeat:
return True
return self.done
def get_image(self):
"""Get the current image in the animation"""
next_frame = abs(self.last_frame - time.get_ticks()) > self.frame_delay
if next_frame and self.repeat:
self.image_index = (self.image_index + 1) % len(self.images)
self.last_frame = time.get_ticks()
elif next_frame and not self.image_index >= len(self.images) - 1:
self.image_index += 1
self.last_frame = time.get_ticks()
elif next_frame and not self.repeat:
self.done = True
return self.images[self.image_index]