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 spritesheet import load_sprite_map

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

# Declare colours, fonts, sprites and sounds
BLACK = (0,0,0)
SPRITES = load_sprite_map("anna_walking_sprite_sheet_by_chielo.png", 7, 4, 32, 64)
x,y = 34,34
spriteNumber = 21
min_sprite_number = 21
max_sprite_number = 27
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:
                spriteNumber += 1
            elif event.key == K_LEFT:
                spriteNumber -= 1
            elif event.key == K_ESCAPE:
                quit = True

    # Perform calculations
    if spriteNumber < min_sprite_number:
        spriteNumber = max_sprite_number
    if spriteNumber > max_sprite_number:
        spriteNumber = min_sprite_number
    print("Sprite number: ",spriteNumber)

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


In a file called

### File:
import pygame

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