pygame - 来自另一个表面的一部分的表面

pygame - surface from part of another surface

如何从另一个表面的一部分创建一个表面。原因是我有一个图像作为背景,上面显示了一个时钟 - 所以当我更新一个新时间时,我需要先恢复那个部分的原始背景,然后再 bliting 新时钟文本。我可以使用翻转/全屏更新来做到这一点,但我想避免这种情况,只更新/恢复屏幕的一部分。

您可以通过将单个矩形或矩形列表传递给 pygame.display.update 来更新屏幕的一部分:

Update portions of the screen for software displays. [...] You can pass the function a single rectangle, or a sequence of rectangles.

例如:

pygame.display.update(rect_region)

您可以使用 subsurface 方法定义直接链接到源表面的次表面:

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.

例如:

rect_region = (x, y, width, height)
subsurf = source_surf.subsurface(rect_region)

或者,blit 方法允许指定源 Surface:

的矩形 sub-area

[...] An optional area rectangle can be passed as well. This represents a smaller portion of the source Surface to draw. [...]

例如:

rect_region = (x, y, width, height)
traget.blit(source_surf, (posx, posy), rect_region)

另见 How can I crop an image with Pygame? and