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