Shoot Bullets Upwards#
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 arcade.set_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(":resources:images/animated_characters/female_person/"
66 "femalePerson_idle.png", SPRITE_SCALING_PLAYER)
67 self.player_sprite.center_x = 50
68 self.player_sprite.center_y = 70
69 self.player_list.append(self.player_sprite)
70
71 # Create the coins
72 for i in range(COIN_COUNT):
73
74 # Create the coin instance
75 # Coin image from kenney.nl
76 coin = arcade.Sprite(":resources:images/items/coinGold.png", SPRITE_SCALING_COIN)
77
78 # Position the coin
79 coin.center_x = random.randrange(SCREEN_WIDTH)
80 coin.center_y = random.randrange(120, SCREEN_HEIGHT)
81
82 # Add the coin to the lists
83 self.coin_list.append(coin)
84
85 # Set the background color
86 arcade.set_background_color(arcade.color.AMAZON)
87
88 def on_draw(self):
89 """
90 Render the screen.
91 """
92
93 # This command has to happen before we start drawing
94 self.clear()
95
96 # Draw all the sprites.
97 self.coin_list.draw()
98 self.bullet_list.draw()
99 self.player_list.draw()
100
101 # Render the text
102 arcade.draw_text(f"Score: {self.score}", 10, 20, arcade.color.WHITE, 14)
103
104 def on_mouse_motion(self, x, y, dx, dy):
105 """
106 Called whenever the mouse moves.
107 """
108 self.player_sprite.center_x = x
109
110 def on_mouse_press(self, x, y, button, modifiers):
111 """
112 Called whenever the mouse button is clicked.
113 """
114 # Gunshot sound
115 arcade.play_sound(self.gun_sound)
116 # Create a bullet
117 bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png", SPRITE_SCALING_LASER)
118
119 # The image points to the right, and we want it to point up. So
120 # rotate it.
121 bullet.angle = 90
122
123 # Give the bullet a speed
124 bullet.change_y = BULLET_SPEED
125
126 # Position the bullet
127 bullet.center_x = self.player_sprite.center_x
128 bullet.bottom = self.player_sprite.top
129
130 # Add the bullet to the appropriate lists
131 self.bullet_list.append(bullet)
132
133 def on_update(self, delta_time):
134 """ Movement and game logic """
135
136 # Call update on bullet sprites
137 self.bullet_list.update()
138
139 # Loop through each bullet
140 for bullet in self.bullet_list:
141
142 # Check this bullet to see if it hit a coin
143 hit_list = arcade.check_for_collision_with_list(bullet, self.coin_list)
144
145 # If it did, get rid of the bullet
146 if len(hit_list) > 0:
147 bullet.remove_from_sprite_lists()
148
149 # For every coin we hit, add to the score and remove the coin
150 for coin in hit_list:
151 coin.remove_from_sprite_lists()
152 self.score += 1
153
154 # Hit Sound
155 arcade.play_sound(self.hit_sound)
156
157 # If the bullet flies off-screen, remove it.
158 if bullet.bottom > SCREEN_HEIGHT:
159 bullet.remove_from_sprite_lists()
160
161
162def main():
163 window = MyGame()
164 window.setup()
165 arcade.run()
166
167
168if __name__ == "__main__":
169 main()