Finalize refactoring
This commit is contained in:
@@ -7,7 +7,6 @@
|
||||
|
||||
package com.example.flappybird;
|
||||
|
||||
import com.example.flappybird.states.GameState;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.image.BufferedImage;
|
||||
@@ -33,6 +32,7 @@ public class Bird extends HasPosition {
|
||||
// Bird sprites
|
||||
private final BufferedImage[] sprites;
|
||||
|
||||
// Track current frame
|
||||
private static double currentFrame = 0;
|
||||
|
||||
public Bird (int x, int y) {
|
||||
@@ -150,7 +150,6 @@ public class Bird extends HasPosition {
|
||||
|
||||
// Create bird animation and pass in rotation angle
|
||||
animateBird(g, rotation);
|
||||
|
||||
}
|
||||
|
||||
private void animateBird(Graphics g, double angle) {
|
||||
@@ -173,7 +172,9 @@ public class Bird extends HasPosition {
|
||||
// Switch animation frames
|
||||
if (currentFrame >= count - 1) {
|
||||
currentFrame = 0;
|
||||
} else currentFrame += 0.09;
|
||||
|
||||
}
|
||||
else {
|
||||
currentFrame += 0.09;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -45,18 +45,21 @@ public class FlappyBird implements ActionListener {
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setSize(WIDTH, HEIGHT);
|
||||
|
||||
String fontPath = "/res/fonts/flappy-font.ttf";
|
||||
|
||||
// Try to load ttf file
|
||||
try {
|
||||
InputStream is = new BufferedInputStream(this.getClass().getResourceAsStream("/res/fonts/flappy-font.ttf"));
|
||||
InputStream fontStream = this.getClass().getResourceAsStream(fontPath);
|
||||
assert fontStream != null;
|
||||
|
||||
InputStream is = new BufferedInputStream(fontStream);
|
||||
flappyFontBase = Font.createFont(Font.TRUETYPE_FONT, is);
|
||||
|
||||
// Header and sub-header fonts
|
||||
flappyScoreFont = flappyFontBase.deriveFont(Font.PLAIN, 50);
|
||||
flappyFontReal = flappyFontBase.deriveFont(Font.PLAIN, 20);
|
||||
flappyMiniFont = flappyFontBase.deriveFont(Font.PLAIN, 15);
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
// Exit is font cannot be loaded
|
||||
ex.printStackTrace();
|
||||
System.err.println("Could not load Flappy Font!");
|
||||
|
@@ -78,10 +78,6 @@ public class GamePanel extends JPanel implements KeyListener, MouseListener {
|
||||
// Mouse actions //
|
||||
///////////////////
|
||||
|
||||
public Point getClickedPoint() {
|
||||
return clickedPoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if point is in rectangle
|
||||
*
|
||||
|
@@ -12,11 +12,17 @@ public class Pipe extends HasPosition {
|
||||
String location;
|
||||
|
||||
// Pipe constants
|
||||
public static final int WIDTH = 67;
|
||||
public static final int HEIGHT = 416;
|
||||
public static final int PIPE_DISTANCE = 150; // Horizontal distance between pipes
|
||||
public static final int PIPE_SPACING = HEIGHT + 170; // Vertical distance between pipes
|
||||
private static final int SPEED = -2;
|
||||
public static final int WIDTH = 67;
|
||||
public static final int HEIGHT = 416;
|
||||
|
||||
// Horizontal distance between pipes
|
||||
public static final int PIPE_DISTANCE = 150;
|
||||
|
||||
// Vertical distance between pipes
|
||||
public static final int PIPE_SPACING = HEIGHT + 200;
|
||||
|
||||
// Speed of the bird.
|
||||
private static final int SPEED = -2;
|
||||
|
||||
// If the bird can get a point passing this pipe
|
||||
public boolean canAwardPoint = true;
|
||||
|
@@ -16,7 +16,6 @@ public class Texture extends HasPosition {
|
||||
private final BufferedImage image;
|
||||
private final Rectangle rect;
|
||||
|
||||
|
||||
public Texture (BufferedImage image, int x, int y) {
|
||||
super(x, y);
|
||||
|
||||
|
@@ -17,6 +17,7 @@ public abstract class GameState {
|
||||
// Game variables //
|
||||
////////////////////
|
||||
|
||||
// Background
|
||||
protected static final int[] baseCoords = { 0, 435 };
|
||||
|
||||
// Player score
|
||||
@@ -31,8 +32,10 @@ public abstract class GameState {
|
||||
// Arraylist of Pipe objects
|
||||
public static ArrayList<Pipe> pipes;
|
||||
|
||||
// Game bird
|
||||
protected static Bird gameBird;
|
||||
|
||||
// Game panel
|
||||
public GamePanel gamePanel;
|
||||
|
||||
public GameState(GamePanel panel) {
|
||||
@@ -45,9 +48,9 @@ public abstract class GameState {
|
||||
public void restart () {
|
||||
|
||||
// Reset game statistics
|
||||
score = 0;
|
||||
pipeDistTracker = 0;
|
||||
scoreWasGreater = false;
|
||||
score = 0;
|
||||
pipeDistTracker = 0;
|
||||
scoreWasGreater = false;
|
||||
|
||||
// Game bird
|
||||
gameBird = new Bird(172, 250);
|
||||
@@ -63,13 +66,11 @@ public abstract class GameState {
|
||||
|
||||
// Only move screen if bird is alive
|
||||
if (gameBird.isAlive()) {
|
||||
|
||||
// Move base
|
||||
// Moving base effect
|
||||
int baseSpeed = 2;
|
||||
baseCoords[0] = baseCoords[0] - baseSpeed < -435 ? 435 : baseCoords[0] - baseSpeed;
|
||||
baseCoords[1] = baseCoords[1] - baseSpeed < -435 ? 435 : baseCoords[1] - baseSpeed;
|
||||
|
||||
}
|
||||
|
||||
// Background
|
||||
@@ -96,4 +97,3 @@ public abstract class GameState {
|
||||
public abstract void handleKeyboardEvent(KeyEvent e);
|
||||
public abstract void handleMouseEvent(MouseEvent e);
|
||||
}
|
||||
|
||||
|
@@ -142,7 +142,6 @@ public class PlayState extends GameState {
|
||||
}
|
||||
|
||||
// Move and draw each pipe
|
||||
|
||||
for (Pipe p : pipes) {
|
||||
|
||||
// Move the pipe
|
||||
|
Reference in New Issue
Block a user