Package: animalib
PygameAnimaLibTheAnimaLibis a library for animatingSpritesof thePyGamelibrary.(GitHub)InstallationSimply runpip install animalibto download and install the library fromPyPI.How it worksThe core of the animation stands in theframesparameter of theAnimatedSprite.In fact, the program will check if the duration associated to the current frame has been reached.
If so, the current frame will be set to the next one, and the duration will be reset, waiting for the next duration to be reached, and so on.How to useClassesClassAnimatedSpriteThe classAnimatedSpriteis a subclass of thepygame.sprite.Spriteclass
that takes two mandatory parameters :ArgumentDescriptionframesA list of tuples of the form(image, duration)*groupsA list of groups to add the sprite to.And a bunch of optional parameters :Optional argumentDescriptionstart_frameThe index of the first frame that will be displayedfirst_frameThe index of the first frame of the loop to be displayed [^1]last_frameThe index of the last frame of the loop that will be displayed [^1]lock_atThe index of the frame on which the animation will stop atdo_killA boolean that indicates whether the sprite will be removed from its groups or not [^2]delay_before_killThe delay before the sprite is removed from its groups [^2][^1]: Considering the animationwillloop.[^2]: To be efficient, thelock_atparameterhas to be set.Functions:update()If the sprite is added to a group, it will automatically be updated.
Although, you can manuallly callupdate()to update the sprite each frame.draw(surface:pygame.Surface, pos:iterable)If the sprite is added to a group, then it will automatically be drawn.
Although, you can calldraw(surface, pos)to draw the sprite on a surface.Notice that theposparameter is optional, and if not provided, the sprite's rect will be used.ExamplesImperative (using group)importpygamefromanimalibimportanimalibpygame.init()run=Truescreen=pygame.display.set_mode((500,500))group=pygame.sprite.Group()frames=[]# [(image_0, duration_0), (image_1, duration_1), ...]r,g,b=255,255,255foriinrange(0,10):surf=pygame.Surface((400,400))surf.fill((r,g,b))# You can either load an image from a file instead of creating a surface...frames.append((surf,500))# the duration is in millisecondsr-=25g-=25b-=25sprite=animalib.AnimatedSprite(frames,group)whilerun:foreinpygame.event.get():ife.type==pygame.QUIT:run=Falsebreakscreen.fill((0,0,0))group.update()group.draw(screen)pygame.display.flip()pygame.quit()Object (without group, usinglock_at)importpygamefromanimalibimportanimalibpygame.init()classGame:def__init__(self):self.screen=pygame.display.set_mode((500,500))self.run=Falseframes=self.load_frames()self.sprite=animalib.AnimatedSprite(frames,lock_at=5)defload_frames(self):frames=[]r,g,b=255,255,255foriinrange(0,10):surf=pygame.Surface((400,400))surf.fill((r,g,b))# You can either load an image from a file instead of creating a surface...frames.append((surf,500))# the duration is in millisecondsr-=25g-=25b-=25returnframesdefevents(self):foreinpygame.event.get():ife.type==pygame.QUIT:self.run=Falsedefloop(self):self.run=Truewhileself.run:self.events()self.screen.fill((0,0,0))self.sprite.update()self.sprite.draw(self.screen,(0,0))pygame.display.flip()pygame.quit()game=Game()game.loop()In these examples, the sprite's image is changing every 500 milliseconds and
its color is changing form white to gray. However, in the first one, the animation is looping,
and the group handle the draw itself.But for the second one, the sprite locks itself at the 5th frame,
and you have to manually handle the draw.