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 https://www.piskelapp.com/
There are a couple of different ways of using these in your game/app.
- Method 1 - Extract a sub-part of the image and use that. This is the best method if you only want to use parts of the sprite sheet, or different sprites are different sizes etc.
- Method 2 - I’ve written a function you can use that will automatically subdivide your image into a grid of rows and columns. This method works best when all sprites are of the same width and height (such as if you have created them yourself using piskelapp or similar)
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))
This demo uses a sprite from https://chielo.deviantart.com/art/Anna-Walking-Sprite-Sheet-357242964 (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:
- The filename of your jpeg or png image file
- The number of columns in your sprite map image file
- The number of rows in your sprite map image file
- The width of each column (pixels) in your sprite map image file
- The height of each column (pixels) in your sprite map image file
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 pygame.init() 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.fill(BLACK) window.blit(SPRITES[spriteNumber], (x,y)) pygame.display.update() fps.tick(10) pygame.quit()
In a file called
### File: spritemap.py 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