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 http://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
 13import os
 14
 15SPRITE_SCALING_PLAYER = 0.5
 16SPRITE_SCALING_COIN = 0.2
 17SPRITE_SCALING_LASER = 0.8
 18COIN_COUNT = 50
 19
 20SCREEN_WIDTH = 800
 21SCREEN_HEIGHT = 600
 22SCREEN_TITLE = "Sprites and Bullets Example"
 23
 24BULLET_SPEED = 5
 25
 26
 27class MyGame(arcade.Window):
 28    """ Main application class. """
 29
 30    def __init__(self):
 31        """ Initializer """
 32        # Call the parent class initializer
 33        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 34
 35        # Set the working directory (where we expect to find files) to the same
 36        # directory this .py file is in. You can leave this out of your own
 37        # code, but it is needed to easily run the examples using "python -m"
 38        # as mentioned at the top of this program.
 39        file_path = os.path.dirname(os.path.abspath(__file__))
 40        os.chdir(file_path)
 41
 42        # Variables that will hold sprite lists
 43        self.player_list = None
 44        self.coin_list = None
 45        self.bullet_list = None
 46
 47        # Set up the player info
 48        self.player_sprite = None
 49        self.score = 0
 50
 51        # Don't show the mouse cursor
 52        self.set_mouse_visible(False)
 53
 54        # Load sounds. Sounds from kenney.nl
 55        self.gun_sound = arcade.load_sound(":resources:sounds/hurt5.wav")
 56        self.hit_sound = arcade.load_sound(":resources:sounds/hit5.wav")
 57
 58        arcade.set_background_color(arcade.color.AMAZON)
 59
 60    def setup(self):
 61
 62        """ Set up the game and initialize the variables. """
 63
 64        # Sprite lists
 65        self.player_list = arcade.SpriteList()
 66        self.coin_list = arcade.SpriteList()
 67        self.bullet_list = arcade.SpriteList()
 68
 69        # Set up the player
 70        self.score = 0
 71
 72        # Image from kenney.nl
 73        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING_PLAYER)
 74        self.player_sprite.center_x = 50
 75        self.player_sprite.center_y = 70
 76        self.player_list.append(self.player_sprite)
 77
 78        # Create the coins
 79        for i in range(COIN_COUNT):
 80
 81            # Create the coin instance
 82            # Coin image from kenney.nl
 83            coin = arcade.Sprite(":resources:images/items/coinGold.png", SPRITE_SCALING_COIN)
 84
 85            # Position the coin
 86            coin.center_x = random.randrange(SCREEN_WIDTH)
 87            coin.center_y = random.randrange(120, SCREEN_HEIGHT)
 88
 89            # Add the coin to the lists
 90            self.coin_list.append(coin)
 91
 92        # Set the background color
 93        arcade.set_background_color(arcade.color.AMAZON)
 94
 95    def on_draw(self):
 96        """
 97        Render the screen.
 98        """
 99
100        # This command has to happen before we start drawing
101        arcade.start_render()
102
103        # Draw all the sprites.
104        self.coin_list.draw()
105        self.bullet_list.draw()
106        self.player_list.draw()
107
108        # Render the text
109        arcade.draw_text(f"Score: {self.score}", 10, 20, arcade.color.WHITE, 14)
110
111    def on_mouse_motion(self, x, y, dx, dy):
112        """
113        Called whenever the mouse moves.
114        """
115        self.player_sprite.center_x = x
116
117    def on_mouse_press(self, x, y, button, modifiers):
118        """
119        Called whenever the mouse button is clicked.
120        """
121        # Gunshot sound
122        arcade.play_sound(self.gun_sound)
123        # Create a bullet
124        bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png", SPRITE_SCALING_LASER)
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 > SCREEN_HEIGHT:
166                bullet.remove_from_sprite_lists()
167
168
169def main():
170    window = MyGame()
171    window.setup()
172    arcade.run()
173
174
175if __name__ == "__main__":
176    main()