Small Steps II — Making Things
Playing sound effects
Play sound effects.
What you will learn
Playing sound takes time. After a function call, the program may continue to its next task before the sound ends.
Try it
Example · example1.cpp
void small_main()
{
print("Pop!");
play_sound(Sound::Pop);
sleep(0.5);
print("Coin!");
play_sound(Sound::Coin);
sleep(1.0);
}
play_sound starts playback and returns immediately. Sound::Pop and Sound::Coin name built-in sound effects.
The example uses sleep to keep the program from ending immediately. Listen for the two effects starting in sequence.
Exercise
Play Click, Coin, and Win in order. Start each sound only after the previous one finishes.
Exercise starter · exercise1_starter.cpp
void small_main()
{
// Play Click, Coin, and Win in order.
}
Hint
Use play_sound_and_wait three times.
show solution
void small_main()
{
play_sound_and_wait(Sound::Click);
play_sound_and_wait(Sound::Coin);
play_sound_and_wait(Sound::Win);
}