Aim and Shoot Bullets

Screenshot of using sprites to shoot things
sprite_bullets_aimed.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_aimed
 10"""
 11
 12import random
 13import arcade
 14import math
 15import os
 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 = "Sprites and Bullets Aimed Example"
 25
 26BULLET_SPEED = 5
 27
 28window = None
 29
 30
 31class MyGame(arcade.Window):
 32    """ Main application class. """
 33
 34    def __init__(self):
 35        """ Initializer """
 36        # Call the parent class initializer
 37        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 38
 39        # Set the working directory (where we expect to find files) to the same
 40        # directory this .py file is in. You can leave this out of your own
 41        # code, but it is needed to easily run the examples using "python -m"
 42        # as mentioned at the top of this program.
 43        file_path = os.path.dirname(os.path.abspath(__file__))
 44        os.chdir(file_path)
 45
 46        # Variables that will hold sprite lists
 47        self.player_list = None
 48        self.coin_list = None
 49        self.bullet_list = None
 50
 51        # Set up the player info
 52        self.player_sprite = None
 53        self.score = 0
 54        self.score_text = None
 55
 56        # Load sounds. Sounds from kenney.nl
 57        self.gun_sound = arcade.sound.load_sound(":resources:sounds/laser1.wav")
 58        self.hit_sound = arcade.sound.load_sound(":resources:sounds/phaseJump1.wav")
 59
 60        arcade.set_background_color(arcade.color.AMAZON)
 61
 62    def setup(self):
 63
 64        """ Set up the game and initialize the variables. """
 65
 66        # Sprite lists
 67        self.player_list = arcade.SpriteList()
 68        self.coin_list = arcade.SpriteList()
 69        self.bullet_list = arcade.SpriteList()
 70
 71        # Set up the player
 72        self.score = 0
 73
 74        # Image from kenney.nl
 75        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING_PLAYER)
 76        self.player_sprite.center_x = 50
 77        self.player_sprite.center_y = 70
 78        self.player_list.append(self.player_sprite)
 79
 80        # Create the coins
 81        for i in range(COIN_COUNT):
 82
 83            # Create the coin instance
 84            # Coin image from kenney.nl
 85            coin = arcade.Sprite(":resources:images/items/coinGold.png", SPRITE_SCALING_COIN)
 86
 87            # Position the coin
 88            coin.center_x = random.randrange(SCREEN_WIDTH)
 89            coin.center_y = random.randrange(120, SCREEN_HEIGHT)
 90
 91            # Add the coin to the lists
 92            self.coin_list.append(coin)
 93
 94        # Set the background color
 95        arcade.set_background_color(arcade.color.AMAZON)
 96
 97    def on_draw(self):
 98        """ Render the screen. """
 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        # Put the text on the screen.
109        output = f"Score: {self.score}"
110        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
111
112    def on_mouse_press(self, x, y, button, modifiers):
113        """ Called whenever the mouse button is clicked. """
114
115        # Create a bullet
116        bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png", SPRITE_SCALING_LASER)
117
118        # Position the bullet at the player's current location
119        start_x = self.player_sprite.center_x
120        start_y = self.player_sprite.center_y
121        bullet.center_x = start_x
122        bullet.center_y = start_y
123
124        # Get from the mouse the destination location for the bullet
125        # IMPORTANT! If you have a scrolling screen, you will also need
126        # to add in self.view_bottom and self.view_left.
127        dest_x = x
128        dest_y = y
129
130        # Do math to calculate how to get the bullet to the destination.
131        # Calculation the angle in radians between the start points
132        # and end points. This is the angle the bullet will travel.
133        x_diff = dest_x - start_x
134        y_diff = dest_y - start_y
135        angle = math.atan2(y_diff, x_diff)
136
137        # Angle the bullet sprite so it doesn't look like it is flying
138        # sideways.
139        bullet.angle = math.degrees(angle)
140        print(f"Bullet angle: {bullet.angle:.2f}")
141
142        # Taking into account the angle, calculate our change_x
143        # and change_y. Velocity is how fast the bullet travels.
144        bullet.change_x = math.cos(angle) * BULLET_SPEED
145        bullet.change_y = math.sin(angle) * BULLET_SPEED
146
147        # Add the bullet to the appropriate lists
148        self.bullet_list.append(bullet)
149
150    def on_update(self, delta_time):
151        """ Movement and game logic """
152
153        # Call update on all sprites
154        self.bullet_list.update()
155
156        # Loop through each bullet
157        for bullet in self.bullet_list:
158
159            # Check this bullet to see if it hit a coin
160            hit_list = arcade.check_for_collision_with_list(bullet, self.coin_list)
161
162            # If it did, get rid of the bullet
163            if len(hit_list) > 0:
164                bullet.remove_from_sprite_lists()
165
166            # For every coin we hit, add to the score and remove the coin
167            for coin in hit_list:
168                coin.remove_from_sprite_lists()
169                self.score += 1
170
171            # If the bullet flies off-screen, remove it.
172            if bullet.bottom > self.width or bullet.top < 0 or bullet.right < 0 or bullet.left > self.width:
173                bullet.remove_from_sprite_lists()
174
175
176def main():
177    game = MyGame()
178    game.setup()
179    arcade.run()
180
181
182if __name__ == "__main__":
183    main()