Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Sprite Sheets in Pygame?

Argh!!! I am new to this whole python thing and the only way I know how to make a "animation" is by using individual pictures (as in 1 pic per file). Well, I got a sprite sheet that is perfect for what I need, only problem is that it is a sprite sheet. I don't want to spend the time, cropping each individual sprite out.

More information, I am new to python and pygame. Just started learning the language back in August is a class that is moving very slowly...so if you could explain what you are doing so I can do it for future projects I would really appreciate it. I am also using Python 3.2, if that makes a difference because I am pretty sure there are some syntax differences between Python 2 and Python 3.

THANKS!!!! This will help me a lot.

like image 324
Zaid Saeed Avatar asked Nov 20 '13 23:11

Zaid Saeed


1 Answers

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.

like image 152
abarnert Avatar answered Nov 10 '22 14:11

abarnert