Help! How Do I Move An Image?¶
out how to make an image move around the screen. Without
understanding all the concepts, it can be very confusing. You're not the
first person to be stuck here, I'll do my best to take things step by step.
We'll even try to end with methods of keeping your animations efficient.
Note that we won't be teaching you to program with python in this article,
just introduce you to some of the basics with pygame.
Just Pixels On The Screen
Pygame has a display Surface. This is basically an image that is visible on
the screen, and the image is made up of pixels. The main way you change
these pixels is by calling the blit() function. This copies the pixels from one
image onto another.
This is the first thing to understand. When you blit an image onto the
screen, you are simply changing the color of the pixels on the screen. Pixels
aren't added or moved, we just change the colors of the pixels already on
the screen. These images you blit to the screen are also Surfaces in
pygame, but they are in no way connected to the display Surface. When
they are blitted to the screen they are copied into the display, but you still
have a unique copy of the original.
With this brief description. Perhaps you can already understand what is
needed to "move" an image. We don't actually move anything at all. We
simply blit the image in a new position. But before we draw the image in
the new position, we'll need to "erase" the old one. Otherwise the image
will be visible in two places on the screen. By rapidly erasing the image and
redrawing it in a new place, we achieve the "illusion" of movement.
Through the rest of this tutorial we will break this process down into
simpler steps. Even explaining the best ways to have multiple images
moving around the screen. You probably already have questions. Like, how
do we "erase" the image before drawing it in a new position? Perhaps
you're still totally lost? Well hopefully the rest of this tutorial can straighten
things out for you.