refactoring II
This commit is contained in:
		@@ -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,18 +24,20 @@ 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.");
 | 
			
		||||
			System.exit(-1); // Exit program if file could not be found
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		
 | 
			
		||||
		// Backgrounds
 | 
			
		||||
		textures.put("background1", new Texture(resize(spriteSheet.getSubimage(0, 0, 144, 256)),   0, 0));
 | 
			
		||||
		textures.put("background2", new Texture(resize(spriteSheet.getSubimage(146, 0, 144, 256)), 0, 0));
 | 
			
		||||
@@ -60,7 +63,7 @@ public class Sprites {
 | 
			
		||||
		textures.put("playButton",   new Texture(resize(spriteSheet.getSubimage(354, 118, 52, 29)), 34, 448));
 | 
			
		||||
		textures.put("leaderboard",  new Texture(resize(spriteSheet.getSubimage(414, 118, 52, 29)), 203, 448));
 | 
			
		||||
		textures.put("rateButton",   new Texture(resize(spriteSheet.getSubimage(465, 1, 31, 18)),   147, 355));
 | 
			
		||||
		
 | 
			
		||||
 | 
			
		||||
		// Helpful / Text
 | 
			
		||||
		textures.put("newHighscore", new Texture(resize(spriteSheet.getSubimage(112, 501, 16, 7)),  210, 305));
 | 
			
		||||
		textures.put("titleText",    new Texture(resize(spriteSheet.getSubimage(351, 91, 89, 24)),  72, 100));
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user