added sound effects (capture and moving piece)

This commit is contained in:
samuelsmith 2025-05-07 15:53:45 +02:00
parent ddbc1cba4b
commit d6c376ab27
4 changed files with 42 additions and 1 deletions

BIN
OOP_2B1_Project/capture.wav Normal file

Binary file not shown.

Binary file not shown.

View File

@ -7,6 +7,7 @@ import javax.swing.JOptionPane;
public class Board {
private KingCheck kingCheck = new KingCheck();
private SoundEffect soundEffect = new SoundEffect();
private int width = 8;
private int height = 8;
private ArrayList<ArrayList<Piece>> board = new ArrayList<>();
@ -162,6 +163,11 @@ public class Board {
}
}
}
if (board.get(y).get(x) != null) {
soundEffect.captureSound();
} else {
soundEffect.movingSound();
}
board.get(this.ym).set(this.xm,null);
this.setPiece(x,y,pieceToMove.getType(),pieceToMove.isWhite());
Piece movedPiece = this.getPiece(x, y);
@ -344,7 +350,11 @@ public class Board {
}
}
}
if (board.get(y).get(x) != null) {
soundEffect.captureSound();
} else {
soundEffect.movingSound();
}
this.setPiece(x,y,pieceToMove.getType(),pieceToMove.isWhite());
Piece movedPiece = this.getPiece(x, y);
if (movedPiece != null) {

View File

@ -0,0 +1,31 @@
package backend;
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
public class SoundEffect {
public void captureSound() {
try {
File file = new File("capture.wav");
AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
Clip clip = AudioSystem.getClip();
clip.open(audioStream);
clip.start();
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
e.printStackTrace();
}
}
public void movingSound() {
try {
File file = new File("move-self.wav");
AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
Clip clip = AudioSystem.getClip();
clip.open(audioStream);
clip.start();
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
e.printStackTrace();
}
}
}