2D Animations

Animations can be used by the Sprite class in place of static images. They are essentially containers for individual image frames, with a duration per frame. They can be infinitely looping, or stop at the last frame. You can load Animations from disk, such as from GIF files:

ani = pyglet.resource.animation('walking.gif')
sprite = pyglet.sprite.Sprite(img=ani)

Alternatively, you can create your own Animations from a sequence of images by using the from_image_sequence() method:

images = [pyglet.resource.image('walk_a.png')

ani = pyglet.image.Animation.from_image_sequence(images, duration=0.1, loop=True)

You can also use an pyglet.image.ImageGrid, which is iterable:

sprite_sheet = pyglet.resource.image('my_sprite_sheet.png')
image_grid = pyglet.image.ImageGrid(sprite_sheet, rows=1, columns=5)

ani = pyglet.image.Animation.from_image_sequence(image_grid, duration=0.1)

In the above examples, all of the Animation Frames have the same duration. If you wish to adjust this, you can manually create the Animation from a list of AnimationFrame:

image_a = pyglet.resource.image('walk_a.png')
image_b = pyglet.resource.image('walk_b.png')
image_c = pyglet.resource.image('walk_c.png')

frame_a = pyglet.image.AnimationFrame(image_a, duration=0.1)
frame_b = pyglet.image.AnimationFrame(image_b, duration=0.2)
frame_c = pyglet.image.AnimationFrame(image_c, duration=0.1)

ani = pyglet.image.Animation(frames=[frame_a, frame_b, frame_c])
class Animation(frames)

Sequence of images with timing information.

If no frames of the animation have a duration of None, the animation loops continuously; otherwise the animation stops at the first frame with duration of None.

Variables:frames (list of ~pyglet.image.AnimationFrame) – The frames that make up the animation.
add_to_texture_bin(texture_bin, border=0)

Add the images of the animation to a TextureBin.

The animation frames are modified in-place to refer to the texture bin regions.

  • texture_bin (TextureBin) – Texture bin to upload animation frames into.
  • border (int) – Leaves specified pixels of blank space around each image frame when adding to the TextureBin.
classmethod from_image_sequence(sequence, duration, loop=True)

Create an animation from a list of images and a constant framerate.

  • sequence (list of ~pyglet.image.AbstractImage) – Images that make up the animation, in sequence.
  • duration (float) – Number of seconds to display each image.
  • loop (bool) – If True, the animation will loop continuously.
Return type:



Get the total duration of the animation in seconds.

Return type:float

Get the maximum image frame height.

This method is useful for determining texture space requirements: due to the use of anchor_y the actual required playback area may be larger.

Return type:int

Get the maximum image frame width.

This method is useful for determining texture space requirements: due to the use of anchor_x the actual required playback area may be larger.

Return type:int
get_transform(flip_x=False, flip_y=False, rotate=0)

Create a copy of this animation applying a simple transformation.

The transformation is applied around the image’s anchor point of each frame. The texture data is shared between the original animation and the transformed animation.

  • flip_x (bool) – If True, the returned animation will be flipped horizontally.
  • flip_y (bool) – If True, the returned animation will be flipped vertically.
  • rotate (int) – Degrees of clockwise rotation of the returned animation. Only 90-degree increments are supported.
Return type:


class AnimationFrame(image, duration)

A single frame of an animation.