Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 40 additions & 69 deletions simpleanimation.py
Original file line number Diff line number Diff line change
@@ -1,94 +1,65 @@
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.
"""
class SimpleAnimation:
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)
self.scroll_timer = scroll_period
self.active_timer = duration

self.scroll_time_passed = 0
self.active_time_passed = 0

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.
self.scroll_time_passed += time_passed
self.active_time_passed += time_passed

if self.scroll_time_passed >= self.scroll_timer:
self._advance_img()
self.scroll_time_passed = 0

time_passed:
The time passed (in ms) since the previous update.
"""
for timer in [self.scroll_timer, self.active_timer]:
timer.update(time_passed)
if self.duration >= 0 and self.active_time_passed >= self.active_timer:
self.active = False

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)
draw_rect = cur_img.get_rect().move(self.pos)
self.screen.blit(cur_img, 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()

if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode((300, 300))
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:
pygame.quit()
sys.exit()

expl.update(time_passed)
expl.draw()

pygame.display.flip()