Skip to main content

A small library to handle animations in PyGame

Project description

Pygame AnimaLib

The AnimaLib is a library for animating Sprites of the PyGame library.

(GitHub)


Installation

Simply run pip install animalib to download and install the library from PyPI.


How it works

The core of the animation stands in the frames parameter of the AnimatedSprite.

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 use

Classes

  • Class AnimatedSprite

The class AnimatedSprite is a subclass of the pygame.sprite.Sprite class that takes two mandatory parameters :

Argument Description
frames A list of tuples of the form (image, duration)
*groups A list of groups to add the sprite to.

And a bunch of optional parameters :

Optional argument Description
start_frame The index of the first frame that will be displayed
first_frame The index of the first frame of the loop to be displayed [^1]
last_frame The index of the last frame of the loop that will be displayed [^1]
lock_at The index of the frame on which the animation will stop at
do_kill A boolean that indicates whether the sprite will be removed from its groups or not [^2]
delay_before_kill The delay before the sprite is removed from its groups [^2]

[^1]: Considering the animation will loop.

[^2]: To be efficient, the lock_at parameter has to be set.


Functions:

  • update()

If the sprite is added to a group, it will automatically be updated. Although, you can manuallly call update() 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 call draw(surface, pos) to draw the sprite on a surface.

Notice that the pos parameter is optional, and if not provided, the sprite's rect will be used.


Examples

  • Imperative (using group)

import pygame
from animalib import animalib
pygame.init()

run = True

screen = pygame.display.set_mode((500, 500))
group = pygame.sprite.Group()

frames = []
# [(image_0, duration_0), (image_1, duration_1), ...]

r, g, b = 255, 255, 255
for i in range(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 milliseconds
    
    r -= 25
    g -= 25
    b -= 25

sprite = animalib.AnimatedSprite(frames, group)

while run:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            run = False
            break
    screen.fill((0, 0, 0))
    
    group.update()
    group.draw(screen)
    
    pygame.display.flip()
pygame.quit()
  • Object (without group, using lock_at)

import pygame
from animalib import animalib

pygame.init()

class Game:
    def __init__(self):
        self.screen = pygame.display.set_mode((500, 500))
        self.run = False
        frames = self.load_frames()
        self.sprite = animalib.AnimatedSprite(frames, lock_at=5)

    def load_frames(self):
        frames = []
        r, g, b = 255, 255, 255
        for i in range(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 milliseconds
            r -= 25
            g -= 25
            b -= 25
        return frames
        
        
    def events(self):
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                self.run = False
                
    def loop(self):
        self.run = True
        while self.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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

animalib-0.0.9.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

animalib-0.0.9-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file animalib-0.0.9.tar.gz.

File metadata

  • Download URL: animalib-0.0.9.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.7.9

File hashes

Hashes for animalib-0.0.9.tar.gz
Algorithm Hash digest
SHA256 fc7c2039adead193babbf2703c23089fa62e4e29a9e29a0a565041001316711d
MD5 94a9cc5aaa79d45cc158a95ac8a3ad55
BLAKE2b-256 d8ede7aa05b48dad4521cd159463ef4683feed4e835bc51c6ac58e701f83f97f

See more details on using hashes here.

File details

Details for the file animalib-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: animalib-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.7.9

File hashes

Hashes for animalib-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 4531248d4518a82e0ddd52f3304fbd1d5a155bf7ecbeb70018cb6746c561cd88
MD5 4737c638fd4204cabac2574022e0740e
BLAKE2b-256 ba7ef80d975d965fb2484d6ff0d12c249335f007143e776e467c85fef08dc58d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page