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