如何保存在pygame中代替矩形的所有像素? [英] How to save all pixels that were in the place of a rect in pygame?
问题描述
有没有办法在绘制之前保存在矩形位置的所有像素?因此,当我在不同的地方再次绘制该矩形时,我可以用保存的像素替换旧的矩形,而不是手动填充该区域?
Is there a way to save all the pixels that were in the place of a rect before drawing it? So that when I draw that rect again in a different place I can replace the old one with the saved pixels instead of filling that area with colors mamually?
推荐答案
您可以使用 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.
创建次表面的副本到永久存储:
Create a copy of the subsurface to store it permanently:
rect_area = pygame.Rect(x, y, width, height)
area_surf = screen.subsurface(rect_area).copy()
稍后使用 Surface 替换矩形区域:
Use the Surface later to replace the rectangular area:
screen.blit(area_surf, region)
这篇关于如何保存在pygame中代替矩形的所有像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!