1"""
2Sprite Explosion
3
4Simple program to show creating explosions with particles
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_explosion_particles
10"""
11import random
12import math
13import arcade
14
15SPRITE_SCALING_PLAYER = 0.5
16SPRITE_SCALING_COIN = 0.3
17SPRITE_SCALING_LASER = 0.8
18COIN_COUNT = 50
19
20SCREEN_WIDTH = 800
21SCREEN_HEIGHT = 600
22SCREEN_TITLE = "Sprite Explosion Example"
23
24BULLET_SPEED = 5
25
26# --- Explosion Particles Related
27
28# How fast the particle will accelerate down. Make 0 if not desired
29PARTICLE_GRAVITY = 0.05
30
31# How fast to fade the particle
32PARTICLE_FADE_RATE = 8
33
34# How fast the particle moves. Range is from 2.5 <--> 5 with 2.5 and 2.5 set.
35PARTICLE_MIN_SPEED = 2.5
36PARTICLE_SPEED_RANGE = 2.5
37
38# How many particles per explosion
39PARTICLE_COUNT = 20
40
41# How big the particle
42PARTICLE_RADIUS = 3
43
44# Possible particle colors
45PARTICLE_COLORS = [arcade.color.ALIZARIN_CRIMSON,
46 arcade.color.COQUELICOT,
47 arcade.color.LAVA,
48 arcade.color.KU_CRIMSON,
49 arcade.color.DARK_TANGERINE]
50
51# Chance we'll flip the texture to white and make it 'sparkle'
52PARTICLE_SPARKLE_CHANCE = 0.02
53
54# --- Smoke
55# Note: Adding smoke trails makes for a lot of sprites and can slow things
56# down. If you want a lot, it will be necessary to move processing to GPU
57# using transform feedback. If to slow, just get rid of smoke.
58
59# Start scale of smoke, and how fast is scales up
60SMOKE_START_SCALE = 0.25
61SMOKE_EXPANSION_RATE = 0.03
62
63# Rate smoke fades, and rises
64SMOKE_FADE_RATE = 7
65SMOKE_RISE_RATE = 0.5
66
67# Chance we leave smoke trail
68SMOKE_CHANCE = 0.25
69
70
71class Smoke(arcade.SpriteCircle):
72 """ This represents a puff of smoke """
73 def __init__(self, size):
74 super().__init__(size, arcade.color.LIGHT_GRAY, soft=True)
75 self.change_y = SMOKE_RISE_RATE
76 self.scale = SMOKE_START_SCALE
77
78 def update(self):
79 """ Update this particle """
80 if self.alpha <= PARTICLE_FADE_RATE:
81 # Remove faded out particles
82 self.remove_from_sprite_lists()
83 else:
84 # Update values
85 self.alpha -= SMOKE_FADE_RATE
86 self.center_x += self.change_x
87 self.center_y += self.change_y
88 self.scale += SMOKE_EXPANSION_RATE
89
90
91class Particle(arcade.SpriteCircle):
92 """ Explosion particle """
93 def __init__(self, my_list):
94 # Choose a random color
95 color = random.choice(PARTICLE_COLORS)
96
97 # Make the particle
98 super().__init__(PARTICLE_RADIUS, color)
99
100 # Track normal particle texture, so we can 'flip' when we sparkle.
101 self.normal_texture = self.texture
102
103 # Keep track of the list we are in, so we can add a smoke trail
104 self.my_list = my_list
105
106 # Set direction/speed
107 speed = random.random() * PARTICLE_SPEED_RANGE + PARTICLE_MIN_SPEED
108 direction = random.randrange(360)
109 self.change_x = math.sin(math.radians(direction)) * speed
110 self.change_y = math.cos(math.radians(direction)) * speed
111
112 # Track original alpha. Used as part of 'sparkle' where we temp set the
113 # alpha back to 255
114 self.my_alpha = 255
115
116 # What list do we add smoke particles to?
117 self.my_list = my_list
118
119 def update(self):
120 """ Update the particle """
121 if self.my_alpha <= PARTICLE_FADE_RATE:
122 # Faded out, remove
123 self.remove_from_sprite_lists()
124 else:
125 # Update
126 self.my_alpha -= PARTICLE_FADE_RATE
127 self.alpha = self.my_alpha
128 self.center_x += self.change_x
129 self.center_y += self.change_y
130 self.change_y -= PARTICLE_GRAVITY
131
132 # Should we sparkle this?
133 if random.random() <= PARTICLE_SPARKLE_CHANCE:
134 self.alpha = 255
135 self.texture = arcade.make_circle_texture(int(self.width),
136 arcade.color.WHITE)
137 else:
138 self.texture = self.normal_texture
139
140 # Leave a smoke particle?
141 if random.random() <= SMOKE_CHANCE:
142 smoke = Smoke(5)
143 smoke.position = self.position
144 self.my_list.append(smoke)
145
146
147class MyGame(arcade.Window):
148 """ Main application class. """
149
150 def __init__(self):
151 """ Initializer """
152 # Call the parent class initializer
153 super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
154
155 # Variables that will hold sprite lists
156 self.player_list = None
157 self.coin_list = None
158 self.bullet_list = None
159 self.explosions_list = None
160
161 # Set up the player info
162 self.player_sprite = None
163 self.score = 0
164
165 # Don't show the mouse cursor
166 self.set_mouse_visible(False)
167
168 # Load sounds. Sounds from kenney.nl
169 self.gun_sound = arcade.sound.load_sound(":resources:sounds/laser2.wav")
170 self.hit_sound = arcade.sound.load_sound(":resources:sounds/explosion2.wav")
171
172 self.background_color = arcade.color.BLACK
173
174 def setup(self):
175
176 """ Set up the game and initialize the variables. """
177
178 # Sprite lists
179 self.player_list = arcade.SpriteList()
180 self.coin_list = arcade.SpriteList()
181 self.bullet_list = arcade.SpriteList()
182 self.explosions_list = arcade.SpriteList()
183
184 # Set up the player
185 self.score = 0
186
187 # Image from kenney.nl
188 self.player_sprite = arcade.Sprite(":resources:images/space_shooter/playerShip2_orange.png",
189 scale=SPRITE_SCALING_PLAYER)
190 self.player_sprite.center_x = 50
191 self.player_sprite.center_y = 70
192 self.player_list.append(self.player_sprite)
193
194 # Create the coins
195 for coin_index in range(COIN_COUNT):
196
197 # Create the coin instance
198 # Coin image from kenney.nl
199 coin = arcade.Sprite(":resources:images/space_shooter/playerShip1_green.png",
200 scale=SPRITE_SCALING_COIN)
201 coin.angle = 180
202
203 # Position the coin
204 coin.center_x = random.randrange(SCREEN_WIDTH)
205 coin.center_y = random.randrange(150, SCREEN_HEIGHT)
206
207 # Add the coin to the lists
208 self.coin_list.append(coin)
209
210 def on_draw(self):
211 """
212 Render the screen.
213 """
214
215 # This command has to happen before we start drawing
216 self.clear()
217
218 # Draw all the sprites.
219 self.coin_list.draw()
220 self.bullet_list.draw()
221 self.player_list.draw()
222 self.explosions_list.draw()
223
224 # Render the text
225 arcade.draw_text(f"Score: {self.score}", 10, 20, arcade.color.WHITE, 14)
226
227 def on_mouse_motion(self, x, y, dx, dy):
228 """
229 Called whenever the mouse moves.
230 """
231 self.player_sprite.center_x = x
232
233 def on_mouse_press(self, x, y, button, modifiers):
234 """
235 Called whenever the mouse button is clicked.
236 """
237
238 # Gunshot sound
239 arcade.sound.play_sound(self.gun_sound)
240
241 # Create a bullet
242 bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png", scale=SPRITE_SCALING_LASER)
243
244 # The image points to the right, and we want it to point up. So
245 # rotate it.
246 bullet.angle = 270
247
248 # Give it a speed
249 bullet.change_y = BULLET_SPEED
250
251 # Position the bullet
252 bullet.center_x = self.player_sprite.center_x
253 bullet.bottom = self.player_sprite.top
254
255 # Add the bullet to the appropriate lists
256 self.bullet_list.append(bullet)
257
258 def on_update(self, delta_time):
259 """ Movement and game logic """
260
261 # Call update on bullet sprites
262 self.bullet_list.update()
263 self.explosions_list.update()
264
265 # Loop through each bullet
266 for bullet in self.bullet_list:
267
268 # Check this bullet to see if it hit a coin
269 hit_list = arcade.check_for_collision_with_list(bullet, self.coin_list)
270
271 # If it did...
272 if len(hit_list) > 0:
273
274 # Get rid of the bullet
275 bullet.remove_from_sprite_lists()
276
277 # For every coin we hit, add to the score and remove the coin
278 for coin in hit_list:
279 # Make an explosion
280 for i in range(PARTICLE_COUNT):
281 particle = Particle(self.explosions_list)
282 particle.position = coin.position
283 self.explosions_list.append(particle)
284
285 smoke = Smoke(50)
286 smoke.position = coin.position
287 self.explosions_list.append(smoke)
288
289 coin.remove_from_sprite_lists()
290 self.score += 1
291
292 # Hit Sound
293 arcade.sound.play_sound(self.hit_sound)
294
295 # If the bullet flies off-screen, remove it.
296 if bullet.bottom > SCREEN_HEIGHT:
297 bullet.remove_from_sprite_lists()
298
299
300def main():
301 window = MyGame()
302 window.center_window()
303 window.setup()
304 arcade.run()
305
306
307if __name__ == "__main__":
308 main()