Using a sprite map

A sprite map is where one image has several icons drawn on it. The idea is you rotate through the icons to give the appearance of animation.

Sprite maps are easy to find online, or create your own using a site such as

There are a couple of different ways of using these in your game/app.

Method 1

def get_image_partial( filename, x, y, w, h ):
    spritesheet = pygame.image.load( filename ).convert()
    image = pygame.Surface( [w,h] ).convert()
    image.blit( spritesheet, (0,0), (x,y,w,h))
    return image

# Create your pygame image variable using this command instead of pygame.image.load
pic = get_image_partial("sprites.gif", 0, 0, 50, 50)

# within your game loop, display the same way as a normal image
    window.blit(pic, (x,y))

Method 2

This demo uses a sprite from (I also have a link to it on my Python page to make it easier to get)

Explanatory notes: The load_sprite_map() command takes 5 parameters:

It then returns a list of images that you can alternate through just like any other list.

In your main game file:

import pygame, time, random
from pygame.locals import *
from spritemap import load_sprite_map

# Other functions we'll use
def constrain(val, min, max):
    """ Keep val within the min and max, looping to the other if it crosses"""
    if val < min:
        val = max
    elif val > max:
        val = min
    return val

# Start PyGame
window = pygame.display.set_mode((200,200))
fps = pygame.time.Clock()

# Declare colours, fonts, sprites and sounds
SPRITES = load_sprite_map("anna_walking.png", cols=7, rows=4, icon_width=32, icon_height=64)
BLACK = (0,0,0)
sprite_num = 21     # starting sprite number
from_sprite = 21    # number of 1st sprite in the walking sequence
to_sprite = 27      # number of last sprite in the walking sequence
x,y = 34,34         # top-left coordinate to draw the sprite
walking_left = False
walking_right = False
quit = False

# Main game loop
while not quit:

    # Process events
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                walking_right = True
            elif event.key == K_LEFT:
                walking_left = True
            elif event.key == K_ESCAPE:
                quit = True
        elif event.type == KEYUP:
            if event.key == K_RIGHT:
                walking_right = False
            elif event.key == K_LEFT:
                walking_left = False

    # Perform calculations
    if walking_left:
        sprite_num = sprite_num - 1
    elif walking_right:
        sprite_num = sprite_num + 1
    sprite_num = constrain(sprite_num, from_sprite, to_sprite)

    # Draw graphics
    window.blit(SPRITES[sprite_num], (x,y))


In a file called

### File:
import pygame

def load_sprite_map(filename, cols, rows, icon_width, icon_height):
    images = []
    SS = pygame.image.load(filename).convert_alpha()
    for row in range(0, rows):
        for col in range(0, cols):
            x = col * icon_width
            y = row * icon_height
            image = pygame.Surface((icon_width,icon_height), pygame.SRCALPHA).convert_alpha()
            image.blit(SS, (0, 0), (x,y,icon_width,icon_height))
            images.append( image )
    return images