Game Design: Explosions
Game Design: Explosions
Sometimes when you shoot something it would be a cool effect if it would explode. We can do this by running through a series of explosion image sprites. We will be using the images in Images/explosions which you forked back in Ch. 15. If you would like other explosion effects these were downloaded from http://www.explosiongenerator.com/.
We're going to go ahead and add a couple of sounds to our game as well to make it more legit. We will be using laser.mp3 and explosion.mp3 which you also forked back in Ch. 15.
Duplicate your BB8 Shooter Game and call it BB8 Explosion Game. We will be updating that code as you go through this chapter.
#12: If you look in the Images/explosions folder you will see 50 images starting with explosion0000.png. We will be iterating through these images to create an explosion animation. On line 12 we made a constant equal to the last numbered image in the folder.
#14: Start an Explosion() Class.
#15: Pass in a variable called texture_list which will hold all of the image references. The reason we don't go ahead and make that list here is because the list would be generated each time the Explosion() Class is called and that would be redundant and slows down our game.
#16: The image of the Explosion sprite should start with the first image in the explosions folder.
#19-20: Set a current_texture variable to zero and a textures variable to the texture_list.
#21: Load the explosion sound.
In the update() method of the Explosion() Class we want to do the following:
#26: Increment the current_texture variable by 1.
#27-#28: Check if the current_texture variable is less that the length of the textures list. If it is, change the image of the explosion sprite to the next image in the folder. We do this by using self.set_texture(self.current_texture)
#29-30: If it's not less, this means we have cycled through all of the images and we should kill the sprite.
#35: Load the laser.mp3 sound when you initialize BB8
In the initialization of our MyGame() Class we will create our explosions textures list.
#81: Create an empty list called explosion_texture_list
#82: Use a for loop with variable i through a range of how many images are in our explosion folder.
#83: Set a variable texture_name to the pathway to the image using the i value in the name. We use f to format the number variable in the string. Then we use {i:04} inside the string. If you remember the formatting section this will format i into 4 digits with leading zeros which is how the images are named in the folder.
#84: Append to the explosion_texture_list the load_texture of each successive image.
#95: In the reset() method create the explosions SpriteList.
#119: In the on_draw() method draw the explosions SpriteList.
#136: In the on_update() method update the explosions SpriteList.
We want to create an explosion when a bullet hits a trooper. We will do this in the on_update() section where we loop through each bullet.
#153: If you remember, this line of code checks to see if a bullet just hit a trooper.
#154: If it has, instantiate an explosion object and send the explosion_texture_list parameter to the class.
#155-156: Set the center_x and the center_y to the center_x and center_y of the recently killed trooper which can be designated as hit_list[0].
#157: Append the explosion to the explosions sprite list.
#158: Play the explosion sound.
#182: In the section where the SPACE key is pressed play the sound of the bullet being fired.
If you got the explosions and the sounds working, let's make the game a little more challenging and have the troopers randomly drop bombs. Hopefully you could do this on your own but if not below are the additions to the code that you should make.
#36: Load the explosion sound in the Player Class()
#58: Create an Enemy_Bullet Class().
#60: Initialize with rbullet.png
#63-64: Traveling down and pointing down
#65-66: Kill the bullet if it goes beneath the bottom of the screen.
#161: Add ebullets SpriteList to reset() section
#129: Draw ebullets in on_draw() section
#147: Update ebullets in on_update() section
In the on_update() section add the following:
#161: Loop through every trooper in the trooper_list
#162: Use an if statement to get a 1 in 800 chance that a trooper drops a bomb. This is 1 in 800 per frame so it's actually more frequent than you would think. You can play with this number later. Also don't drop bombs if the game is already over.
#163-166: Instantiate an ebullet and align it's center_x with the trooper's center_x. Also align the top of the bullet with the bottom of the trooper. Add the ebullet to the ebullets SpriteList.
Also in the on_update() section add the following:
#169: Create a collision list called bb8_bombed.
#170: Check if bb8_bombed is greater than zero which means BB8 has been struck by a bullet.
#171-174: If it has, play the explosion sound, kill BB8, kill the bullet and set Gameover to True.
Finally, in the main function, we can change the game title to "BB8 Explosions".
When you get this game to work, demonstrate it to your instructor. Feel free to implement any new knowledge into your game project.