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.5
 15SPRITE_SCALING_COIN = 0.2
 16SPRITE_SCALING_LASER = 0.8
 17COIN_COUNT = 50
 18
 19SCREEN_WIDTH = 800
 20SCREEN_HEIGHT = 600
 21SCREEN_TITLE = "Sprites and Bullets Example"
 22
 23BULLET_SPEED = 5
 24
 25
 26class MyGame(arcade.Window):
 27    """ Main application class. """
 28
 29    def __init__(self):
 30        """ Initializer """
 31        # Call the parent class initializer
 32        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 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.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(":resources:images/items/coinGold.png", scale=SPRITE_SCALING_COIN)
 78
 79            # Position the coin
 80            coin.center_x = random.randrange(SCREEN_WIDTH)
 81            coin.center_y = random.randrange(120, SCREEN_HEIGHT)
 82
 83            # Add the coin to the lists
 84            self.coin_list.append(coin)
 85
 86        # Set the background color
 87        self.background_color = arcade.color.AMAZON
 88
 89    def on_draw(self):
 90        """
 91        Render the screen.
 92        """
 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        # Render the text
103        arcade.draw_text(f"Score: {self.score}", 10, 20, arcade.color.WHITE, 14)
104
105    def on_mouse_motion(self, x, y, dx, dy):
106        """
107        Called whenever the mouse moves.
108        """
109        self.player_sprite.center_x = x
110
111    def on_mouse_press(self, x, y, button, modifiers):
112        """
113        Called whenever the mouse button is clicked.
114        """
115        # Gunshot sound
116        arcade.play_sound(self.gun_sound)
117        # Create a bullet
118        bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png", scale=SPRITE_SCALING_LASER)
119
120        # The image points to the right, and we want it to point up. So
121        # rotate it.
122        bullet.angle = -90
123
124        # Give the bullet a speed
125        bullet.change_y = BULLET_SPEED
126
127        # Position the bullet
128        bullet.center_x = self.player_sprite.center_x
129        bullet.bottom = self.player_sprite.top
130
131        # Add the bullet to the appropriate lists
132        self.bullet_list.append(bullet)
133
134    def on_update(self, delta_time):
135        """ Movement and game logic """
136
137        # Call update on bullet sprites
138        self.bullet_list.update()
139
140        # Loop through each bullet
141        for bullet in self.bullet_list:
142
143            # Check this bullet to see if it hit a coin
144            hit_list = arcade.check_for_collision_with_list(bullet, self.coin_list)
145
146            # If it did, get rid of the bullet
147            if len(hit_list) > 0:
148                bullet.remove_from_sprite_lists()
149
150            # For every coin we hit, add to the score and remove the coin
151            for coin in hit_list:
152                coin.remove_from_sprite_lists()
153                self.score += 1
154
155                # Hit Sound
156                arcade.play_sound(self.hit_sound)
157
158            # If the bullet flies off-screen, remove it.
159            if bullet.bottom > SCREEN_HEIGHT:
160                bullet.remove_from_sprite_lists()
161
162
163def main():
164    window = MyGame()
165    window.setup()
166    arcade.run()
167
168
169if __name__ == "__main__":
170    main()