如何在 Pygame 中使用 Sprite Sheets? [英] How do I use Sprite Sheets in Pygame?

查看:57
本文介绍了如何在 Pygame 中使用 Sprite Sheets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

啊!!!我对这整个 python 事物很陌生,我知道如何制作动画"的唯一方法是使用单个图片(如每个文件 1 张图片).好吧,我得到了一个非常适合我需要的精灵表,唯一的问题是它是一个精灵表.我不想花时间裁剪每个单独的精灵.

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.

更多信息,我新手 Python 和 pygame.八月份刚开始学习这门语言是一个进展非常缓慢的课程......所以如果你能解释你在做什么,我可以为未来的项目做这件事,我会非常感激.我也在使用 Python 3.2,如果这有所不同,因为我很确定 Python 2 和 Python 3 之间存在一些语法差异.

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.

推荐答案

这确实不难做……但我在快速搜索中找到的最好的示例代码也是一个可用的库这对你有用:spritesheet,直接来自 pygame wiki.

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))
…

同时,您可以阅读该 spritesheet 类中的(非常简单的)代码以了解其工作原理.

Meanwhile, you can read the (very simple) code in that spritesheet class to understand how it works.

这篇关于如何在 Pygame 中使用 Sprite Sheets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆