refactorings I
This commit is contained in:
		@@ -1,53 +0,0 @@
 | 
			
		||||
package com.example.flappybird; /**
 | 
			
		||||
 * Animation.java
 | 
			
		||||
 * Creates animation with array of sprites
 | 
			
		||||
 *
 | 
			
		||||
 * @author  Paul Krishnamurthy
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
import java.awt.Graphics;
 | 
			
		||||
import java.awt.Graphics2D;
 | 
			
		||||
import java.awt.image.BufferedImage;
 | 
			
		||||
import java.awt.geom.AffineTransform;
 | 
			
		||||
 | 
			
		||||
public class Animation {
 | 
			
		||||
 | 
			
		||||
	// Start at first frame
 | 
			
		||||
	private static double currentFrame = 0;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Creates an animation with an array of sprites
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param sprites     Array of BufferedImages
 | 
			
		||||
	 * @param x           x-coordinate
 | 
			
		||||
	 * @param y           y-coordinate
 | 
			
		||||
	 * @param speed       Speed of animation
 | 
			
		||||
	 * @param angle       Angle to rotate sprite
 | 
			
		||||
	 */
 | 
			
		||||
	public static void animate (Graphics g, BufferedImage[] sprites, int x, int y, double speed, double angle) {
 | 
			
		||||
		
 | 
			
		||||
		Graphics2D g2d        = (Graphics2D) g;
 | 
			
		||||
		AffineTransform trans = g2d.getTransform();
 | 
			
		||||
		AffineTransform at    = new AffineTransform();
 | 
			
		||||
 | 
			
		||||
		// Number of frames
 | 
			
		||||
		int count = sprites.length;
 | 
			
		||||
 | 
			
		||||
		// Rotate the image
 | 
			
		||||
		at.rotate(angle, x + 25, y + 25);
 | 
			
		||||
		g2d.transform(at);
 | 
			
		||||
 | 
			
		||||
		// Draw the current rotated frame
 | 
			
		||||
		g2d.drawImage(sprites[(int) (Math.round(currentFrame))], x, y, null);
 | 
			
		||||
 | 
			
		||||
		g2d.setTransform(trans);
 | 
			
		||||
		
 | 
			
		||||
		// Switch animation frames
 | 
			
		||||
    if (currentFrame >= count - 1) {
 | 
			
		||||
        currentFrame = 0;
 | 
			
		||||
    } else currentFrame += speed;
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -8,12 +8,9 @@ package com.example.flappybird; /**
 | 
			
		||||
import javax.sound.sampled.AudioInputStream;
 | 
			
		||||
import javax.sound.sampled.AudioSystem;
 | 
			
		||||
import javax.sound.sampled.Clip;
 | 
			
		||||
import java.net.URL;
 | 
			
		||||
 | 
			
		||||
public class Audio {
 | 
			
		||||
 | 
			
		||||
	private AudioInputStream audioInputStream;
 | 
			
		||||
	private Clip clip;
 | 
			
		||||
 | 
			
		||||
	private void playSound (String sound) {
 | 
			
		||||
 | 
			
		||||
		// Path to sound file
 | 
			
		||||
@@ -21,8 +18,11 @@ public class Audio {
 | 
			
		||||
 | 
			
		||||
		// Try to load and play sound
 | 
			
		||||
		try {
 | 
			
		||||
		    audioInputStream = AudioSystem.getAudioInputStream(this.getClass().getResource(soundURL));
 | 
			
		||||
		    clip = AudioSystem.getClip();
 | 
			
		||||
			URL audioUrl = this.getClass().getResource(soundURL);
 | 
			
		||||
			assert audioUrl != null;
 | 
			
		||||
 | 
			
		||||
			AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioUrl);
 | 
			
		||||
			Clip clip = AudioSystem.getClip();
 | 
			
		||||
		    clip.open(audioInputStream);
 | 
			
		||||
		    clip.start();
 | 
			
		||||
		} catch (Exception e) {
 | 
			
		||||
 
 | 
			
		||||
@@ -5,15 +5,15 @@ package com.example.flappybird; /**
 | 
			
		||||
 * @author  Paul Krishnamurthy
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
import javax.swing.JPanel;
 | 
			
		||||
import java.awt.*;
 | 
			
		||||
import java.awt.geom.AffineTransform;
 | 
			
		||||
import java.awt.image.BufferedImage;
 | 
			
		||||
import java.awt.Graphics;
 | 
			
		||||
 | 
			
		||||
public class Bird extends JPanel {
 | 
			
		||||
public class Bird extends HasPosition {
 | 
			
		||||
 | 
			
		||||
	// Bird attributes
 | 
			
		||||
	public String color;
 | 
			
		||||
	private int x, y;
 | 
			
		||||
 | 
			
		||||
	private boolean isAlive = true;
 | 
			
		||||
	
 | 
			
		||||
	// Bird constants
 | 
			
		||||
@@ -34,11 +34,12 @@ public class Bird extends JPanel {
 | 
			
		||||
	// Bird sprites
 | 
			
		||||
	private BufferedImage[] sprites;
 | 
			
		||||
 | 
			
		||||
	private static double currentFrame = 0;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	public Bird (String color, int x, int y, BufferedImage[] s) {
 | 
			
		||||
		super(x, y);
 | 
			
		||||
		this.color = color;
 | 
			
		||||
		this.x = x;
 | 
			
		||||
		this.y = y;
 | 
			
		||||
		this.sprites = s;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -144,7 +145,7 @@ public class Bird extends JPanel {
 | 
			
		||||
		rotation /= 2;
 | 
			
		||||
 | 
			
		||||
		// Handle rotation offset
 | 
			
		||||
		rotation = rotation > Math.PI / 2 ? Math.PI / 2 : rotation;
 | 
			
		||||
		rotation = Math.min(rotation, Math.PI / 2);
 | 
			
		||||
 | 
			
		||||
		if (!isAlive()) {
 | 
			
		||||
 | 
			
		||||
@@ -157,8 +158,32 @@ public class Bird extends JPanel {
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Create bird animation and pass in rotation angle
 | 
			
		||||
		Animation.animate(g, sprites, x, y, .09, rotation);
 | 
			
		||||
		animateBird(g, rotation);
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private void animateBird(Graphics g, double angle) {
 | 
			
		||||
 | 
			
		||||
		Graphics2D g2d = (Graphics2D) g;
 | 
			
		||||
		AffineTransform trans = g2d.getTransform();
 | 
			
		||||
		AffineTransform at = new AffineTransform();
 | 
			
		||||
 | 
			
		||||
		// Number of frames
 | 
			
		||||
		int count = sprites.length;
 | 
			
		||||
 | 
			
		||||
		// Rotate the image
 | 
			
		||||
		at.rotate(angle, x + 25, y + 25);
 | 
			
		||||
		g2d.transform(at);
 | 
			
		||||
 | 
			
		||||
		// Draw the current rotated frame
 | 
			
		||||
		g2d.drawImage(sprites[(int) (Math.round(currentFrame))], x, y, null);
 | 
			
		||||
 | 
			
		||||
		g2d.setTransform(trans);
 | 
			
		||||
 | 
			
		||||
		// Switch animation frames
 | 
			
		||||
		if (currentFrame >= count - 1) {
 | 
			
		||||
			currentFrame = 0;
 | 
			
		||||
		} else currentFrame += 0.09;
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@ package com.example.flappybird; /**
 | 
			
		||||
import java.awt.*;
 | 
			
		||||
import java.awt.event.*;
 | 
			
		||||
import javax.swing.*;
 | 
			
		||||
import java.net.URI;
 | 
			
		||||
import java.util.Random;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
@@ -501,6 +502,18 @@ public class GamePanel extends JPanel implements KeyListener, MouseListener {
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void openURL (String url) {
 | 
			
		||||
 | 
			
		||||
		try {
 | 
			
		||||
			if (Desktop.isDesktopSupported()) {
 | 
			
		||||
				Desktop.getDesktop().browse(new URI(url));
 | 
			
		||||
			}
 | 
			
		||||
		} catch (Exception e) {
 | 
			
		||||
			e.printStackTrace();
 | 
			
		||||
			System.out.println("Sorry could not open URL...");
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	//////////////////////
 | 
			
		||||
	// Keyboard actions //
 | 
			
		||||
@@ -569,7 +582,7 @@ public class GamePanel extends JPanel implements KeyListener, MouseListener {
 | 
			
		||||
			if (gameBird.isAlive()) {
 | 
			
		||||
 | 
			
		||||
				if (isTouching(textures.get("rateButton").getRect())) {
 | 
			
		||||
					Helper.openURL("http://paulkr.com"); // Open website
 | 
			
		||||
					openURL("http://paulkr.com"); // Open website
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,19 @@
 | 
			
		||||
package com.example.flappybird;
 | 
			
		||||
 | 
			
		||||
public class HasPosition {
 | 
			
		||||
    protected int x;
 | 
			
		||||
    protected int y;
 | 
			
		||||
 | 
			
		||||
    public HasPosition(int x, int y) {
 | 
			
		||||
        this.x = x;
 | 
			
		||||
        this.y = y;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getX() {
 | 
			
		||||
        return this.x;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getY() {
 | 
			
		||||
        return this.y;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,31 +0,0 @@
 | 
			
		||||
package com.example.flappybird; /**
 | 
			
		||||
 * Helper.java
 | 
			
		||||
 * Helper class with various tools
 | 
			
		||||
 *
 | 
			
		||||
 * @author  Paul Krishnamurthy
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
import java.awt.Desktop;
 | 
			
		||||
import java.net.URI;
 | 
			
		||||
 | 
			
		||||
public class Helper {
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Tries to open url in default web browser
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param url     Destination URL
 | 
			
		||||
	 */
 | 
			
		||||
	public static void openURL (String url) {
 | 
			
		||||
 | 
			
		||||
		try {
 | 
			
		||||
			if (Desktop.isDesktopSupported()) {
 | 
			
		||||
				Desktop.getDesktop().browse(new URI(url));
 | 
			
		||||
			}
 | 
			
		||||
		} catch (Exception e) {
 | 
			
		||||
			e.printStackTrace();
 | 
			
		||||
			System.out.println("Sorry could not open URL...");
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -7,12 +7,7 @@ package com.example.flappybird;
 | 
			
		||||
 * @author  Paul Krishnamurthy
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
public class Pipe {
 | 
			
		||||
 | 
			
		||||
	// Pipe coordinates
 | 
			
		||||
	private int x = FlappyBird.WIDTH + 5;
 | 
			
		||||
	private int y;
 | 
			
		||||
 | 
			
		||||
public class Pipe extends HasPosition {
 | 
			
		||||
	// Placement (top or bottom) of pipe
 | 
			
		||||
	String location;
 | 
			
		||||
 | 
			
		||||
@@ -27,17 +22,18 @@ public class Pipe {
 | 
			
		||||
	public boolean canAwardPoint = true;
 | 
			
		||||
 | 
			
		||||
	public Pipe (String location) {
 | 
			
		||||
		super(FlappyBird.WIDTH + 5, 0);
 | 
			
		||||
		this.location = location;
 | 
			
		||||
		reset();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void reset () {
 | 
			
		||||
		x = FlappyBird.WIDTH + 5; // Reset x-coordinate
 | 
			
		||||
		super.x = FlappyBird.WIDTH + 5; // Reset x-coordinate
 | 
			
		||||
 | 
			
		||||
		// Set boundaries for top pipes
 | 
			
		||||
		// This y-coordinte + PIPE_SPACING will be for the bottom pipe
 | 
			
		||||
		if (location.equals("top")) {
 | 
			
		||||
			y = - Math.max((int) (Math.random() * 320) + 30, 140);
 | 
			
		||||
			super.y = - Math.max((int) (Math.random() * 320) + 30, 140);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -45,7 +41,7 @@ public class Pipe {
 | 
			
		||||
	 * Moves the pipe
 | 
			
		||||
	 */
 | 
			
		||||
	public void move () {
 | 
			
		||||
		x += SPEED;
 | 
			
		||||
		super.x += SPEED;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -61,29 +57,14 @@ public class Pipe {
 | 
			
		||||
	public boolean collide (int nX, int nY, int nW, int nH) {
 | 
			
		||||
 | 
			
		||||
		// Do not allow bird to jump over pipe
 | 
			
		||||
		if (nX > x && nY < 0 && canAwardPoint) {
 | 
			
		||||
		if (nX > super.x && nY < 0 && canAwardPoint) {
 | 
			
		||||
			return true;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return nX < x + WIDTH && 
 | 
			
		||||
				nX + nW > x &&
 | 
			
		||||
				nY < y + HEIGHT &&
 | 
			
		||||
				nY + nH > y;
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * @return     Pipe's x-coordinate
 | 
			
		||||
	 */
 | 
			
		||||
	public int getX () {
 | 
			
		||||
		return x;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * @return     Pipe's y-coordinate
 | 
			
		||||
	 */
 | 
			
		||||
	public int getY () {
 | 
			
		||||
		return y;
 | 
			
		||||
		return nX < super.x + WIDTH &&
 | 
			
		||||
				nX + nW > super.x &&
 | 
			
		||||
				nY < super.y + HEIGHT &&
 | 
			
		||||
				nY + nH > super.y;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
@@ -94,6 +75,5 @@ public class Pipe {
 | 
			
		||||
	public void setY (int newY) {
 | 
			
		||||
		y = newY;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -8,20 +8,20 @@ package com.example.flappybird; /**
 | 
			
		||||
import java.awt.image.BufferedImage;
 | 
			
		||||
import java.awt.Rectangle;
 | 
			
		||||
 | 
			
		||||
public class Texture {
 | 
			
		||||
public class Texture extends HasPosition {
 | 
			
		||||
 | 
			
		||||
	// Image attributes
 | 
			
		||||
	private BufferedImage image;
 | 
			
		||||
	private int x, y, width, height;
 | 
			
		||||
	private Rectangle rect;
 | 
			
		||||
	private final BufferedImage image;
 | 
			
		||||
	private final Rectangle rect;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	public Texture (BufferedImage image, int x, int y) {
 | 
			
		||||
		super(x, y);
 | 
			
		||||
 | 
			
		||||
		this.image = image;
 | 
			
		||||
		this.x = x;
 | 
			
		||||
		this.y = y;
 | 
			
		||||
		this.width = image.getWidth();
 | 
			
		||||
		this.height = image.getHeight();
 | 
			
		||||
		int width = image.getWidth();
 | 
			
		||||
		int height = image.getHeight();
 | 
			
		||||
 | 
			
		||||
		this.rect = new Rectangle(x, y, width, height);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -33,22 +33,6 @@ public class Texture {
 | 
			
		||||
		return image;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public int getX () {
 | 
			
		||||
		return x;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public int getY () {
 | 
			
		||||
		return y;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public int getWidth () {
 | 
			
		||||
		return width;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public int getHeight () {
 | 
			
		||||
		return height;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public Rectangle getRect () {
 | 
			
		||||
		return rect;
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user