Finalize refactoring

This commit is contained in:
Jan-Niclas Loosen 2024-11-28 19:13:20 +01:00
parent a7d68e5ef6
commit 08bca1c0db
9 changed files with 161 additions and 24 deletions

View File

@ -7,7 +7,6 @@
package com.example.flappybird; package com.example.flappybird;
import com.example.flappybird.states.GameState;
import java.awt.*; import java.awt.*;
import java.awt.geom.AffineTransform; import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
@ -33,6 +32,7 @@ public class Bird extends HasPosition {
// Bird sprites // Bird sprites
private final BufferedImage[] sprites; private final BufferedImage[] sprites;
// Track current frame
private static double currentFrame = 0; private static double currentFrame = 0;
public Bird (int x, int y) { public Bird (int x, int y) {
@ -150,7 +150,6 @@ public class Bird extends HasPosition {
// Create bird animation and pass in rotation angle // Create bird animation and pass in rotation angle
animateBird(g, rotation); animateBird(g, rotation);
} }
private void animateBird(Graphics g, double angle) { private void animateBird(Graphics g, double angle) {
@ -173,7 +172,9 @@ public class Bird extends HasPosition {
// Switch animation frames // Switch animation frames
if (currentFrame >= count - 1) { if (currentFrame >= count - 1) {
currentFrame = 0; currentFrame = 0;
} else currentFrame += 0.09; }
else {
currentFrame += 0.09;
}
} }
} }

View File

@ -45,18 +45,21 @@ public class FlappyBird implements ActionListener {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT); frame.setSize(WIDTH, HEIGHT);
String fontPath = "/res/fonts/flappy-font.ttf";
// Try to load ttf file // Try to load ttf file
try { 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); flappyFontBase = Font.createFont(Font.TRUETYPE_FONT, is);
// Header and sub-header fonts // Header and sub-header fonts
flappyScoreFont = flappyFontBase.deriveFont(Font.PLAIN, 50); flappyScoreFont = flappyFontBase.deriveFont(Font.PLAIN, 50);
flappyFontReal = flappyFontBase.deriveFont(Font.PLAIN, 20); flappyFontReal = flappyFontBase.deriveFont(Font.PLAIN, 20);
flappyMiniFont = flappyFontBase.deriveFont(Font.PLAIN, 15); flappyMiniFont = flappyFontBase.deriveFont(Font.PLAIN, 15);
} catch (Exception ex) { } catch (Exception ex) {
// Exit is font cannot be loaded // Exit is font cannot be loaded
ex.printStackTrace(); ex.printStackTrace();
System.err.println("Could not load Flappy Font!"); System.err.println("Could not load Flappy Font!");

View File

@ -78,10 +78,6 @@ public class GamePanel extends JPanel implements KeyListener, MouseListener {
// Mouse actions // // Mouse actions //
/////////////////// ///////////////////
public Point getClickedPoint() {
return clickedPoint;
}
/** /**
* Checks if point is in rectangle * Checks if point is in rectangle
* *

View File

@ -12,11 +12,17 @@ public class Pipe extends HasPosition {
String location; String location;
// Pipe constants // Pipe constants
public static final int WIDTH = 67; public static final int WIDTH = 67;
public static final int HEIGHT = 416; 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 // Horizontal distance between pipes
private static final int SPEED = -2; 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 // If the bird can get a point passing this pipe
public boolean canAwardPoint = true; public boolean canAwardPoint = true;

View File

@ -16,7 +16,6 @@ public class Texture extends HasPosition {
private final BufferedImage image; private final BufferedImage image;
private final Rectangle rect; private final Rectangle rect;
public Texture (BufferedImage image, int x, int y) { public Texture (BufferedImage image, int x, int y) {
super(x, y); super(x, y);

View File

@ -17,6 +17,7 @@ public abstract class GameState {
// Game variables // // Game variables //
//////////////////// ////////////////////
// Background
protected static final int[] baseCoords = { 0, 435 }; protected static final int[] baseCoords = { 0, 435 };
// Player score // Player score
@ -31,8 +32,10 @@ public abstract class GameState {
// Arraylist of Pipe objects // Arraylist of Pipe objects
public static ArrayList<Pipe> pipes; public static ArrayList<Pipe> pipes;
// Game bird
protected static Bird gameBird; protected static Bird gameBird;
// Game panel
public GamePanel gamePanel; public GamePanel gamePanel;
public GameState(GamePanel panel) { public GameState(GamePanel panel) {
@ -45,9 +48,9 @@ public abstract class GameState {
public void restart () { public void restart () {
// Reset game statistics // Reset game statistics
score = 0; score = 0;
pipeDistTracker = 0; pipeDistTracker = 0;
scoreWasGreater = false; scoreWasGreater = false;
// Game bird // Game bird
gameBird = new Bird(172, 250); gameBird = new Bird(172, 250);
@ -63,13 +66,11 @@ public abstract class GameState {
// Only move screen if bird is alive // Only move screen if bird is alive
if (gameBird.isAlive()) { if (gameBird.isAlive()) {
// Move base // Move base
// Moving base effect // Moving base effect
int baseSpeed = 2; int baseSpeed = 2;
baseCoords[0] = baseCoords[0] - baseSpeed < -435 ? 435 : baseCoords[0] - baseSpeed; baseCoords[0] = baseCoords[0] - baseSpeed < -435 ? 435 : baseCoords[0] - baseSpeed;
baseCoords[1] = baseCoords[1] - baseSpeed < -435 ? 435 : baseCoords[1] - baseSpeed; baseCoords[1] = baseCoords[1] - baseSpeed < -435 ? 435 : baseCoords[1] - baseSpeed;
} }
// Background // Background
@ -96,4 +97,3 @@ public abstract class GameState {
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

@ -142,7 +142,6 @@ public class PlayState extends GameState {
} }
// Move and draw each pipe // Move and draw each pipe
for (Pipe p : pipes) { for (Pipe p : pipes) {
// Move the pipe // Move the pipe

Binary file not shown.

After

Width:  |  Height:  |  Size: 189 KiB

View File

@ -0,0 +1,133 @@
@startuml
!theme plain
top to bottom direction
skinparam linetype ortho
class Audio {
+ Audio():
- playSound(String): void
+ jump(): void
+ point(): void
+ hit(): void
}
class Bird {
+ Bird(int, int):
- isAlive: boolean
+ renderBird(Graphics): void
+ jump(): void
+ killBird(): void
+ menuFloat(): void
+ inGameBorders(): void
- animateBird(Graphics, double): void
+ setGameStartPos(): void
isAlive: boolean
}
class FlappyBird {
+ FlappyBird():
+ actionPerformed(ActionEvent): void
+ main(String[]): void
}
class GamePanel {
+ GamePanel():
- gameState: GameState
# clickedPoint: Point
+ keyReleased(KeyEvent): void
+ mouseEntered(MouseEvent): void
+ keyPressed(KeyEvent): void
+ mousePressed(MouseEvent): void
+ paintComponent(Graphics): void
+ keyTyped(KeyEvent): void
+ mouseClicked(MouseEvent): void
+ mouseReleased(MouseEvent): void
+ isTouching(Rectangle): boolean
+ addNotify(): void
+ mouseExited(MouseEvent): void
gameState: GameState
clickedPoint: Point
}
class GameState {
+ GameState(GamePanel):
+ prepareScreen(Graphics): void
+ renderGameState(Graphics): void
+ handleMouseEvent(MouseEvent): void
+ handleKeyboardEvent(KeyEvent): void
+ restart(): void
+ drawBase(Graphics): void
}
class HasPosition {
+ HasPosition(int, int):
# y: int
# x: int
y: int
x: int
}
class Highscore {
+ Highscore():
+ bestScore(): int
newBest: int
}
class MenuState {
+ MenuState(GamePanel):
+ handleKeyboardEvent(KeyEvent): void
# drawMenu(Graphics): void
+ drawCentered(String, int, int, int, Graphics): void
+ handleMouseEvent(MouseEvent): void
+ renderGameState(Graphics): void
+ openReviewUrl(): void
}
class Pipe {
+ Pipe(String):
+ move(): void
+ reset(): void
+ collide(Bird): boolean
y: int
}
class PlayState {
+ PlayState(GamePanel):
+ drawScore(Graphics, int, boolean, int, int): void
+ startGameScreen(Graphics): void
+ pipeHandler(Graphics): void
+ handleMouseEvent(MouseEvent): void
+ gameOver(Graphics): void
+ renderGameState(Graphics): void
+ handleKeyboardEvent(KeyEvent): void
}
class Sprites {
+ Sprites():
- putTexture(String, int, int, int, int, int, int): void
- resize(BufferedImage): BufferedImage
gameTextures: HashMap<String, Texture>
}
class Texture {
+ Texture(BufferedImage, int, int):
- image: BufferedImage
- rect: Rectangle
image: BufferedImage
rect: Rectangle
}
Bird -[#000082,plain]-^ HasPosition
FlappyBird -[#595959,dashed]-> Audio : "«create»"
FlappyBird "1" *-[#595959,plain]-> "audio\n1" Audio
FlappyBird -[#595959,dashed]-> GamePanel : "«create»"
FlappyBird "1" *-[#595959,plain]-> "game\n1" GamePanel
FlappyBird -[#595959,dashed]-> Sprites : "«create»"
FlappyBird "1" *-[#595959,plain]-> "textures\n*" Texture
GamePanel "1" *-[#595959,plain]-> "gameState\n1" GameState
GamePanel -[#595959,dashed]-> MenuState : "«create»"
GameState -[#595959,dashed]-> Bird : "«create»"
GameState "1" *-[#595959,plain]-> "gameBird\n1" Bird
GameState "1" *-[#595959,plain]-> "gamePanel\n1" GamePanel
GameState "1" *-[#595959,plain]-> "pipes\n*" Pipe
MenuState -[#000082,plain]-^ GameState
MenuState -[#595959,dashed]-> PlayState : "«create»"
Pipe -[#000082,plain]-^ HasPosition
PlayState -[#000082,plain]-^ GameState
PlayState -[#595959,dashed]-> Highscore : "«create»"
PlayState "1" *-[#595959,plain]-> "highscore\n1" Highscore
PlayState -[#595959,dashed]-> Pipe : "«create»"
Sprites "1" *-[#595959,plain]-> "textures\n*" Texture
Sprites -[#595959,dashed]-> Texture : "«create»"
Texture -[#000082,plain]-^ HasPosition
@enduml