how can I create a surface from a section of another surface. Reason is that I have an image as a background, and on that am displaying a clock - so when i update a new time, I need to first restore the original background back on that section before bliting the new clock text. I could do this using flip / full screen update, but want to avoid that and only update / restore a section of the screen.
1 Answers
You can update a section of the screen by passing a a single rectnagle or a list of rectangles to pygame.display.update
:
Update portions of the screen for software displays. [...] You can pass the function a single rectangle, or a sequence of rectangles.
e.g.:
pygame.display.update(rect_region)
You can define a subsurface that is directly linked to the source surface with the subsurface
method:
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.
e.g.:
rect_region = (x, y, width, height)
subsurf = source_surf.subsurface(rect_region)
Alternatively the blit
method allows to specify a rectangular sub-area of a source Surface:
[...] An optional area rectangle can be passed as well. This represents a smaller portion of the source Surface to draw. [...]
e.g.:
rect_region = (x, y, width, height)
traget.blit(source_surf, (posx, posy), rect_region)
See also How can I crop an image with Pygame? and Pygame - blitting from x and y cordinates of image.

- 202,892
- 27
- 131
- 174