refactoring II
This commit is contained in:
parent
50937ab7fd
commit
8b0cdf6963
@ -14,14 +14,14 @@ public class Audio {
|
||||
private void playSound (String sound) {
|
||||
|
||||
// Path to sound file
|
||||
String soundURL = "/res/sound/" + sound + ".wav";
|
||||
String soundPath = "/res/sound/" + sound + ".wav";
|
||||
|
||||
// Try to load and play sound
|
||||
try {
|
||||
URL audioUrl = this.getClass().getResource(soundURL);
|
||||
assert audioUrl != null;
|
||||
URL soundUrl = this.getClass().getResource(soundPath);
|
||||
assert soundUrl != null;
|
||||
|
||||
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioUrl);
|
||||
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundUrl);
|
||||
Clip clip = AudioSystem.getClip();
|
||||
clip.open(audioInputStream);
|
||||
clip.start();
|
||||
|
@ -422,12 +422,7 @@ public class GamePanel extends JPanel implements KeyListener, MouseListener {
|
||||
|
||||
// Check if bird hits pipes
|
||||
if (gameBird.isAlive()) {
|
||||
if (p.collide(
|
||||
gameBird.getX(),
|
||||
gameBird.getY(),
|
||||
gameBird.BIRD_WIDTH,
|
||||
gameBird.BIRD_HEIGHT
|
||||
)) {
|
||||
if (p.collide(gameBird)) {
|
||||
// Kill bird and play sound
|
||||
gameBird.kill();
|
||||
audio.hit();
|
||||
|
@ -21,11 +21,12 @@ public class Highscore {
|
||||
// Read / Write to file setup
|
||||
private static final String FILE_PATH = "/res/data/highscore.dat";
|
||||
|
||||
private static URL dataURL = Highscore.class.getResource(FILE_PATH);
|
||||
private static File dataFile;
|
||||
private static final URL dataURL = Highscore.class.getResource(FILE_PATH);
|
||||
private static final File dataFile;
|
||||
|
||||
static {
|
||||
try {
|
||||
assert dataURL != null;
|
||||
dataFile = Paths.get(dataURL.toURI()).toFile();
|
||||
} catch (URISyntaxException e) {
|
||||
throw new RuntimeException(e);
|
||||
@ -33,7 +34,6 @@ public class Highscore {
|
||||
}
|
||||
|
||||
private static Scanner dataScanner = null;
|
||||
private static PrintWriter dataWriter = null;
|
||||
|
||||
// Highscore
|
||||
private int bestScore;
|
||||
@ -71,7 +71,7 @@ public class Highscore {
|
||||
|
||||
try {
|
||||
// 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.close();
|
||||
} catch (FileNotFoundException | UnsupportedEncodingException e) {
|
||||
|
@ -44,27 +44,22 @@ public class Pipe extends HasPosition {
|
||||
super.x += SPEED;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks for bird colliding with pipe
|
||||
*
|
||||
* @param nX Bird x-coordinate
|
||||
* @param nY Bird y-coordinate
|
||||
* @param nW Bird width
|
||||
* @param nH Bird height
|
||||
* @param bird The bird.
|
||||
* @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
|
||||
if (nX > super.x && nY < 0 && canAwardPoint) {
|
||||
if (bird.getX() > super.x && bird.getY() < 0 && canAwardPoint) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return nX < super.x + WIDTH &&
|
||||
nX + nW > super.x &&
|
||||
nY < super.y + HEIGHT &&
|
||||
nY + nH > super.y;
|
||||
return bird.getX() < super.x + WIDTH &&
|
||||
bird.getX() + bird.BIRD_WIDTH > super.x &&
|
||||
bird.getY() < super.y + HEIGHT &&
|
||||
bird.getY() + bird.BIRD_HEIGHT > super.y;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -10,6 +10,7 @@ import java.awt.Graphics2D;
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Sprites {
|
||||
@ -23,11 +24,13 @@ public class Sprites {
|
||||
private static HashMap<String, Texture> textures = new HashMap<String, Texture>();
|
||||
|
||||
public Sprites () {
|
||||
|
||||
// Try to load sprite sheet, exit program if cannot
|
||||
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
System.out.println("Could not load sprite sheet.");
|
||||
|
Loading…
Reference in New Issue
Block a user