Pygame : 创建一个不能修改父图像但可以被父图像修改的次表面

Pygame : create a subsurface that cannot modify parent image, but can be modified by parent

我正在尝试创建另一个表面的次表面,但仅由父级 -> 子级链接。

如果我们想修改父表面,它应该有一个次表面的属性:子表面也应该被修改。 但是,如果我们想修改子表面,它应该有副本的 属性:父表面应该 被修改。

例如:

#1 step : all the "subsurfaces" are created from "myimage"
parent = ('myimage.png')
child = parent.subsurface((32,96,32,32))
prop = parent.subsurface((0,0,32,32))
child.blit(prop_to_blit,(0,0))

#2 step : the parent image is modified
parent.blit('otherimage.png')

在第一步结束时,子表面应该看起来像 child+prop 但父表面应该保持完整。

在第 2 步结束时,修改了父级,所有的次表面也应该被修改。

为了让我的程序运行,我需要将另一个图像 blit 到父图像上(因此具有具有这些属性的“地下”功能)。 我已经尝试过这个解决方案的替代方案,所有这些都需要直接修改带有变量赋值的父图像,然后重新加载文件。但由于循环模块调用无法工作

提前致谢!

次表面与源表面共享像素。因此,表面不可能在一个方向上共享像素但在另一个方向上不共享像素。但是,您可以随时 copy() Surface
所以你必须做出决定。您要么想要与像素共享,要么想要复制像素。 copy 第二个地下和 blit 第二个地下:

child = parent.subsurface((32,96,32,32)).copy()
prop = parent.subsurface((0,0,32,32))
child.blit(prop_to_blit,(0,0))