Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

python - 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?

question from:https://stackoverflow.com/questions/65858276/how-to-save-all-pixels-that-were-in-the-place-of-a-rect-in-pygame

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can define a subsurface that is directly linked to the source surface with the method subsurface:

subsurface(Rect) -> 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()

Use the Surface later to replace the rectangular area:

screen.blit(area_surf, region)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...