Sprite Explosions Bitmapped#

Screenshot of using sprites to shoot things
sprite_explosion_bitmapped.py#
  1"""
  2Sprite Explosion
  3
  4Simple program to show how to make explosions with a series of bitmaps.
  5
  6Artwork from https://kenney.nl
  7Explosion graphics from https://www.explosiongenerator.com/
  8
  9If Python and Arcade are installed, this example can be run from the command line with:
 10python -m arcade.examples.sprite_explosion_bitmapped
 11"""
 12import random
 13import arcade
 14
 15SPRITE_SCALING_PLAYER = 0.5
 16SPRITE_SCALING_COIN = 0.2
 17SPRITE_SCALING_LASER = 0.8
 18COIN_COUNT = 50
 19
 20SCREEN_WIDTH = 800
 21SCREEN_HEIGHT = 600
 22SCREEN_TITLE = "Sprite Explosion Example"
 23
 24BULLET_SPEED = 5
 25
 26EXPLOSION_TEXTURE_COUNT = 60
 27
 28
 29class Explosion(arcade.Sprite):
 30    """ This class creates an explosion animation """
 31
 32    def __init__(self, texture_list):
 33        super().__init__(texture_list[0])
 34
 35        # Start at the first frame
 36        self.current_texture = 0
 37        self.textures = texture_list
 38
 39    def update(self):
 40
 41        # Update to the next frame of the animation. If we are at the end
 42        # of our frames, then delete this sprite.
 43        self.current_texture += 1
 44        if self.current_texture < len(self.textures):
 45            self.set_texture(self.current_texture)
 46        else:
 47            self.remove_from_sprite_lists()
 48
 49
 50class MyGame(arcade.Window):
 51    """ Main application class. """
 52
 53    def __init__(self):
 54        """ Initializer """
 55        # Call the parent class initializer
 56        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 57
 58        # Variables that will hold sprite lists
 59        self.player_list = None
 60        self.coin_list = None
 61        self.bullet_list = None
 62        self.explosions_list = None
 63
 64        # Set up the player info
 65        self.player_sprite = None
 66        self.score = 0
 67
 68        # Don't show the mouse cursor
 69        self.set_mouse_visible(False)
 70
 71        # Pre-load the animation frames. We don't do this in the __init__
 72        # of the explosion sprite because it
 73        # takes too long and would cause the game to pause.
 74        self.explosion_texture_list = []
 75
 76        columns = 16
 77        count = 60
 78        sprite_width = 256
 79        sprite_height = 256
 80        file_name = ":resources:images/spritesheets/explosion.png"
 81
 82        # Load the explosions from a sprite sheet
 83        self.explosion_texture_list = arcade.load_spritesheet(file_name, sprite_width, sprite_height, columns, count)
 84
 85        # Load sounds. Sounds from kenney.nl
 86        self.gun_sound = arcade.sound.load_sound(":resources:sounds/laser2.wav")
 87        self.hit_sound = arcade.sound.load_sound(":resources:sounds/explosion2.wav")
 88
 89        arcade.background_color = arcade.color.AMAZON
 90
 91    def setup(self):
 92
 93        """ Set up the game and initialize the variables. """
 94
 95        # Sprite lists
 96        self.player_list = arcade.SpriteList()
 97        self.coin_list = arcade.SpriteList()
 98        self.bullet_list = arcade.SpriteList()
 99        self.explosions_list = arcade.SpriteList()
100
101        # Set up the player
102        self.score = 0
103
104        # Image from kenney.nl
105        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
106                                           scale=SPRITE_SCALING_PLAYER)
107        self.player_sprite.center_x = 50
108        self.player_sprite.center_y = 70
109        self.player_list.append(self.player_sprite)
110
111        # Create the coins
112        for coin_index in range(COIN_COUNT):
113
114            # Create the coin instance
115            # Coin image from kenney.nl
116            coin = arcade.Sprite(":resources:images/items/coinGold.png", scale=SPRITE_SCALING_COIN)
117
118            # Position the coin
119            coin.center_x = random.randrange(SCREEN_WIDTH)
120            coin.center_y = random.randrange(150, SCREEN_HEIGHT)
121
122            # Add the coin to the lists
123            self.coin_list.append(coin)
124
125        # Set the background color
126        self.background_color = arcade.color.AMAZON
127
128    def on_draw(self):
129        """
130        Render the screen.
131        """
132
133        # This command has to happen before we start drawing
134        self.clear()
135
136        # Draw all the sprites.
137        self.coin_list.draw()
138        self.bullet_list.draw()
139        self.player_list.draw()
140        self.explosions_list.draw()
141
142        # Render the text
143        arcade.draw_text(f"Score: {self.score}", 10, 20, arcade.color.WHITE, 14)
144
145    def on_mouse_motion(self, x, y, dx, dy):
146        """
147        Called whenever the mouse moves.
148        """
149        self.player_sprite.center_x = x
150
151    def on_mouse_press(self, x, y, button, modifiers):
152        """
153        Called whenever the mouse button is clicked.
154        """
155
156        # Gunshot sound
157        arcade.sound.play_sound(self.gun_sound)
158
159        # Create a bullet
160        bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png", scale=SPRITE_SCALING_LASER)
161
162        # The image points to the right, and we want it to point up. So
163        # rotate it.
164        bullet.angle = 270
165
166        # Give it a speed
167        bullet.change_y = BULLET_SPEED
168
169        # Position the bullet
170        bullet.center_x = self.player_sprite.center_x
171        bullet.bottom = self.player_sprite.top
172
173        # Add the bullet to the appropriate lists
174        self.bullet_list.append(bullet)
175
176    def on_update(self, delta_time):
177        """ Movement and game logic """
178
179        # Call update on bullet sprites
180        self.bullet_list.update()
181        self.explosions_list.update()
182
183        # Loop through each bullet
184        for bullet in self.bullet_list:
185
186            # Check this bullet to see if it hit a coin
187            hit_list = arcade.check_for_collision_with_list(bullet, self.coin_list)
188
189            # If it did...
190            if len(hit_list) > 0:
191
192                # Make an explosion
193                explosion = Explosion(self.explosion_texture_list)
194
195                # Move it to the location of the coin
196                explosion.center_x = hit_list[0].center_x
197                explosion.center_y = hit_list[0].center_y
198
199                # Call update() because it sets which image we start on
200                explosion.update()
201
202                # Add to a list of sprites that are explosions
203                self.explosions_list.append(explosion)
204
205                # Get rid of the bullet
206                bullet.remove_from_sprite_lists()
207
208            # For every coin we hit, add to the score and remove the coin
209            for coin in hit_list:
210                coin.remove_from_sprite_lists()
211                self.score += 1
212
213                # Hit Sound
214                arcade.sound.play_sound(self.hit_sound)
215
216            # If the bullet flies off-screen, remove it.
217            if bullet.bottom > SCREEN_HEIGHT:
218                bullet.remove_from_sprite_lists()
219
220
221def main():
222    window = MyGame()
223    window.setup()
224    arcade.run()
225
226
227if __name__ == "__main__":
228    main()