Shoot Bullets Upwards#

Screenshot of using sprites to shoot things
sprite_bullets.py#
  1"""
  2Sprite Bullets
  3
  4Simple program to show basic sprite usage.
  5
  6Artwork from https://kenney.nl
  7
  8If Python and Arcade are installed, this example can be run from the command line with:
  9python -m arcade.examples.sprite_bullets
 10"""
 11from __future__ import annotations
 12
 13import random
 14import arcade
 15
 16SPRITE_SCALING_PLAYER = 0.5
 17SPRITE_SCALING_COIN = 0.2
 18SPRITE_SCALING_LASER = 0.8
 19COIN_COUNT = 50
 20
 21SCREEN_WIDTH = 800
 22SCREEN_HEIGHT = 600
 23SCREEN_TITLE = "Sprites and Bullets Example"
 24
 25BULLET_SPEED = 5
 26
 27
 28class MyGame(arcade.Window):
 29    """ Main application class. """
 30
 31    def __init__(self):
 32        """ Initializer """
 33        # Call the parent class initializer
 34        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 35
 36        # Variables that will hold sprite lists
 37        self.player_list = None
 38        self.coin_list = None
 39        self.bullet_list = None
 40
 41        # Set up the player info
 42        self.player_sprite = None
 43        self.score = 0
 44
 45        # Don't show the mouse cursor
 46        self.set_mouse_visible(False)
 47
 48        # Load sounds. Sounds from kenney.nl
 49        self.gun_sound = arcade.load_sound(":resources:sounds/hurt5.wav")
 50        self.hit_sound = arcade.load_sound(":resources:sounds/hit5.wav")
 51
 52        self.background_color = arcade.color.AMAZON
 53
 54    def setup(self):
 55
 56        """ Set up the game and initialize the variables. """
 57
 58        # Sprite lists
 59        self.player_list = arcade.SpriteList()
 60        self.coin_list = arcade.SpriteList()
 61        self.bullet_list = arcade.SpriteList()
 62
 63        # Set up the player
 64        self.score = 0
 65
 66        # Image from kenney.nl
 67        self.player_sprite = arcade.Sprite(
 68            ":resources:images/animated_characters/female_person/femalePerson_idle.png",
 69            scale=SPRITE_SCALING_PLAYER)
 70        self.player_sprite.center_x = 50
 71        self.player_sprite.center_y = 70
 72        self.player_list.append(self.player_sprite)
 73
 74        # Create the coins
 75        for i in range(COIN_COUNT):
 76
 77            # Create the coin instance
 78            # Coin image from kenney.nl
 79            coin = arcade.Sprite(":resources:images/items/coinGold.png", scale=SPRITE_SCALING_COIN)
 80
 81            # Position the coin
 82            coin.center_x = random.randrange(SCREEN_WIDTH)
 83            coin.center_y = random.randrange(120, SCREEN_HEIGHT)
 84
 85            # Add the coin to the lists
 86            self.coin_list.append(coin)
 87
 88        # Set the background color
 89        self.background_color = arcade.color.AMAZON
 90
 91    def on_draw(self):
 92        """
 93        Render the screen.
 94        """
 95
 96        # This command has to happen before we start drawing
 97        self.clear()
 98
 99        # Draw all the sprites.
100        self.coin_list.draw()
101        self.bullet_list.draw()
102        self.player_list.draw()
103
104        # Render the text
105        arcade.draw_text(f"Score: {self.score}", 10, 20, arcade.color.WHITE, 14)
106
107    def on_mouse_motion(self, x, y, dx, dy):
108        """
109        Called whenever the mouse moves.
110        """
111        self.player_sprite.center_x = x
112
113    def on_mouse_press(self, x, y, button, modifiers):
114        """
115        Called whenever the mouse button is clicked.
116        """
117        # Gunshot sound
118        arcade.play_sound(self.gun_sound)
119        # Create a bullet
120        bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png", scale=SPRITE_SCALING_LASER)
121
122        # The image points to the right, and we want it to point up. So
123        # rotate it.
124        bullet.angle = -90
125
126        # Give the bullet a speed
127        bullet.change_y = BULLET_SPEED
128
129        # Position the bullet
130        bullet.center_x = self.player_sprite.center_x
131        bullet.bottom = self.player_sprite.top
132
133        # Add the bullet to the appropriate lists
134        self.bullet_list.append(bullet)
135
136    def on_update(self, delta_time):
137        """ Movement and game logic """
138
139        # Call update on bullet sprites
140        self.bullet_list.update()
141
142        # Loop through each bullet
143        for bullet in self.bullet_list:
144
145            # Check this bullet to see if it hit a coin
146            hit_list = arcade.check_for_collision_with_list(bullet, self.coin_list)
147
148            # If it did, get rid of the bullet
149            if len(hit_list) > 0:
150                bullet.remove_from_sprite_lists()
151
152            # For every coin we hit, add to the score and remove the coin
153            for coin in hit_list:
154                coin.remove_from_sprite_lists()
155                self.score += 1
156
157                # Hit Sound
158                arcade.play_sound(self.hit_sound)
159
160            # If the bullet flies off-screen, remove it.
161            if bullet.bottom > SCREEN_HEIGHT:
162                bullet.remove_from_sprite_lists()
163
164
165def main():
166    window = MyGame()
167    window.setup()
168    arcade.run()
169
170
171if __name__ == "__main__":
172    main()