Added sound effects

This commit is contained in:
Sagi Dayan 2016-01-23 13:04:06 +02:00
parent 7860ceefe9
commit cdaf2c7023
6 changed files with 11 additions and 5 deletions

View file

@ -30,9 +30,9 @@ public class GameEngine extends MouseAdapter {
private BufferedImage sceneImage; private BufferedImage sceneImage;
private Vector<SideScollerBackground> backgrounds; private Vector<SideScollerBackground> backgrounds;
private Vector<AudioClip> successSounds;
private AudioClip themeAudioClip, jumpAudioClip; private AudioClip themeAudioClip;
private final URL jumpURL= getClass().getResource("/Sounds/jump.wav");
private final URL themeURL= getClass().getResource("/Sounds/theme.wav"); private final URL themeURL= getClass().getResource("/Sounds/theme.wav");
public GameEngine(int width, int height){ public GameEngine(int width, int height){
@ -45,10 +45,8 @@ public class GameEngine extends MouseAdapter {
this.pWidth = width; this.pWidth = width;
this.pHeight = height; this.pHeight = height;
try { try {
jumpAudioClip = Applet.newAudioClip(jumpURL);
themeAudioClip = Applet.newAudioClip(themeURL); themeAudioClip = Applet.newAudioClip(themeURL);
}catch (Exception e){ }catch (Exception e){
jumpAudioClip = null;
themeAudioClip = null; themeAudioClip = null;
} }
@ -56,6 +54,7 @@ public class GameEngine extends MouseAdapter {
themeAudioClip.loop(); themeAudioClip.loop();
r = new Random(); r = new Random();
sceneImage = new BufferedImage(width, height, Image.SCALE_SMOOTH); sceneImage = new BufferedImage(width, height, Image.SCALE_SMOOTH);
loadSuccessSounds();
startNewGame(); startNewGame();
@ -88,6 +87,13 @@ public class GameEngine extends MouseAdapter {
} }
private void loadSuccessSounds(){
successSounds = new Vector<>();
for (int i = 1 ; i < 5 ; i++){
successSounds.add(Applet.newAudioClip(getClass().getResource("/Sounds/pass"+i+".wav")));
}
}
private void initBackgrounds(){ private void initBackgrounds(){
backgrounds.add(new SideScollerBackground(pWidth, pHeight, 2, "skyLine.png", pWidth, pHeight)); backgrounds.add(new SideScollerBackground(pWidth, pHeight, 2, "skyLine.png", pWidth, pHeight));
backgrounds.add(new SideScollerBackground(pWidth, pHeight, 5, "trees.png", pWidth + 50, pHeight)); backgrounds.add(new SideScollerBackground(pWidth, pHeight, 5, "trees.png", pWidth + 50, pHeight));
@ -172,6 +178,7 @@ public class GameEngine extends MouseAdapter {
if(canScore && !pipes.isEmpty() && bird.getLocX() >= pipes.elementAt(0).getLocX() + pipes.elementAt(0).getSWidth()) { if(canScore && !pipes.isEmpty() && bird.getLocX() >= pipes.elementAt(0).getLocX() + pipes.elementAt(0).getSWidth()) {
score++; score++;
canScore = false; canScore = false;
successSounds.elementAt(r.nextInt(5)).play();
} }
} }
@ -224,7 +231,6 @@ public class GameEngine extends MouseAdapter {
initGame(); initGame();
} else { } else {
bird.jump(); bird.jump();
jumpAudioClip.play();
} }
} }

Binary file not shown.

BIN
src/Sounds/pass1.wav Normal file

Binary file not shown.

BIN
src/Sounds/pass2.wav Normal file

Binary file not shown.

BIN
src/Sounds/pass3.wav Normal file

Binary file not shown.

BIN
src/Sounds/pass4.wav Normal file

Binary file not shown.