def __init__(self, sheetName, frames, frameWidth, frameHeight, framePerRow): """ Initialize the animated sprite. """ # call the super super(AnimatedSprite, self).__init__() # if the sheet is not exists... # TODO: prove it #if AnimatedSprite.sheet is None: # AnimatedSprite.sheet = pygame.image.load(sheetName) self.sheet = pygame.image.load(sheetName) # initialize values #self.sheet = AnimatedSprite.sheet self.maxFrames = frames self.frameWidth = frameWidth self.frameHeight = frameHeight self.framePerRow = framePerRow self.stepToFrame(self.currentFrame) self.spriteAnimation = AnimatedValue(0, 0, 1, False, False)
def startAnimation(self, startFrame, endFrame, timeInterval, loop): """ Starts the animation. """ self.spriteAnimation = AnimatedValue(startFrame, endFrame, timeInterval, loop)
class AnimatedSprite(pygame.sprite.Sprite): """ This class represents an animated Sprite. The sprite animation based on a sprite sheet. Sprite sheets: http://www.codeandweb.com/what-is-a-sprite-sheet TODO: implement pause TODO: implement named animations """ currentFrame = 0 # the current frame maxFrames = 28 # the maximum frame framePerRow = 7 # frames per row frameWidth = 64 # frame's width frameHeight = 64 # frame's height sheet = None # the sprite sheet spriteRect = None # rect to draw the current frame # the program moves this frame on the sheet spriteAnimation = None # the animated value, the current frame calc. _lastFrame = -1 # the last rendered frame def __init__(self, sheetName, frames, frameWidth, frameHeight, framePerRow): """ Initialize the animated sprite. """ # call the super super(AnimatedSprite, self).__init__() # if the sheet is not exists... # TODO: prove it #if AnimatedSprite.sheet is None: # AnimatedSprite.sheet = pygame.image.load(sheetName) self.sheet = pygame.image.load(sheetName) # initialize values #self.sheet = AnimatedSprite.sheet self.maxFrames = frames self.frameWidth = frameWidth self.frameHeight = frameHeight self.framePerRow = framePerRow self.stepToFrame(self.currentFrame) self.spriteAnimation = AnimatedValue(0, 0, 1, False, False) def stepToFrame(self, frame): """ Steps the current frame to the parameter. """ self.currentFrame = frame if self.currentFrame >= self.maxFrames: self.currentFrame = 0 elif self.currentFrame < 0: self.currentFrame = self.maxFrames # calculates the the current frame's rect topx = self.currentFrame % self.framePerRow * self.frameWidth topy = int(self.currentFrame / self.framePerRow) topy *= self.frameHeight self.spriteRect = pygame.Rect( topx, topy, self.frameWidth, self.frameHeight) def startAnimation(self, startFrame, endFrame, timeInterval, loop): """ Starts the animation. """ self.spriteAnimation = AnimatedValue(startFrame, endFrame, timeInterval, loop) def stopAnimation(self): self.spriteAnimation.stopAnimation() def update(self): """ Updates the sprite """ frame = round(self.spriteAnimation.getCurrentValue()) if frame != self._lastFrame: self.stepToFrame(frame) self._lastFrame = frame def drawSprite(self, surface, location): """ Draws the sprite on the parameter surface. """ surface.blit(self.sheet, location, self.spriteRect)