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