# # Animation.py # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # Copyright # Author: Nicholas F. Hoover # Contributors: # Created: 2007.06.17 # Last Modified: 2007.07.18 # class Animation(object): NO_KEY_FRAME = [] ALL_KEY_FRAMES = -2 def __init__(self, animation, timingList, keyFrameList, rotate = False): self.animation = animation if type(timingList) == int: self.timingList = [] for i in range(len(animation)): self.timingList.append(timingList) elif type(timingList) == list: self.timingList = timingList if keyFrameList == Animation.ALL_KEY_FRAMES: self.keyFrameList = range(len(animation)) elif type(keyFrameList) == int: self.keyFrameList = [keyFrameList] else: self.keyFrameList = keyFrameList self.rotate = rotate self.reset() def update(self): """Advances the tick count for the current frame by 1. If the current frame's tick count expires, moves to the next image of the animation, and resets the tickCount. Sets animationComplete to true when the animation returns to the 0th frame. Returns True if the image was updated, false if it wasn't """ self.animationComplete = False if self.tickCount == self.timingList[self.currentFrame]: self.tickCount = 0 if not self.rotate: self.currentFrame += 1 if self.currentFrame == len(self.animation): self.currentFrame = 0 self.animationComplete = True else: if not self.rotatingBack: self.currentFrame += 1 if self.currentFrame == len(self.animation): self.currentFrame -=2 self.rotatingBack = True else: self.currentFrame -= 1 if self.currentFrame == -1: self.currentFrame += 2 self.rotatingBack = False self.image = self.animation[self.currentFrame] return True else: self.tickCount += 1 return False def isKeyFrame(self): return self.keyFrameList.count(self.currentFrame) >= 1 \ and self.tickCount == 0 def isComplete(self): return self.animationComplete def reset(self): self.currentFrame = 0 self.tickCount = 0 self.image = self.animation[self.currentFrame] self.rect = self.image.get_rect() self.animationComplete = False self.rotatingBack = False