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
 20WINDOW_WIDTH = 1280
 21WINDOW_HEIGHT = 720
 22WINDOW_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        # How long the explosion has been around.
 35        self.time_elapsed = 0
 36
 37        # Start at the first frame
 38        self.current_texture = 0
 39        self.textures = texture_list
 40
 41    def update(self, delta_time=1 / 60):
 42        self.time_elapsed += delta_time
 43        # Update to the next frame of the animation. If we are at the end
 44        # of our frames, then delete this sprite.
 45        self.current_texture = int(self.time_elapsed * 60)
 46        if self.current_texture < len(self.textures):
 47            self.set_texture(self.current_texture)
 48        else:
 49            self.remove_from_sprite_lists()
 50
 51
 52class GameView(arcade.View):
 53    """ Main application class. """
 54
 55    def __init__(self):
 56        """ Initializer """
 57        # Call the parent class initializer
 58        super().__init__()
 59
 60        # Variables that will hold sprite lists
 61        self.player_list = arcade.SpriteList()
 62        self.enemy_list = arcade.SpriteList()
 63        self.bullet_list = arcade.SpriteList()
 64        self.explosions_list = arcade.SpriteList()
 65
 66        # Set up the player info
 67        # Image from kenney.nl
 68        self.player_sprite = arcade.Sprite(
 69            ":resources:images/animated_characters/female_person/femalePerson_idle.png",
 70            scale=SPRITE_SCALING_PLAYER,
 71        )
 72        self.player_sprite.center_x = 50
 73        self.player_sprite.center_y = 70
 74        self.player_list.append(self.player_sprite)
 75
 76        # Player score
 77        self.score = 0
 78
 79        # Don't show the mouse cursor
 80        self.window.set_mouse_visible(False)
 81
 82        # Pre-load the animation frames. We don't do this in the __init__
 83        # of the explosion sprite because it
 84        # takes too long and would cause the game to pause.
 85        self.explosion_texture_list = []
 86
 87        # Load the explosion from a sprite sheet
 88        columns = 16
 89        count = 60
 90        sprite_width = 256
 91        sprite_height = 256
 92        file_name = ":resources:images/spritesheets/explosion.png"
 93
 94        # Load the explosions from a sprite sheet
 95        spritesheet = arcade.load_spritesheet(file_name)
 96        self.explosion_texture_list = spritesheet.get_texture_grid(
 97            size=(sprite_width, sprite_height),
 98            columns=columns,
 99            count=count,
100        )
101
102        # Load sounds. Sounds from kenney.nl
103        self.gun_sound = arcade.sound.load_sound(":resources:sounds/laser2.wav")
104        self.hit_sound = arcade.sound.load_sound(":resources:sounds/explosion2.wav")
105
106        self.background_color = arcade.color.AMAZON
107        self.spawn_enemies()
108
109    def reset(self):
110        """Restart the game."""
111
112        # Clear out the sprite lists
113        self.enemy_list.clear()
114        self.bullet_list.clear()
115        self.explosions_list.clear()
116
117        # Reset the score
118        self.score = 0
119
120        self.spawn_enemies()
121
122    def spawn_enemies(self):
123        for coin_index in range(COIN_COUNT):
124            # Create the enemy instance. Image from kenney.nl
125            coin = arcade.Sprite(
126                ":resources:images/items/coinGold.png",
127                scale=SPRITE_SCALING_COIN,
128                center_x=random.randrange(25, WINDOW_WIDTH - 25),
129                center_y=random.randrange(150, WINDOW_HEIGHT),
130            )
131            # Add the coin to enemy list
132            self.enemy_list.append(coin)
133
134    def on_draw(self):
135        """
136        Render the screen.
137        """
138        # This command has to happen before we start drawing
139        self.clear()
140
141        # Draw all the sprites.
142        self.enemy_list.draw()
143        self.bullet_list.draw()
144        self.player_list.draw()
145        self.explosions_list.draw()
146
147        # Render the text
148        arcade.draw_text(f"Score: {self.score}", 10, 20, arcade.color.WHITE, 14)
149
150    def on_mouse_motion(self, x, y, dx, dy):
151        """
152        Called whenever the mouse moves.
153        """
154        self.player_sprite.center_x = x
155
156    def on_mouse_press(self, x, y, button, modifiers):
157        """
158        Called whenever the mouse button is clicked.
159        """
160        # Gunshot sound
161        arcade.sound.play_sound(self.gun_sound)
162
163        # Create a bullet
164        bullet = arcade.Sprite(
165            ":resources:images/space_shooter/laserBlue01.png",
166            scale=SPRITE_SCALING_LASER,
167        )
168
169        # The image points to the right, and we want it to point up. So
170        # rotate it.
171        bullet.angle = 270
172
173        # Give it a speed
174        bullet.change_y = BULLET_SPEED
175
176        # Position the bullet
177        bullet.center_x = self.player_sprite.center_x
178        bullet.bottom = self.player_sprite.top
179
180        # Add the bullet to the appropriate lists
181        self.bullet_list.append(bullet)
182
183    def on_key_press(self, symbol: int, modifiers: int):
184        if symbol == arcade.key.R:
185            self.reset()
186        # Close the window
187        elif symbol == arcade.key.ESCAPE:
188            self.window.close()
189
190    def on_update(self, delta_time):
191        """Movement and game logic"""
192
193        # Call update on bullet sprites
194        self.bullet_list.update()
195        self.explosions_list.update()
196
197        # Loop through each bullet
198        for bullet in self.bullet_list:
199
200            # Check this bullet to see if it hit a coin
201            hit_list = arcade.check_for_collision_with_list(bullet, self.enemy_list)
202
203            # If it did...
204            if len(hit_list) > 0:
205
206                # Make an explosion
207                explosion = Explosion(self.explosion_texture_list)
208
209                # Move it to the location of the coin
210                explosion.center_x = hit_list[0].center_x
211                explosion.center_y = hit_list[0].center_y
212
213                # Call update() because it sets which image we start on
214                explosion.update()
215
216                # Add to a list of sprites that are explosions
217                self.explosions_list.append(explosion)
218
219                # Get rid of the bullet
220                bullet.remove_from_sprite_lists()
221
222            # For every coin we hit, add to the score and remove the coin
223            for coin in hit_list:
224                coin.remove_from_sprite_lists()
225                self.score += 1
226
227                # Hit Sound
228                arcade.sound.play_sound(self.hit_sound)
229
230            # If the bullet flies off-screen, remove it.
231            if bullet.bottom > WINDOW_HEIGHT:
232                bullet.remove_from_sprite_lists()
233
234
235
236def main():
237    """ Main function """
238    # Create a window class. This is what actually shows up on screen
239    window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
240
241    # Create the GameView
242    game = GameView()
243
244    # Show GameView on screen
245    window.show_view(game)
246
247    # Start the arcade game loop
248    arcade.run()
249
250if __name__ == "__main__":
251    main()