forked from serenity-valley/game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleanimation.py
94 lines (72 loc) · 2.7 KB
/
simpleanimation.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
import sys
import pygame
from utils import Timer
class SimpleAnimation(object):
""" A simple animation. Scrolls cyclically through a list of
images, drawing them onto the screen in the same posision.
"""
def __init__(self, screen, pos, images, scroll_period, duration=-1):
""" Create an animation.
screen: The screen to which the animation will be drawn
pos: Position on the screen
images:
A list of surface objects to cyclically scroll through
scroll_period:
Scrolling period (in ms)
duration:
Duration of the animation (in ms). If -1, the
animation will have indefinite duration.
"""
print "starting animation."
self.screen = screen
self.images = images
self.pos = pos
self.img_ptr = 0
self.active = True
self.duration = duration
self.scroll_timer = Timer(scroll_period, self._advance_img)
self.active_timer = Timer(duration, self._inactivate, True)
def is_active(self):
""" Is the animation active ?
An animation is active from the moment of its creation
and until the duration has passed.
"""
return self.active
def update(self, time_passed):
""" Update the animation's state.
time_passed:
The time passed (in ms) since the previous update.
"""
for timer in [self.scroll_timer, self.active_timer]:
timer.update(time_passed)
def draw(self):
""" Draw the animation onto the screen.
"""
if self.active:
cur_img = self.images[self.img_ptr]
self.draw_rect = cur_img.get_rect().move(self.pos)
self.screen.blit(cur_img, self.draw_rect)
def _inactivate(self):
if self.duration >= 0:
self.active = False
def _advance_img(self):
self.img_ptr = (self.img_ptr + 1) % len(self.images)
class start():
if __name__ == "__main__":
print "initializing"
pygame.init()
screen = pygame.display.set_mode((300, 300), 0, 32)
clock = pygame.time.Clock()
explosion_img = pygame.image.load('images/explosion1.png').convert_alpha()
images = [explosion_img, pygame.transform.rotate(explosion_img, 90)]
expl = SimpleAnimation(screen, (100, 100), images, 100, 2120)
while True:
time_passed = clock.tick(50)
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
expl.update(time_passed)
expl.draw()
pygame.display.flip()
go = start()