There are two types of animation: frame - dependent and time-dependent . Both work in a similar way.
To the main cycle
- Upload all images to a list.
- Create three variables:
index , which tracks the current index of the image list.current_time or current_frame , which tracks the current time or current frame since the last index switch.animation_time or animation_frames , which determine how many seconds or frames must elapse before switching the image.
During the main cycle
- The increment of
current_time by the number of seconds elapsed since its last increase, or the increase of current_frame by 1. - Check if
current_time >= animation_time or current_frame >= animation_frame . If true, continue with 3-5. - Reset
current_time = 0 or current_frame = 0 . - Index increase, unless it is equal to or greater than the number of images. In this case, reset
index = 0 . - Resize the sprite accordingly.
Full working example
import os import pygame pygame.init() SIZE = WIDTH, HEIGHT = 720, 480 BACKGROUND_COLOR = pygame.Color('black') FPS = 60 screen = pygame.display.set_mode(SIZE) clock = pygame.time.Clock() def load_images(path): """ Loads all images in directory. The directory must only contain images. Args: path: The relative or absolute path to the directory to load images from. Returns: List of images. """ images = [] for file_name in os.listdir(path): image = pygame.image.load(path + os.sep + file_name).convert() images.append(image) return images class AnimatedSprite(pygame.sprite.Sprite): def __init__(self, position, images): """ Animated sprite object. Args: position: x, y coordinate on the screen to place the AnimatedSprite. images: Images to use in the animation. """ super(AnimatedSprite, self).__init__() size = (32, 32)
When to choose which
Time-dependent animation allows you to play the animation at the same speed, regardless of how slow / fast the frame rate or how slow / fast your computer is. This allows your program to freely change the frame rate without affecting the animation, and it will also be consistent, even if the computer can not cope with the frame rate. If the program lags behind, the animation will catch up to the state, it should have been as if there was no delay.
Although it may happen that the animation loop does not synchronize with the frame rate, which makes the animation loop irregular. For example, let's say that we update frames every 0.05 seconds and the image of the animation switch every 0.075 seconds, then the cycle will be:
- Frame 1; 0.00 seconds image 1
- Frame 2; 0.05 seconds; image 1
- Frame 3; 0.10 seconds; image 2
- Frame 4; 0.15 s; image 1
- Frame 5; 0.20 s; image 1
- Box 6; 0.25 s; image 2
And so on...
Frame-dependent may look smoother if your computer can process frame rates sequentially. If a lag occurs, it will stop in its current state and restart when the lag stops, which makes the lag more noticeable. This alternative is a little easier to implement, since you just need to increment the current_frame with 1 per call instead of dealing with delta time ( dt ) and passing it to each object.
Sprites

Result

Ted klein bergman
source share