问题描述
我试图减少我的 pygame 项目所需的文件数量,而不是使用一个包含 8 个引导文件的文件夹,我可以制作 1 个更大的图像,其中所有 8 张图片并排放置并根据动画刻度,图像的特定部分会被 blit.
I'm trying to lessen the number of files I need for my pygame project by instead of having a folder with for example 8 boots files, I can make 1 bigger image that has all of them 8 pictures put next to each other and depending on animation tick, that specific part of the image gets blitted.
目前,我使用列表.
right = ["playerdesigns/playerright0.png","playerdesigns/playerright1.png","playerdesigns/playerright2.png","playerdesigns/playerright3.png"]
然后我的代码只取决于动画滴答声,接收这些文件并对其进行 blits
my code then just depending on animation tick, takes on of those files and blits it
但我想把它做成一个playerright.png图像文件,图片的0-100 Xpixels有playerright1.png,101-200 Xpixels有playerright2.png 等,然后根据需要,我可以从任何一点 blit 100 宽图像.
but I wish to make it into one playerright.png image file that 0-100 Xpixels of the picture has playerright1.png, 101-200 Xpixels has playerright2.png etc, and then depending on need, I can blit 100 wide image from any point.
推荐答案
您可以使用 subsurface
:
You can define a subsurface that is directly linked to the source surface with the method subsurface
:
次表面(矩形)->表面
返回一个与其新父级共享像素的新 Surface.新 Surface 被视为原始 Surface 的子代.对任一表面像素的修改会相互影响.
Returns a new Surface that shares its pixels with its new parent. The new Surface is considered a child of the original. Modifications to either Surface pixels will effect each other.
subsurface
的Rect
参数指定子图像的矩形区域.它可以是 pygame.Rect
对象或具有 4 个组件(x、y、width、height)的元组.
The Rect
argument of subsurface
specifies the rectangular area for the sub-image. It can either be a pygame.Rect
object or a tuple with 4 components (x, y, width, height).
例如,如果您的图片包含 3 个 100x100 大小的子图片:
For example, if you have an image that contains 3 100x100 size sub-images:
right_surf = pygame.image.load("playerdesigns/playerright.png")
right_surf_list = [right_surf.subsurface((i*100, 0, 100, 100)) for i in range(3)]
这篇关于Pygame - 从图像的 x 和 y 坐标进行 blitting的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!