-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.py
47 lines (36 loc) · 1.36 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
import pygame
class Animation(object):
def __init__(self, frames, frame_duration):
self.current_time = 0
self.frame_number = 0
self.frames = []
for f in frames:
self.frames.append(f)
self.frame_duration = frame_duration
def current_frame(self):
return self.frames[self.frame_number]
def next_frame(self, dt):
if self.frame_duration == 0:
return self.frames[0]
self.current_time += dt
if self.current_time > self.frame_duration:
self.frame_number += 1
self.current_time = 0
if self.frame_number == len(self.frames):
self.frame_number = 0
return self.frames[self.frame_number]
class PlayOnceAnimation(Animation):
def __init__(self, frames, frame_duration):
Animation.__init__(self, frames, frame_duration)
def next_frame(self, dt):
if self.frame_number == len(self.frames) - 1:
self.frame_duration = 0
if self.frame_duration == 0:
return self.frames[len(self.frames) - 1]
self.current_time += dt
if self.current_time > self.frame_duration:
self.frame_number += 1
self.current_time = 0
if self.frame_number == len(self.frames):
self.frame_number = 0
return self.frames[self.frame_number]