/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
def __init__(self, frames, time): """frames stores tuples (duration in ms, image-surf)""" self.start_time = time # personally, i would define the frames tuples the other way around and # just turn this around: durations, self.frame_list = zip(*frames) # next line does the same as # reduce(lambda l, v: l + [v + l[-1]], durations[1:], [durations[0]]) ;-> self.frame_end_time = [sum(durations[:i + 1]) for i in xrange(len(durations))] self.run_time = self.frame_end_time[-1] self.image = self.frame_list[0][1] self.rect = self.image.get_rect() def update(self, time): play_position = (time - self.start_time) % self.run_time # the above doesn't really work with a pausing feature, but you could # remember the pause time, and when you unpause, recompute the start_time for i, f in enumerate(self.frame_end_time): if play_position <= f: break self.image = self.frame_list[i] self.rect = self.image.get_rect()