sound effects

This commit is contained in:
2026-04-20 03:11:19 -05:00
parent 14efa7826e
commit cdb3f66cae
7 changed files with 30 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import javax.sound.sampled.*;
import javax.swing.*;
public class Platformer extends JPanel implements KeyListener, ActionListener {
@@ -44,6 +45,7 @@ public class Platformer extends JPanel implements KeyListener, ActionListener {
boolean gameStarted;
Image heart, emptyHeart, slash, amendmentImg, powerImg, pressRImg, endImg, winImg, titleImg;
ArrayList<Image> numbers;
Clip jumpSound, shootSound, hitSound, collectSound;
public Platformer(int boardWidth, int boardHeight, int tileSize) {
// setup game
@@ -68,6 +70,10 @@ public class Platformer extends JPanel implements KeyListener, ActionListener {
endImg = new ImageIcon("Sprites/end.png").getImage();
pressRImg = new ImageIcon("Sprites/PressR.png").getImage();
winImg = new ImageIcon("Sprites/win.png").getImage();
jumpSound = loadClip("Sounds/jump.wav");
shootSound = loadClip("Sounds/shoot.wav");
hitSound = loadClip("Sounds/hit.wav");
collectSound = loadClip("Sounds/collect.wav");
gameTimer = new Timer(15, this);
player = new Player(-20, 0, tileSize, tileSize);
collidables = new ArrayList<>();
@@ -172,11 +178,13 @@ public class Platformer extends JPanel implements KeyListener, ActionListener {
boolean jumpKeyDown = (isKeyPressed(KeyEvent.VK_W) || isKeyPressed(KeyEvent.VK_UP));
if (jumpKeyDown && !jumpPressed) {
if (player.onGround) {
playClip(jumpSound);
player.yVelo = -15;
player.onGround = false;
jumpPressed = true;
player.airJumps = 0;
} else if (player.curPower == 1 && player.airJumps < 1) {
playClip(jumpSound);
player.yVelo = -15;
player.airJumps++;
jumpPressed = true;
@@ -220,6 +228,7 @@ public class Platformer extends JPanel implements KeyListener, ActionListener {
Amendment am = (Amendment) c;
if (player.collidesWith(am)) {
player.numAmendments++;
playClip(collectSound);
}
}
}
@@ -268,6 +277,7 @@ public class Platformer extends JPanel implements KeyListener, ActionListener {
// shoot
// projectiles
if (isKeyPressed(KeyEvent.VK_SPACE) && player.shootCooldown == 0) {
playClip(shootSound);
int projX = player.facing == 1 ? player.x + player.width : player.x - 10;
projectiles.add(new Projectile(projX, player.y, tileSize, 10, currentLevel, player.facing));
player.shootCooldown = Player.SHOOT_COOLDOWN;
@@ -291,6 +301,7 @@ public class Platformer extends JPanel implements KeyListener, ActionListener {
// enemy hits player
if (player.collidesWith(e)) {
player.takeDamage();
playClip(hitSound);
}
}
@@ -557,4 +568,23 @@ public class Platformer extends JPanel implements KeyListener, ActionListener {
// dont need
@Override
public void keyTyped(KeyEvent e) {}
public Clip loadClip(String path) {
try {
AudioInputStream audio = AudioSystem.getAudioInputStream(new File(path));
Clip clip = AudioSystem.getClip();
clip.open(audio);
return clip;
} catch (Exception e) {
System.out.println("Could not load sound: " + path);
return null;
}
}
public void playClip(Clip clip) {
if (clip != null) {
clip.setFramePosition(0); // rewind to start
clip.start();
}
}
}