Introduction
Pygame library is one of the python programming libraries with very efficient functionalities such as building custom events, taking input from the user through the keyboard, creating custom input boxes for inputs, etc. This article will discuss the pygame blit() function and how to apply animations of different styles in the Pygame application. I hope you will enjoy the article!
Animation in Pygame
Animation is a simple sequence of images that are displayed too fast. To get animation in our application, we need to follow the below procedure:
Simple Animation:
- Load the images into a list using pygame.image.load()
- During each time of update, we need to change the current image to the next one.
- If our counter reaches the end of the list, we need to reset the counter to the beginning.
- Do it as fast enough to make it look smooth.
Example: The pseudocode looks like this:
playing, current = 0, 0
frames = [list of images]
image = frames[0]
rect = image.get_rect()
def update(args):
if playing: #update the animation if it is playing
current+=1
if current == len(frames): #if images in frames completed, restart the process.
current = 0
image = frames[current]
rect = image.get_rect(center = rect.center)
You can follow this link to get the original code.
We can update each frame for the animation by using the lines:
current +=1
if current == len(frames):
current = 0
Or
current +=1
current %= len(frames)
Types of animations we can perform in our pygame application:
- Looping
- Forward
- Reverse
- ping-pong
You can learn these animations in detail through our later articles.