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 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_aimed
 10"""
 11
 12import random
 13import arcade
 14import math
 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 Aimed Example"
 24
 25BULLET_SPEED = 5
 26
 27window = None
 28
 29
 30class MyGame(arcade.Window):
 31    """ Main application class. """
 32
 33    def __init__(self):
 34        """ Initializer """
 35        # Call the parent class initializer
 36        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 37
 38        # Variables that will hold sprite lists
 39        self.player_list = None
 40        self.coin_list = None
 41        self.bullet_list = None
 42
 43        # Set up the player info
 44        self.player_sprite = None
 45        self.score = 0
 46        self.score_text = None
 47
 48        # Load sounds. Sounds from kenney.nl
 49        self.gun_sound = arcade.sound.load_sound(":resources:sounds/laser1.wav")
 50        self.hit_sound = arcade.sound.load_sound(":resources:sounds/phaseJump1.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        """ Render the screen. """
 93
 94        # This command has to happen before we start drawing
 95        self.clear()
 96
 97        # Draw all the sprites.
 98        self.coin_list.draw()
 99        self.bullet_list.draw()
100        self.player_list.draw()
101
102        # Put the text on the screen.
103        output = f"Score: {self.score}"
104        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
105
106    def on_mouse_press(self, x, y, button, modifiers):
107        """ Called whenever the mouse button is clicked. """
108
109        # Create a bullet
110        bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png", scale=SPRITE_SCALING_LASER)
111
112        # Position the bullet at the player's current location
113        start_x = self.player_sprite.center_x
114        start_y = self.player_sprite.center_y
115        bullet.center_x = start_x
116        bullet.center_y = start_y
117
118        # Get from the mouse the destination location for the bullet
119        # IMPORTANT! If you have a scrolling screen, you will also need
120        # to add in self.view_bottom and self.view_left.
121        dest_x = x
122        dest_y = y
123
124        # Do math to calculate how to get the bullet to the destination.
125        # Calculation the angle in radians between the start points
126        # and end points. This is the angle the bullet will travel.
127        x_diff = dest_x - start_x
128        y_diff = dest_y - start_y
129        angle = math.atan2(y_diff, x_diff)
130
131        # Angle the bullet sprite so it doesn't look like it is flying
132        # sideways.
133        bullet.angle = math.degrees(angle)
134        print(f"Bullet angle: {bullet.angle:.2f}")
135
136        # Taking into account the angle, calculate our change_x
137        # and change_y. Velocity is how fast the bullet travels.
138        bullet.change_x = math.cos(angle) * BULLET_SPEED
139        bullet.change_y = math.sin(angle) * BULLET_SPEED
140
141        # Add the bullet to the appropriate lists
142        self.bullet_list.append(bullet)
143
144    def on_update(self, delta_time):
145        """ Movement and game logic """
146
147        # Call update on all sprites
148        self.bullet_list.update()
149
150        # Loop through each bullet
151        for bullet in self.bullet_list:
152
153            # Check this bullet to see if it hit a coin
154            hit_list = arcade.check_for_collision_with_list(bullet, self.coin_list)
155
156            # If it did, get rid of the bullet
157            if len(hit_list) > 0:
158                bullet.remove_from_sprite_lists()
159
160            # For every coin we hit, add to the score and remove the coin
161            for coin in hit_list:
162                coin.remove_from_sprite_lists()
163                self.score += 1
164
165            # If the bullet flies off-screen, remove it.
166            if bullet.bottom > self.width or bullet.top < 0 or bullet.right < 0 or bullet.left > self.width:
167                bullet.remove_from_sprite_lists()
168
169
170def main():
171    game = MyGame()
172    game.setup()
173    arcade.run()
174
175
176if __name__ == "__main__":
177    main()