Description
For this extra credit, add sound to your program. Play a sound when the user hits a ship, and play a different sound when the user misses. I will give extra points on top of the 5 points if you choose particularly amusing or enjoyable sounds. For example, you could pick up a microphone and record your own sounds, if you want.
There are a few ways to play sounds in Java. One way is relatively easy (using the Applet
class' newAudioClip
method), but gives very
little flexibility. Another way, using the Java Sound API, is powerful but somewhat
complex. Both techniques require at least a minimal understanding of how exceptions work
in Java, which we have not covered. If you're interested in learning about exceptions, read Chapter 8 in
the 4th edition, or Chapter 9 in the 5th edition of the textbook. I
have made a class that you can use to play audio without having to worry about exceptions.
Using it should be fairly straightforward.
Download
AudioPlayer.java
and put it in the same directory as the rest of your
Battleship program files. Then, take a look at the
AudioPlayer
documentation I have generated.
You will have to figure out where to call AudioPlayer
's methods from your code.
By default, only .WAV, .AIFF, and .AU files are supported. If you would like to use .mp3 files, then you must download and install Sun's Java MP3 plugin. Installation is pretty quick.
If you are planning to do this extra credit, please try to keep the sound files you use small. Before you submit your program, let me know if you have done this extra credit. I may have a different submission process if your files are too large -- my inbox might not take too many large attachments.
Let me know if you have any trouble getting the AudioPlayer
class to work.