class TimedFrameAnimation(Sprite)


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. def __init__(self, frames, time):
  2. """frames stores tuples (duration in ms, image-surf)"""
  3. self.start_time = time
  4. # personally, i would define the frames tuples the other way around and
  5. # just turn this around:
  6. durations, self.frame_list = zip(*frames)
  7. # next line does the same as
  8. # reduce(lambda l, v: l + [v + l[-1]], durations[1:], [durations[0]]) ;->
  9. self.frame_end_time = [sum(durations[:i + 1]) for i in xrange(len(durations))]
  10. self.run_time = self.frame_end_time[-1]
  11. self.image = self.frame_list[0][1]
  12. self.rect = self.image.get_rect()
  13.  
  14. def update(self, time):
  15. play_position = (time - self.start_time) % self.run_time
  16. # the above doesn't really work with a pausing feature, but you could
  17. # remember the pause time, and when you unpause, recompute the start_time
  18. for i, f in enumerate(self.frame_end_time):
  19. if play_position <= f:
  20. break
  21. self.image = self.frame_list[i]
  22. self.rect = self.image.get_rect()

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.