-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
animations.py
137 lines (106 loc) · 3.38 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
#!/usr/bin/env python
#-*- coding: utf8 -*-
from locals import TEXTURES_DIR
from objects import Object
import glob
import os
import pygame.transform
class AnimationException(Exception):
pass
class Animation(Object):
passable = True
movable = True
frame_time = 40 # ms
directory = None
texture_loader = None
_looped = False
def __init__(self, position):
Object.__init__(self)
self.frame = 0
self._position = position
self.frames = self._cls_frames[:]
self.frames_count = len(self.frames)
self._current_frame_time = 0
self._finished = False
self.load_frame()
def update(self, deltat):
if self._finished:
return
self._current_frame_time += deltat
if self._current_frame_time < self.frame_time:
return
self.frame += 1
if self.frame >= self.frames_count:
if self._looped:
self.frame = 0
else:
self._finished = True
self.frame -= self.frames_count - 1
return
self._current_frame_time -= self.frame_time
self.load_frame()
def load_frame(self):
self.image = self.frames[self.frame]
self.rect = self.image.get_rect()
self.rect.center = self._position
def rotate(self, degrees):
'''
Returns a rotated animation from this one.
'''
new_frames = {}
for ind, frame in enumerate(self.frames):
new_frames[ind] = pygame.transform.rotate(frame, degrees)
obj = self.__class__(self._position)
obj.frames = new_frames
return obj
def current_frame(self):
return self.frames[self.frame]
def stop(self):
self._finished = True
def play(self):
self._finished = False
@classmethod
def load_animation(cls, texture_loader):
if cls.directory is None:
raise AnimationException(
'Animation of class {} does not have frams directory'.
format(cls))
cls._cls_frames.clear()
cls.texture_loader = texture_loader
images = glob.glob(os.path.join(cls.directory, '*.png'))
images.sort()
if len(images) < 1:
raise AnimationException('No images found for animation')
for image in images:
cls._cls_frames.append(cls.texture_loader.load_texture(image))
@property
def finished(self):
return self._finished
class BulletExplosion(Animation):
directory = os.path.join(TEXTURES_DIR, 'animation_bullet_explosion')
_cls_frames = []
frame_time = 30
class FullSizeExplosion(Animation):
directory = os.path.join(TEXTURES_DIR, 'animation_full_explosion')
_cls_frames = []
frame_time = 40
class PanzerTankMovement(Animation):
directory = os.path.join(TEXTURES_DIR, 'player-panzer')
_cls_frames = []
frame_time = 20
_looped = True
class BasicTankMovement(Animation):
directory = os.path.join(TEXTURES_DIR, 'player-basic')
_cls_frames = []
frame_time = 30
_looped = True
class EnemyOneMovement(Animation):
directory = os.path.join(TEXTURES_DIR, 'enemy-1')
_cls_frames = []
frame_time = 30
_looped = True
class EnemyTwoMovement(Animation):
directory = os.path.join(TEXTURES_DIR, 'enemy-2')
_cls_frames = []
frame_time = 30
_looped = True