In earlier examples, we never used a very complex background. Because of this, erasing a sprite was simply a matter of filling up its position with a solid color. However, this time, we have a graphical background with many different elements at unique positions. Each time an object moves, we'll need to replace its old position with the corresponding piece of background. Otherwise, things definitely wouldn't look right.
Thankfully, this is quite easy to do. Recall that when loading the background image, we store it in a variable called background. This object certainly hasn't gone anywhere, and we're free to access its contents—an original copy of the background—at any time. So, to erase an object in our game, we'll simply clip the corresponding rectangle from the original background image and draw it in its proper place on the screen:
def erase(screen, rect):
# Get the piece of the original background and copy it to the screen screen.blit(background.subsurface(rect).copy(), rect)
The subsurface method simply creates a “Surface object within a Surface object.” Both of the objects share pixels, so by creating a Surface object within our background, we have access to the exact contents of the area we need to access. We simply copy it and blit it to its proper place on the screen. It's a lot easier than it sounds, taking just one line to do.