Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you select a sprite image from a sprite sheet in Python?

Tags:

python

pygame

I want to import a sprite sheet and select one sprite. How would I do this in Python/pygame?

like image 282
enrique2334 Avatar asked May 12 '12 01:05

enrique2334


People also ask

How do you use a sprite sheet in Python?

To use a sprite sheet, you load the sprite sheet as a single large image, and then you load the individual images from the sprite sheet image. This turns out to be much more efficient than loading a bunch of separate image files.

How do you use sprite sheets and sprites?

Sprite sheets are also used to store tiles for tile-based games. To use the images in a sprite sheet, you can import the file into Flash and then add the images to a timeline to create your sprites. Note: You can also use ActionScript to import and display images in a sprite sheet.


2 Answers

The above spritesheet loader is a good method. Based on that code, I made this generalized function that works for any spritesheet:

#!/usr/bin/python
#
# Sprite Sheet Loader - hammythepig
#
# Edited by Peter Kennedy
#
# License - Attribution - hammythepig
    #http://stackoverflow.com/questions/10560446/how-do-you-select-a-sprite-image-from-a-sprite-sheet-in-python
#
# Version = '2.0'

import pygame,sys
from pygame.locals import *

def sprite_sheet(size,file,pos=(0,0)):

    #Initial Values
    len_sprt_x,len_sprt_y = size #sprite size
    sprt_rect_x,sprt_rect_y = pos #where to find first sprite on sheet

    sheet = pygame.image.load(file).convert_alpha() #Load the sheet
    sheet_rect = sheet.get_rect()
    sprites = []
    print sheet_rect.height, sheet_rect.width
    for i in range(0,sheet_rect.height-len_sprt_y,size[1]):#rows
        print "row"
        for i in range(0,sheet_rect.width-len_sprt_x,size[0]):#columns
            print "column"
            sheet.set_clip(pygame.Rect(sprt_rect_x, sprt_rect_y, len_sprt_x, len_sprt_y)) #find sprite you want
            sprite = sheet.subsurface(sheet.get_clip()) #grab the sprite you want
            sprites.append(sprite)
            sprt_rect_x += len_sprt_x

        sprt_rect_y += len_sprt_y
        sprt_rect_x = 0
    print sprites
    return sprites

#VERSION HISTORY

    #1.1 - turned code into useable function

    #2.0 - fully functional sprite sheet loader

For reference, this cuts sheets into individual sprites one row at a time from left to right.

like image 179
aquasheep Avatar answered Oct 28 '22 17:10

aquasheep


I made this, it might interest you:

import pygame, sys
from pygame.locals import *

SCREEN_X=400
SCREEN_Y=400
#Screen size

SPRT_RECT_X=0  
SPRT_RECT_Y=0
#This is where the sprite is found on the sheet

LEN_SPRT_X=100
LEN_SPRT_Y=100
#This is the length of the sprite

screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y)) #Create the screen
sheet = pygame.image.load('C:\YOURFILE') #Load the sheet

sheet.set_clip(pygame.Rect(SPRT_RECT_X, SPRT_RECT_Y, LEN_SPRT_X, LEN_SPRT_Y)) #Locate the sprite you want
draw_me = sheet.subsurface(sheet.get_clip()) #Extract the sprite you want

backdrop = pygame.Rect(0, 0, SCREEN_X, SCREEN_Y) #Create the whole screen so you can draw on it

screen.blit(draw_me,backdrop) #'Blit' on the backdrop
pygame.display.flip()
#Draw the sprite on the screen

Hope I could help

like image 44
hammythepig Avatar answered Oct 28 '22 16:10

hammythepig