Sound Effects
Pygame supports playing sound effects in addition to music. Sound effects are typically short audio clips used for in-game actions like explosions, footsteps, or button clicks.
Here’s an example of how to play a sound effect using Pygame:
To add sound effects in Pygame,first load the sound effect file:
sound_effect = pygame.mixer.Sound(‘laser.mp3’)
To play the sound effect:
sound_effect.play()
By default, the sound effect will play once. If you want to repeat the sound effect, you can use the play() function in a loop.
To adjust the sound effect volume. The `set_volume()` function allows you to adjust the volume of the sound effect. It takes a value between 0.0 and 1.0, where 0.0 is mute and 1.0 is full volume.
sound_effect.set_volume(0.5) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access