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