简体   繁体   English

Pygame - 从图像的 x 和 y 坐标进行 blitting

[英]Pygame - blitting from x and y cordinates of image

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.我正在尝试减少我的 pygame 项目所需的文件数量,而不是使用包含例如 8 个引导文件的文件夹,我可以制作 1 个更大的图像,其中所有这些图像都有 8 张图片并排放置,具体取决于animation 打勾,图像的特定部分被 blitted。

Currently, I utilise lists.目前,我使用列表。

right = ["playerdesigns/playerright0.png","playerdesigns/playerright1.png","playerdesigns/playerright2.png","playerdesigns/playerright3.png"]

my code then just depending on animation tick, takes on of those files and blits it然后我的代码仅取决于 animation 滴答声,接收这些文件并对其进行 blits

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.但我希望将其制作成一个playerright.png图像文件,图片的 0-100 Xpixels 有playerright1.png , 101-200 Xpixels 有playerright2.png等,然后根据需要,我可以从任何一个 blit 100 宽的图像观点。

You can define a subsurface that is directly linked to the source surface with the method subsurface :您可以使用 subsurface 方法定义直接链接到源表面的subsurface

subsurface(Rect) -> Surface

Returns a new Surface that shares its pixels with its new parent.返回一个与其新父级共享其像素的新 Surface。 The new Surface is considered a child of the original.新 Surface 被视为原始 Surface 的子级。 Modifications to either Surface pixels will effect each other.对任一表面像素的修改将相互影响。

The Rect argument of subsurface specifies the rectangular area for the sub-image. subsurfaceRect参数指定子图像的矩形区域。 It can either be a pygame.Rect object or a tuple with 4 components ( x , y , width , height ).它可以是pygame.Rect object 或具有 4 个组件( xywidthheight )的元组。

For example, if you have an image that contains 3 100x100 size sub-images:例如,如果您的图像包含 3 个 100x100 大小的子图像:

right_surf = pygame.image.load("playerdesigns/playerright.png")
right_surf_list = [right_surf.subsurface((i*100, 0, 100, 100)) for i in range(3)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM