Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
845 views
in Technique[技术] by (71.8m points)

python - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...