Millenium Falcon explosions, cartoon style. Thanks to OpenCV.

There is something magical about animation. Seeing each still image assembled on top of one another with ever so slight variations to create the illusion of movement. And then playing those animations at higher speeds to create a moving picture. I still remember my first game was just a square that could move, but the first time I made it move I knew I loved game making and programming.

Today, let’s keep our game making going and talk about how to animate sprites. We will use individual images for this tutorial, and in a later tutorial, I will show how to do sprite sheet animation.

By the end of this tutorial, you will understand how sprite animation works in Pygame and be able to animate sprites in a space shooter to show explosions and enemy ship actions. 

CODE FOR THIS TUTORIAL CAN BE FOUND ON GITHUB.

Before we begin, the sprites used in this tutorial are from kisspng.com, or from a quick Google search for explosion sprite sheets.

What is Sprite Animation?

The sprites that we have been using in our space shooter, the ships, asteroids and others, are the 2D graphics that we can see and move around within the larger window. Before we only showed animation or movement by updating the location a specified number of pixels. For example, when we press the right arrow key, the ship is updated 8 pixels in the right direction. But this doesn’t really change the look of our ship as we move, only the location within the window.

If we want to show animation, what we need is a set of images (often using a spritesheet) and then a way to play those images on top of each other in rapid succession to give us the illusion that the object is really moving. Each individual image, or sprite, is a frame and we will need to display each frame in a continuous movement to create a loop.

In order to create the animation, we will need to create a loop to:

  1. Load the image,
  2. Update the image to each frame,
  3. Draw the frame to the window.

Loading images

We will use separate explosion animations for when the asteroids explodes and for when our ship explodes.


Let’s load the images for our types of explosions.

We have three different situations in which we will need to animate explosions (when an asteroid explodes (large), when an asteroid hits our ship (small), and when our ship or enemy ships explode (ship)). In line 2, we create a dictionary called explosion_anim, and in lines 3 – 5 create the keys. The keys will be used later when we are deciding what kind of explosion animation we need to display.

For the large and small explosions, there are 5 images that we need to cycle through. We locate the images based on their file names in line 7, then load them. Finally, we rescale the image sizes depending upon if they are large or not in lines 10 – 13.

The following code loads the explosion for the player’s and enemy ships. There are 10 images for the player explosion, so we need to load all 10 images using the for loop in line 1.

Explosion class and Updating images

Creating the Explosion class is similar to other classes we made in previous tutorials.

Our Explosion class takes three arguments, the location of the explosion which is the center of the object that just exploded, the type of explosion, ex_type, and the list for the explosion animation. It’s important to note that we set the first frame to be the first image, and that we will cycle through the images by a specified frame_rate.

In the update function, we check the current game time, and if the current_time minus the last frame that was displayed is greater than our frame_rate, we update the frame number, check if it is the last frame in line 20, and if it’s not, continue to update the frames based upon the type of explosion and the image number.

Collision detection and Drawing frames

In the collision detection tutorial, our ships or asteroids would disappear when hit, but nothing else happened. In the collision detection loops we are going to create the explosions using our Explosion class and then add them to our all_active_sprites list to be updated and drawn in the window.

Below are the following bits of code needed to add to display the explosions.

Explosion when enemy ships are hit by player bullets. Notice in line 6 the ‘ship’¬†argument is passed to the Explosion class.

Explosion when asteroids are hit by player bullets. ‘large’ argument in line 6.

Explosion when asteroids hit the player’s ship.

Explosion when enemy ships hit the player’s ship.

Later, we will also add explosions if the player is hit by enemy lasers, but we will need to create a shield and health for the player first.

Boost Animation

One last animation we will discuss for our game is when the enemies perform their dive bomb attacks. Just having them speed off towards our player with out firing some sort of boosters looks strange. So we want to correct this by adding some animation when this occurs.

First, we create a boost_anim dictionary similar to our explosions, load the images, and rescale them.

We then create the Boost class, which is very similar to the Explosion class, except that the frame_rate value is 35.

Update the EnemyShip class parameters to take the boost_anim argument. We want to use the update method of the EnemyShip class to check when the player dive bombs and then draw the boost animation when a ship does so.

Add Boost animation to EnemyShip class in divebomb method.

And that’s it! We can finally see things blow up! Below is what our game looks like with explosions and the boost animation.

Finally some explosions!

Summary

We looked at what sprite animation is and how to use Pygame to show animations in our game, showing explosions and a boost animation to make our game more realistic. Next time, we will add a game display to our window showing our health, score and lives.

I also really want to say thank you for taking the time to read and to learn from the tutorials I write. If you ever have any suggestions or questions, please let me know.

CODE FOR THIS TUTORIAL CAN BE FOUND ON GITHUB.

Leave a Reply

Your email address will not be published. Required fields are marked *