refactoring II

This commit is contained in:
Jan-Niclas Loosen 2024-11-27 17:13:52 +01:00
parent 50937ab7fd
commit 8b0cdf6963
5 changed files with 25 additions and 32 deletions

View File

@ -14,14 +14,14 @@ public class Audio {
private void playSound (String sound) { private void playSound (String sound) {
// Path to sound file // Path to sound file
String soundURL = "/res/sound/" + sound + ".wav"; String soundPath = "/res/sound/" + sound + ".wav";
// Try to load and play sound // Try to load and play sound
try { try {
URL audioUrl = this.getClass().getResource(soundURL); URL soundUrl = this.getClass().getResource(soundPath);
assert audioUrl != null; assert soundUrl != null;
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioUrl); AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundUrl);
Clip clip = AudioSystem.getClip(); Clip clip = AudioSystem.getClip();
clip.open(audioInputStream); clip.open(audioInputStream);
clip.start(); clip.start();

View File

@ -422,12 +422,7 @@ public class GamePanel extends JPanel implements KeyListener, MouseListener {
// Check if bird hits pipes // Check if bird hits pipes
if (gameBird.isAlive()) { if (gameBird.isAlive()) {
if (p.collide( if (p.collide(gameBird)) {
gameBird.getX(),
gameBird.getY(),
gameBird.BIRD_WIDTH,
gameBird.BIRD_HEIGHT
)) {
// Kill bird and play sound // Kill bird and play sound
gameBird.kill(); gameBird.kill();
audio.hit(); audio.hit();

View File

@ -21,11 +21,12 @@ public class Highscore {
// Read / Write to file setup // Read / Write to file setup
private static final String FILE_PATH = "/res/data/highscore.dat"; private static final String FILE_PATH = "/res/data/highscore.dat";
private static URL dataURL = Highscore.class.getResource(FILE_PATH); private static final URL dataURL = Highscore.class.getResource(FILE_PATH);
private static File dataFile; private static final File dataFile;
static { static {
try { try {
assert dataURL != null;
dataFile = Paths.get(dataURL.toURI()).toFile(); dataFile = Paths.get(dataURL.toURI()).toFile();
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -33,7 +34,6 @@ public class Highscore {
} }
private static Scanner dataScanner = null; private static Scanner dataScanner = null;
private static PrintWriter dataWriter = null;
// Highscore // Highscore
private int bestScore; private int bestScore;
@ -71,7 +71,7 @@ public class Highscore {
try { try {
// Write new highscore to data file // Write new highscore to data file
dataWriter = new PrintWriter(FILE_PATH, "UTF-8"); PrintWriter dataWriter = new PrintWriter(FILE_PATH, "UTF-8");
dataWriter.println(Integer.toString(newBest)); dataWriter.println(Integer.toString(newBest));
dataWriter.close(); dataWriter.close();
} catch (FileNotFoundException | UnsupportedEncodingException e) { } catch (FileNotFoundException | UnsupportedEncodingException e) {

View File

@ -44,27 +44,22 @@ public class Pipe extends HasPosition {
super.x += SPEED; super.x += SPEED;
} }
/** /**
* Checks for bird colliding with pipe * Checks for bird colliding with pipe
* *
* @param nX Bird x-coordinate * @param bird The bird.
* @param nY Bird y-coordinate
* @param nW Bird width
* @param nH Bird height
* @return If bird is colliding with the pipe * @return If bird is colliding with the pipe
*/ */
public boolean collide (int nX, int nY, int nW, int nH) { public boolean collide (Bird bird) {
// Do not allow bird to jump over pipe // Do not allow bird to jump over pipe
if (nX > super.x && nY < 0 && canAwardPoint) { if (bird.getX() > super.x && bird.getY() < 0 && canAwardPoint) {
return true; return true;
} }
return nX < super.x + WIDTH && return bird.getX() < super.x + WIDTH &&
nX + nW > super.x && bird.getX() + bird.BIRD_WIDTH > super.x &&
nY < super.y + HEIGHT && bird.getY() < super.y + HEIGHT &&
nY + nH > super.y; bird.getY() + bird.BIRD_HEIGHT > super.y;
} }
/** /**

View File

@ -10,6 +10,7 @@ import java.awt.Graphics2D;
import java.io.IOException; import java.io.IOException;
import java.io.File; import java.io.File;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
public class Sprites { public class Sprites {
@ -23,11 +24,13 @@ public class Sprites {
private static HashMap<String, Texture> textures = new HashMap<String, Texture>(); private static HashMap<String, Texture> textures = new HashMap<String, Texture>();
public Sprites () { public Sprites () {
// Try to load sprite sheet, exit program if cannot // Try to load sprite sheet, exit program if cannot
try { try {
spriteSheet = ImageIO.read(this.getClass().getResource("/res/img/spriteSheet.png")); String fontPath = "/res/img/spriteSheet.png";
URL fontUrl = this.getClass().getResource(fontPath);
assert fontUrl != null;
spriteSheet = ImageIO.read(fontUrl);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
System.out.println("Could not load sprite sheet."); System.out.println("Could not load sprite sheet.");