finish refactoring
This commit is contained in:
parent
08bca1c0db
commit
c93b8d02b6
@ -8,7 +8,7 @@
|
|||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -106,9 +106,7 @@ public class Bird extends HasPosition {
|
|||||||
/**
|
/**
|
||||||
* Bird movement during the game
|
* Bird movement during the game
|
||||||
*/
|
*/
|
||||||
public void inGameBorders () {
|
public void moveBird() {
|
||||||
// If the bird did not hit the base, lower it
|
|
||||||
if (y < BASE_COLLISION) {
|
|
||||||
// Change and velocity
|
// Change and velocity
|
||||||
velocity += gravity;
|
velocity += gravity;
|
||||||
|
|
||||||
@ -117,18 +115,16 @@ public class Bird extends HasPosition {
|
|||||||
|
|
||||||
// Add rounded velocity to y-coordinate
|
// Add rounded velocity to y-coordinate
|
||||||
y += (int) velocity;
|
y += (int) velocity;
|
||||||
} else {
|
|
||||||
// Play audio and set state to dead
|
|
||||||
FlappyBird.audio.hit();
|
|
||||||
isAlive = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean collidesWithBase() {
|
||||||
|
return y >= BASE_COLLISION;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders bird
|
* Renders bird
|
||||||
*/
|
*/
|
||||||
public void renderBird (Graphics g) {
|
public void paintBird(Graphics g) {
|
||||||
// Calculate angle to rotate bird based on y-velocity
|
// Calculate angle to rotate bird based on y-velocity
|
||||||
double rotation = ((90 * (velocity + 25) / 25) - 90) * Math.PI / 180;
|
double rotation = ((90 * (velocity + 25) / 25) - 90) * Math.PI / 180;
|
||||||
|
|
||||||
|
@ -23,6 +23,9 @@ public class GamePanel extends JPanel implements KeyListener, MouseListener {
|
|||||||
// Store point when player clicks
|
// Store point when player clicks
|
||||||
protected Point clickedPoint = new Point(-1, -1);
|
protected Point clickedPoint = new Point(-1, -1);
|
||||||
|
|
||||||
|
// Background
|
||||||
|
protected static int[] backgroundPosition = { 0, 435 };
|
||||||
|
|
||||||
public GamePanel () {
|
public GamePanel () {
|
||||||
setGameState(new MenuState(this));
|
setGameState(new MenuState(this));
|
||||||
|
|
||||||
@ -50,16 +53,49 @@ public class GamePanel extends JPanel implements KeyListener, MouseListener {
|
|||||||
/**
|
/**
|
||||||
* To start game after everything has been loaded
|
* To start game after everything has been loaded
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public void addNotify() {
|
public void addNotify() {
|
||||||
super.addNotify();
|
super.addNotify();
|
||||||
requestFocusInWindow(); // Ensures the panel gains focus
|
requestFocusInWindow();
|
||||||
ready = true;
|
ready = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void moveBase(Graphics g) {
|
||||||
|
// Set font and color
|
||||||
|
g.setFont(FlappyBird.flappyFontReal);
|
||||||
|
g.setColor(Color.white);
|
||||||
|
|
||||||
|
// Only move screen if bird is alive
|
||||||
|
if (GameState.gameBird.isAlive()) {
|
||||||
|
// Move base
|
||||||
|
// Moving base effect
|
||||||
|
int baseSpeed = 2;
|
||||||
|
backgroundPosition[0] = backgroundPosition[0] - baseSpeed < -435 ? 435 : backgroundPosition[0] - baseSpeed;
|
||||||
|
backgroundPosition[1] = backgroundPosition[1] - baseSpeed < -435 ? 435 : backgroundPosition[1] - baseSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Background
|
||||||
|
g.drawImage(FlappyBird.darkTheme ? FlappyBird.textures.get("background2").getImage() :
|
||||||
|
FlappyBird.textures.get("background1").getImage(), 0, 0, null);
|
||||||
|
|
||||||
|
// Draw bird
|
||||||
|
GameState.gameBird.paintBird(g);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Needs to be called differently based on screen
|
||||||
|
*/
|
||||||
|
public void paintBase(Graphics g) {
|
||||||
|
// Moving base effect
|
||||||
|
g.drawImage(FlappyBird.textures.get("base").getImage(), backgroundPosition[0], FlappyBird.textures.get("base").getY(), null);
|
||||||
|
g.drawImage(FlappyBird.textures.get("base").getImage(), backgroundPosition[1], FlappyBird.textures.get("base").getY(), null);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void paintComponent(Graphics g) {
|
public void paintComponent(Graphics g) {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
gameState.renderGameState(g);
|
gameState.paintGameState(g);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////
|
//////////////////////
|
||||||
|
@ -17,9 +17,6 @@ public abstract class GameState {
|
|||||||
// Game variables //
|
// Game variables //
|
||||||
////////////////////
|
////////////////////
|
||||||
|
|
||||||
// Background
|
|
||||||
protected static final int[] baseCoords = { 0, 435 };
|
|
||||||
|
|
||||||
// Player score
|
// Player score
|
||||||
protected int score;
|
protected int score;
|
||||||
|
|
||||||
@ -33,7 +30,7 @@ public abstract class GameState {
|
|||||||
public static ArrayList<Pipe> pipes;
|
public static ArrayList<Pipe> pipes;
|
||||||
|
|
||||||
// Game bird
|
// Game bird
|
||||||
protected static Bird gameBird;
|
public static Bird gameBird;
|
||||||
|
|
||||||
// Game panel
|
// Game panel
|
||||||
public GamePanel gamePanel;
|
public GamePanel gamePanel;
|
||||||
@ -46,7 +43,6 @@ public abstract class GameState {
|
|||||||
* Restarts game by resetting game variables
|
* Restarts game by resetting game variables
|
||||||
*/
|
*/
|
||||||
public void restart () {
|
public void restart () {
|
||||||
|
|
||||||
// Reset game statistics
|
// Reset game statistics
|
||||||
score = 0;
|
score = 0;
|
||||||
pipeDistTracker = 0;
|
pipeDistTracker = 0;
|
||||||
@ -59,41 +55,7 @@ public abstract class GameState {
|
|||||||
pipes = new ArrayList<Pipe>();
|
pipes = new ArrayList<Pipe>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void prepareScreen(Graphics g) {
|
public abstract void paintGameState(Graphics g);
|
||||||
// Set font and color
|
|
||||||
g.setFont(FlappyBird.flappyFontReal);
|
|
||||||
g.setColor(Color.white);
|
|
||||||
|
|
||||||
// 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
|
|
||||||
g.drawImage(FlappyBird.darkTheme ? FlappyBird.textures.get("background2").getImage() :
|
|
||||||
FlappyBird.textures.get("background1").getImage(), 0, 0, null);
|
|
||||||
|
|
||||||
// Draw bird
|
|
||||||
gameBird.renderBird(g);
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract void renderGameState(Graphics g);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Needs to be called differently based on screen
|
|
||||||
*/
|
|
||||||
public void drawBase (Graphics g) {
|
|
||||||
|
|
||||||
// Moving base effect
|
|
||||||
g.drawImage(FlappyBird.textures.get("base").getImage(), baseCoords[0], FlappyBird.textures.get("base").getY(), null);
|
|
||||||
g.drawImage(FlappyBird.textures.get("base").getImage(), baseCoords[1], FlappyBird.textures.get("base").getY(), null);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract void handleKeyboardEvent(KeyEvent e);
|
public abstract void handleKeyboardEvent(KeyEvent e);
|
||||||
public abstract void handleMouseEvent(MouseEvent e);
|
public abstract void handleMouseEvent(MouseEvent e);
|
||||||
}
|
}
|
||||||
|
@ -14,32 +14,16 @@ public class MenuState extends GameState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void renderGameState(Graphics g) {
|
public void paintGameState(Graphics g) {
|
||||||
prepareScreen(g);
|
gamePanel.moveBase(g);
|
||||||
|
gamePanel.paintBase(g);
|
||||||
|
|
||||||
drawBase(g);
|
paintMenu(g);
|
||||||
drawMenu(g);
|
|
||||||
|
|
||||||
gameBird.menuFloat();
|
gameBird.menuFloat();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected void paintMenu(Graphics g) {
|
||||||
* Draws a string centered based on given restrictions
|
|
||||||
*
|
|
||||||
* @param s String to be drawn
|
|
||||||
* @param w Constraining width
|
|
||||||
* @param h Constraining height
|
|
||||||
* @param y Fixed y-coordinate
|
|
||||||
*/
|
|
||||||
public void drawCentered (String s, int w, int h, int y, Graphics g) {
|
|
||||||
FontMetrics fm = g.getFontMetrics();
|
|
||||||
|
|
||||||
// Calculate x-coordinate based on string length and width
|
|
||||||
int x = (w - fm.stringWidth(s)) / 2;
|
|
||||||
g.drawString(s, x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void drawMenu (Graphics g) {
|
|
||||||
// Title
|
// Title
|
||||||
g.drawImage(FlappyBird.textures.get("titleText").getImage(),
|
g.drawImage(FlappyBird.textures.get("titleText").getImage(),
|
||||||
FlappyBird.textures.get("titleText").getX(),
|
FlappyBird.textures.get("titleText").getX(),
|
||||||
@ -57,15 +41,29 @@ public class MenuState extends GameState {
|
|||||||
FlappyBird.textures.get("rateButton").getY(), null);
|
FlappyBird.textures.get("rateButton").getY(), null);
|
||||||
|
|
||||||
// Credits :p
|
// Credits :p
|
||||||
drawCentered("Created by Paul Krishnamurthy", FlappyBird.WIDTH, FlappyBird.HEIGHT, 600, g);
|
paintCenteredText("Created by Paul Krishnamurthy", 600, g);
|
||||||
g.setFont(FlappyBird.flappyMiniFont); // Change font
|
g.setFont(FlappyBird.flappyMiniFont); // Change font
|
||||||
drawCentered("www.PaulKr.com", FlappyBird.WIDTH, FlappyBird.HEIGHT, 630, g);
|
paintCenteredText("www.PaulKr.com", 630, g);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws a string centered based on given restrictions
|
||||||
|
*
|
||||||
|
* @param text String to be drawn
|
||||||
|
* @param yPos Fixed y-coordinate
|
||||||
|
*/
|
||||||
|
protected void paintCenteredText(String text, int yPos, Graphics g) {
|
||||||
|
FontMetrics fm = g.getFontMetrics();
|
||||||
|
|
||||||
|
// Calculate x-coordinate based on string length and width
|
||||||
|
int x = (FlappyBird.WIDTH - fm.stringWidth(text)) / 2;
|
||||||
|
g.drawString(text, x, yPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tries to open the review url in default web browser
|
* Tries to open the review url in default web browser
|
||||||
*/
|
*/
|
||||||
public void openReviewUrl() {
|
protected void openReviewUrl() {
|
||||||
try {
|
try {
|
||||||
if (Desktop.isDesktopSupported()) {
|
if (Desktop.isDesktopSupported()) {
|
||||||
Desktop.getDesktop().browse(new URI("http://paulkr.com"));
|
Desktop.getDesktop().browse(new URI("http://paulkr.com"));
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package com.example.flappybird.states;
|
package com.example.flappybird.states;
|
||||||
|
|
||||||
import com.example.flappybird.FlappyBird;
|
import com.example.flappybird.*;
|
||||||
import com.example.flappybird.GamePanel;
|
|
||||||
import com.example.flappybird.Highscore;
|
|
||||||
import com.example.flappybird.Pipe;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@ -23,7 +20,42 @@ public class PlayState extends GameState {
|
|||||||
super(panel);
|
super(panel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startGameScreen (Graphics g) {
|
@Override
|
||||||
|
public void paintGameState(Graphics g) {
|
||||||
|
gamePanel.moveBase(g);
|
||||||
|
|
||||||
|
if (gameBird.isAlive()) {
|
||||||
|
// Start at instructions state
|
||||||
|
if (inStartGameState) {
|
||||||
|
paintStartScreen(g);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Start game
|
||||||
|
movePipes(g);
|
||||||
|
gameBird.moveBird();
|
||||||
|
if(gameBird.collidesWithBase()) {
|
||||||
|
// Play audio and set state to dead
|
||||||
|
gameBird.killBird();
|
||||||
|
FlappyBird.audio.hit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw base over pipes
|
||||||
|
gamePanel.paintBase(g);
|
||||||
|
|
||||||
|
// Draw player score
|
||||||
|
paintScore(g, score, false, 0, 0);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
movePipes(g);
|
||||||
|
gamePanel.paintBase(g);
|
||||||
|
|
||||||
|
// Draw game over assets
|
||||||
|
paintScoreBoard(g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void paintStartScreen(Graphics g) {
|
||||||
// Set bird's new position
|
// Set bird's new position
|
||||||
gameBird.setGameStartPos();
|
gameBird.setGameStartPos();
|
||||||
|
|
||||||
@ -44,9 +76,9 @@ public class PlayState extends GameState {
|
|||||||
* @param mini Boolean for drawing small or large numbers
|
* @param mini Boolean for drawing small or large numbers
|
||||||
* @param x X-coordinate to draw for mini numbers
|
* @param x X-coordinate to draw for mini numbers
|
||||||
*/
|
*/
|
||||||
public void drawScore (Graphics g, int drawNum, boolean mini, int x, int y) {
|
public void paintScore(Graphics g, int drawNum, boolean mini, int x, int y) {
|
||||||
// Char array of digits
|
// Char array of digits
|
||||||
char[] digits = ("" + drawNum).toCharArray();
|
char[] digits = (String.valueOf(drawNum)).toCharArray();
|
||||||
|
|
||||||
// Calculate width for numeric textures
|
// Calculate width for numeric textures
|
||||||
int takeUp = 0;
|
int takeUp = 0;
|
||||||
@ -65,7 +97,8 @@ public class PlayState extends GameState {
|
|||||||
|
|
||||||
// Draw every digit
|
// Draw every digit
|
||||||
for (char digit : digits) {
|
for (char digit : digits) {
|
||||||
g.drawImage(FlappyBird.textures.get((mini ? "mini-score-" : "score-") + digit).getImage(), drawScoreX, (mini ? y : 60), null);
|
Texture texture = FlappyBird.textures.get((mini ? "mini-score-" : "score-") + digit);
|
||||||
|
g.drawImage(texture.getImage(), drawScoreX, (mini ? y : 60), null);
|
||||||
|
|
||||||
// Size to add varies based on texture
|
// Size to add varies based on texture
|
||||||
if (mini) {
|
if (mini) {
|
||||||
@ -79,7 +112,7 @@ public class PlayState extends GameState {
|
|||||||
/**
|
/**
|
||||||
* Moves and repositions pipes
|
* Moves and repositions pipes
|
||||||
*/
|
*/
|
||||||
public void pipeHandler (Graphics g) {
|
protected void movePipes(Graphics g) {
|
||||||
|
|
||||||
// Decrease distance between pipes
|
// Decrease distance between pipes
|
||||||
if (gameBird.isAlive()) {
|
if (gameBird.isAlive()) {
|
||||||
@ -162,8 +195,8 @@ public class PlayState extends GameState {
|
|||||||
// Kill bird and play sound
|
// Kill bird and play sound
|
||||||
gameBird.killBird();
|
gameBird.killBird();
|
||||||
FlappyBird.audio.hit();
|
FlappyBird.audio.hit();
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
// Checks if bird passes a pipe
|
// Checks if bird passes a pipe
|
||||||
if (gameBird.getX() >= p.getX() + Pipe.WIDTH / 2) {
|
if (gameBird.getX() >= p.getX() + Pipe.WIDTH / 2) {
|
||||||
|
|
||||||
@ -179,7 +212,7 @@ public class PlayState extends GameState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void gameOver (Graphics g) {
|
protected void paintScoreBoard(Graphics g) {
|
||||||
// Game over text
|
// Game over text
|
||||||
g.drawImage(FlappyBird.textures.get("gameOverText").getImage(),
|
g.drawImage(FlappyBird.textures.get("gameOverText").getImage(),
|
||||||
FlappyBird.textures.get("gameOverText").getX(),
|
FlappyBird.textures.get("gameOverText").getX(),
|
||||||
@ -198,8 +231,8 @@ public class PlayState extends GameState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Draw mini fonts for current and best scores
|
// Draw mini fonts for current and best scores
|
||||||
drawScore(g, score, true, 303, 276);
|
paintScore(g, score, true, 303, 276);
|
||||||
drawScore(g, highscore.bestScore(), true, 303, 330);
|
paintScore(g, highscore.bestScore(), true, 303, 330);
|
||||||
|
|
||||||
// Handle highscore
|
// Handle highscore
|
||||||
if (score > highscore.bestScore()) {
|
if (score > highscore.bestScore()) {
|
||||||
@ -231,35 +264,6 @@ public class PlayState extends GameState {
|
|||||||
FlappyBird.textures.get("leaderboard").getY(), null);
|
FlappyBird.textures.get("leaderboard").getY(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void renderGameState(Graphics g) {
|
|
||||||
prepareScreen(g);
|
|
||||||
|
|
||||||
if (gameBird.isAlive()) {
|
|
||||||
|
|
||||||
// Start at instructions state
|
|
||||||
if (inStartGameState) {
|
|
||||||
startGameScreen(g);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// Start game
|
|
||||||
pipeHandler(g);
|
|
||||||
gameBird.inGameBorders();
|
|
||||||
}
|
|
||||||
|
|
||||||
drawBase(g); // Draw base over pipes
|
|
||||||
drawScore(g, score, false, 0, 0); // Draw player score
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
pipeHandler(g);
|
|
||||||
drawBase(g);
|
|
||||||
|
|
||||||
// Draw game over assets
|
|
||||||
gameOver(g);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleKeyboardEvent(KeyEvent e) {
|
public void handleKeyboardEvent(KeyEvent e) {
|
||||||
int keyCode = e.getKeyCode();
|
int keyCode = e.getKeyCode();
|
||||||
|
Loading…
Reference in New Issue
Block a user