finish refactoring

This commit is contained in:
Jan-Niclas Loosen 2024-11-28 23:23:28 +01:00
parent 08bca1c0db
commit c93b8d02b6
8 changed files with 128 additions and 132 deletions

View File

@ -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>

View File

@ -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;

View File

@ -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);
} }
////////////////////// //////////////////////

View File

@ -27,13 +27,13 @@ public class Pipe extends HasPosition {
// If the bird can get a point passing this pipe // If the bird can get a point passing this pipe
public boolean canAwardPoint = true; public boolean canAwardPoint = true;
public Pipe (String location) { public Pipe(String location) {
super(FlappyBird.WIDTH + 5, 0); super(FlappyBird.WIDTH + 5, 0);
this.location = location; this.location = location;
reset(); reset();
} }
public void reset () { public void reset() {
super.x = FlappyBird.WIDTH + 5; // Reset x-coordinate super.x = FlappyBird.WIDTH + 5; // Reset x-coordinate
// Set boundaries for top pipes // Set boundaries for top pipes
@ -46,7 +46,7 @@ public class Pipe extends HasPosition {
/** /**
* Moves the pipe * Moves the pipe
*/ */
public void move () { public void move() {
super.x += SPEED; super.x += SPEED;
} }
@ -55,7 +55,7 @@ public class Pipe extends HasPosition {
* @param bird The bird. * @param bird The bird.
* @return If bird is colliding with the pipe * @return If bird is colliding with the pipe
*/ */
public boolean collide (Bird bird) { public boolean collide(Bird bird) {
// Do not allow bird to jump over pipe // Do not allow bird to jump over pipe
if (bird.getX() > super.x && bird.getY() < 0 && canAwardPoint) { if (bird.getX() > super.x && bird.getY() < 0 && canAwardPoint) {
return true; return true;
@ -71,7 +71,7 @@ public class Pipe extends HasPosition {
* Set's pipe's y-coordinate (for bottom pipes) * Set's pipe's y-coordinate (for bottom pipes)
* @param newY New y-coordinate * @param newY New y-coordinate
*/ */
public void setY (int newY) { public void setY(int newY) {
y = newY; y = newY;
} }
} }

View File

@ -135,7 +135,7 @@ public class Sprites {
* *
* @return Texture * @return Texture
*/ */
public HashMap<String, Texture> getGameTextures () { public HashMap<String, Texture> getGameTextures() {
return textures; return textures;
} }

View File

@ -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);
} }

View File

@ -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"));

View File

@ -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();