PART FOURTEEN
SOUND
GAMEPLAY
Sounds Class
Similar to the Images class, you'll create a class to store all the Sounds in your program.
Make a public static Sound variable for each sound you intend to add, then load them in the loadSound() method.
Don't forget to call this method in your init() method immediately after the line where you call loadImages().
Example: Tilling Sound Effect
Hoe plays when tilling soil
To implement this...
Identify where the soil tilling actually happens (in this case, class Dirt)
Call the play() method on the Sound object
Action Sounds
Harvest plays when collecting a grown crop
Seeds plays when planting a seed
WaterLap plays when watering soil
WaterScoop plays when filling a watering can
Axe plays when chopping a tree (every hit)
TreeFall plays when a tree is destroyed
Pick plays when breaking a rock (every hit)
RockBreak plays when a rock is destroyed
CHECKPOINT
Run your code and see...
That all of the sound effects are working
PITCH AND VOLUME
Repeated Sounds and Variance
When using a Sound's play() method, there is another version of the method that takes two parameters: pitch and volume.
For the Pick, Axe, and WaterLap sounds, make your sounds change a little each time they are clicked. You can do one or both of the following options.
Option #1 - Randomness
Modify the pitch by a random amount each time
Pitch should range from about 75% to 125%
Option #2 - Progression
Modify the pitch to make the sound higher each time, based on the health of the target object or remaining water in the bucket.
Pitch should range from about 75% to 125%
USER INTERFACE
UI Sounds
Cancel plays when a user can't complete an action they were trying while they have an item selected.
Look at Dirt, Water, Grass, Tree, and Rock in the clicked() method
In each case, play the sound if no other action has been taken in this method and item is null.
Select plays when a user selects an item in their bar and Unselect plays when the user chooses to select no items when it had one previously selected
Play both of these at about 20%-40% volume. It should be a subtle sound, since it will occur frequently and is tied to a less critical action.
Coins plays when the user successfully buys an item
Yes plays when the user changes to a new game state
NextDay plays when the user starts a new day.
Alternatively, use "Rooster" if you prefer a bolder start to each day.
AMBIENCE
Ambient Sounds
In class Weather, you'll want to manage which ambient sound is playing each day. You can use the loop, stop, and playing commands to change these. The code to the right shows how to change sounds when it starts raining.
Day loops on a sunny, clear day (including the first day)
Rain loops when it is raining
MUSIC
Music
In class Sounds, add a music file. Note that this uses class Music rather than class Sound.
A music file must be in the .ogg format. You can find many file converters to put your music in this format, but note that it is entirely possible some won't work properly in Slick2D.
Music works differently from Sound in that it is unique; only one music track may be playing at once.
In class Title, you'll want to start looping your music in the init() method.
Hint: Make sure you are loading your images and sounds in the Title's init() method, rather than Game, and that you are doing so before playing the music!
You may want to play your music at a lower volume (40-60%), compare its balance to the sound effects overall.
CUSTOMIZING SOUND LEVELS
User Controlled Volume
You don't need to do anything more with sound for this project, but to consider one enhancement to allow user control over sound.
Create variables to track the different layers of sound in your game. Most games will have a master volume, plus categories like:
SFX Volume
UI Volume
Music
Ambience
Voice
Store these values as a float between 0 and 1. Then when you play a sound, multiply that by the appropriate category's value and the master sound volume.
You may also want a boolean to track if the game is muted temporarily.
For example:
if(!muted)
{
Sound.music.play(1, master * music);
}
You can then create hotkeys, buttons, or a slider to allow the user to change these values or mute the game.
Baldur's Gate III
Diablo IV