The only way I know how to make an animation is by using individual images (as in 1 image per file). I got a sprite sheet but I don't want to spend the time cropping each individual sprite out.
I am using Python 3.2.
The only way I know how to make an animation is by using individual images (as in 1 image per file). I got a sprite sheet but I don't want to spend the time cropping each individual sprite out.
I am using Python 3.2.
It really isn't very hard to do… but the best sample code I found in a quick search is also a usable library that does the work for you: spritesheet
, right from the pygame wiki.
So, you can start off by just using that. I'd give you an example tailored to your use case, but you haven't given us any idea what your code looks like or what you want to do, so I can't possibly give you anything better than is already on that page, so:
import spritesheet
...
ss = spritesheet.spritesheet('somespritesheet.png')
# Sprite is 16x16 pixels at location 0,0 in the file...
image = ss.image_at((0, 0, 16, 16))
images = []
# Load two images into an array, their transparent bit is (255, 255, 255)
images = ss.images_at((0, 0, 16, 16),(17, 0, 16,16), colorkey=(255, 255, 255))
…
Meanwhile, you can read the (very simple) code in that spritesheet
class to understand how it works.
I use this
from pathlib import Path
from typing import List, Tuple, Union
import pygame
class Spritesheet:
def __init__(self, filepath: Path, sprite_size: Tuple[int, int], spacing: Tuple[int, int] = (0, 0), scale: Tuple[int, int] = None) -> None:
"""Initialize the spritesheet.
Args:
filepath (Path): Path to the spritesheet image file.
sprite_size (Tuple[int, int]): Width and height of each sprite in the sheet.
spacing (Tuple[int, int], optional): Spacing between each sprite (row spacing, column spacing). Defaults to (0, 0).
scale (Tuple[int, int], optional): Rescale each sprite to the given size. Defaults to None.
"""
self._sheet = pygame.image.load(filepath).convert_alpha()
self._sprite_size = sprite_size
self._spacing = spacing
self._scale = scale
def get_sprite(self, loc: Tuple[int, int], colorkey: Union[pygame.Color, int, None] = None) -> pygame.Surface:
"""Load a specific sprite from the spritesheet.
Args:
loc (Tuple[int, int]): Location of the sprite in the sheet (row, column).
colorkey (Union[pygame.Color, int, None], optional): Color to be treated as transparent. Defaults to None.
Returns:
pygame.Surface: The sprite image.
"""
x = loc[1] * (self._sprite_size[0] + self._spacing[0])
y = loc[0] * (self._sprite_size[1] + self._spacing[1])
rect = pygame.Rect(x, y, *self._sprite_size)
image = pygame.Surface(self._sprite_size, pygame.SRCALPHA).convert_alpha()
image.blit(self._sheet, (0, 0), rect)
if colorkey is not None:
if colorkey == -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, pygame.RLEACCEL)
if self._scale:
image = pygame.transform.scale(image, self._scale)
return image
def get_sprites(self, locs: List[Tuple[int, int]], colorkey: Union[pygame.Color, int, None] = None) -> List[pygame.Surface]:
"""Load multiple sprites from the spritesheet.
Args:
locs (List[Tuple[int, int]]): List of locations of the sprites in the sheet (row, column).
colorkey (Union[pygame.Color, int, None], optional): Color to be treated as transparent. Defaults to None.
Returns:
List[pygame.Surface]: List of sprite images.
"""
return [self.get_sprite(loc, colorkey) for loc in locs]
You can use it in this way:
spritesheet = Spritesheet("MySprite.png")
sprite_1 = spritesheet.get_sprite((1, 1)) # Gets sprite in location of (1, 1) from top left corner
sprite_2 = spritesheet.get_sprite((5, 3)) # Gets sprite in location of (5, 3) considering that the starting number is (0, 0)
© 2022 - 2025 — McMap. All rights reserved.