Is there any way to "clear" a surface?

Eric picture Eric · Oct 28, 2009 · Viewed 20k times · Source

Is there any way to clear a surface from anything that has been blitted to it?

Answer

Wolfang Torres picture Wolfang Torres · Jun 18, 2015

If what you want is to make a pygame Surface object "clean", that is erase all images blited to it, to blit new images to it every game loop and keep the peer pixel alpha without creating a new surface, you can fill it, but instead of a solid color, use a transparent color

from pygame import Color, Surface
empty = Color(0,0,0,0) #The last 0 indicates 0 alpha, a transparent color
field = Surface((width, height), flags=SRCALPHA)

#Inside the game loop
field.fill(empty)

*Sorry is my english is bad, still learning