Continue refactoring
This commit is contained in:
parent
4df15dc254
commit
7af9624252
@ -8,7 +8,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
@ -1,18 +1,20 @@
|
||||
package com.example.flappybird; /**
|
||||
/**
|
||||
* Audio.java
|
||||
* Plays all sound effects
|
||||
*
|
||||
* @author Paul Krishnamurthy
|
||||
*/
|
||||
|
||||
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 void playSound (String sound) {
|
||||
|
||||
private void playSound (String sound) {
|
||||
// Path to sound file
|
||||
String soundPath = "/res/sound/" + sound + ".wav";
|
||||
|
||||
|
@ -1,12 +1,13 @@
|
||||
package com.example.flappybird; /**
|
||||
/**
|
||||
* Bird.java
|
||||
* Handles bird's state and actions
|
||||
*
|
||||
* @author Paul Krishnamurthy
|
||||
*/
|
||||
|
||||
import com.example.flappybird.states.GameState;
|
||||
package com.example.flappybird;
|
||||
|
||||
import com.example.flappybird.states.GameState;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.image.BufferedImage;
|
||||
@ -15,7 +16,6 @@ public class Bird extends HasPosition {
|
||||
|
||||
// Bird attributes
|
||||
public String color;
|
||||
|
||||
private boolean isAlive = true;
|
||||
|
||||
// Bird constants
|
||||
@ -23,18 +23,14 @@ public class Bird extends HasPosition {
|
||||
public final int BIRD_WIDTH = 44;
|
||||
public final int BIRD_HEIGHT = 31;
|
||||
private final int BASE_COLLISION = 521 - BIRD_HEIGHT - 5;
|
||||
private final int SHIFT = 10;
|
||||
private final int STARTING_BIRD_X = 90;
|
||||
private final int STARTING_BIRD_Y = 343;
|
||||
|
||||
// Physics variables
|
||||
private double velocity = 0;
|
||||
private double gravity = .41;
|
||||
private final double gravity = .41;
|
||||
private double delay = 0;
|
||||
private double rotation = 0;
|
||||
|
||||
// Bird sprites
|
||||
private BufferedImage[] sprites;
|
||||
private final BufferedImage[] sprites;
|
||||
|
||||
private static double currentFrame = 0;
|
||||
|
||||
@ -45,20 +41,6 @@ public class Bird extends HasPosition {
|
||||
this.sprites = s;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Bird's x-coordinate
|
||||
*/
|
||||
public int getX () {
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Bird's y-coordinate
|
||||
*/
|
||||
public int getY () {
|
||||
return y;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return If bird is alive
|
||||
*/
|
||||
@ -69,7 +51,7 @@ public class Bird extends HasPosition {
|
||||
/**
|
||||
* Kills bird
|
||||
*/
|
||||
public void kill () {
|
||||
public void killBird() {
|
||||
isAlive = false;
|
||||
}
|
||||
|
||||
@ -77,15 +59,14 @@ public class Bird extends HasPosition {
|
||||
* Set new coordinates when starting game
|
||||
*/
|
||||
public void setGameStartPos () {
|
||||
x = STARTING_BIRD_X;
|
||||
y = STARTING_BIRD_Y;
|
||||
x = 90;
|
||||
y = 343;
|
||||
}
|
||||
|
||||
/**
|
||||
* Floating bird effect on menu screen
|
||||
*/
|
||||
public void menuFloat () {
|
||||
|
||||
y += FLOAT_MULTIPLIER;
|
||||
|
||||
// Change direction within floating range
|
||||
@ -101,22 +82,19 @@ public class Bird extends HasPosition {
|
||||
* Bird jump
|
||||
*/
|
||||
public void jump () {
|
||||
|
||||
if (delay < 1) {
|
||||
int SHIFT = 10;
|
||||
velocity = -SHIFT;
|
||||
delay = SHIFT;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Bird movement during the game
|
||||
*/
|
||||
public void inGame () {
|
||||
|
||||
public void inGameBorders () {
|
||||
// If the bird did not hit the base, lower it
|
||||
if (y < BASE_COLLISION) {
|
||||
|
||||
// Change and velocity
|
||||
velocity += gravity;
|
||||
|
||||
@ -125,9 +103,7 @@ public class Bird extends HasPosition {
|
||||
|
||||
// Add rounded velocity to y-coordinate
|
||||
y += (int) velocity;
|
||||
|
||||
} else {
|
||||
|
||||
// Play audio and set state to dead
|
||||
GameState.audio.hit();
|
||||
isAlive = false;
|
||||
@ -139,9 +115,8 @@ public class Bird extends HasPosition {
|
||||
* Renders bird
|
||||
*/
|
||||
public void renderBird (Graphics g) {
|
||||
|
||||
// Calculate angle to rotate bird based on y-velocity
|
||||
rotation = ((90 * (velocity + 25) / 25) - 90) * Math.PI / 180;
|
||||
double rotation = ((90 * (velocity + 25) / 25) - 90) * Math.PI / 180;
|
||||
|
||||
// Divide for clean jump
|
||||
rotation /= 2;
|
||||
|
@ -1,10 +1,12 @@
|
||||
package com.example.flappybird; /**
|
||||
/**
|
||||
* FlappyBird.java
|
||||
* Main game class
|
||||
*
|
||||
* @author Paul Krishnamurthy
|
||||
*/
|
||||
|
||||
package com.example.flappybird;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.Toolkit;
|
||||
@ -20,7 +22,6 @@ public class FlappyBird extends JFrame implements ActionListener {
|
||||
private static final int DELAY = 12;
|
||||
|
||||
public FlappyBird () {
|
||||
|
||||
super("Flappy Bird");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setSize(WIDTH, HEIGHT);
|
||||
@ -46,9 +47,8 @@ public class FlappyBird extends JFrame implements ActionListener {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
FlappyBird game = new FlappyBird();
|
||||
new FlappyBird();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,32 +1,24 @@
|
||||
package com.example.flappybird; /**
|
||||
/**
|
||||
* GamePanel.java
|
||||
* Main game panel
|
||||
*
|
||||
* @author Paul Krishnamurthy
|
||||
*/
|
||||
|
||||
package com.example.flappybird;
|
||||
|
||||
import com.example.flappybird.states.GameState;
|
||||
import com.example.flappybird.states.MenuState;
|
||||
|
||||
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;
|
||||
import java.io.InputStream;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Calendar;
|
||||
|
||||
public class GamePanel extends JPanel implements KeyListener, MouseListener {
|
||||
// Textures
|
||||
private GameState gameState;
|
||||
|
||||
public boolean ready = false; // If game has loaded
|
||||
|
||||
// If game has loaded
|
||||
public boolean ready = false;
|
||||
|
||||
public GamePanel () {
|
||||
setGameState(new MenuState(this));
|
||||
@ -42,6 +34,10 @@ public class GamePanel extends JPanel implements KeyListener, MouseListener {
|
||||
addMouseListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current game state.
|
||||
* @param state New GameState
|
||||
*/
|
||||
public void setGameState(GameState state) {
|
||||
System.out.println("Switching to state: " + state.getClass().getName());
|
||||
this.gameState = state;
|
||||
|
@ -1,30 +1,30 @@
|
||||
package com.example.flappybird; /**
|
||||
/**
|
||||
* Highscore.java
|
||||
* Handles setting and getting highscores
|
||||
*
|
||||
* @author Paul Krishnamurthy
|
||||
*/
|
||||
|
||||
package com.example.flappybird;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Scanner;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
|
||||
public class Highscore {
|
||||
|
||||
// Read / Write to file setup
|
||||
private static final String FILE_PATH = "/res/data/highscore.dat";
|
||||
|
||||
private static final URL dataURL = Highscore.class.getResource(FILE_PATH);
|
||||
|
||||
private static final File dataFile;
|
||||
|
||||
// Highscore
|
||||
private int bestScore;
|
||||
|
||||
static {
|
||||
try {
|
||||
assert dataURL != null;
|
||||
@ -36,9 +36,6 @@ public class Highscore {
|
||||
|
||||
private static Scanner dataScanner = null;
|
||||
|
||||
// Highscore
|
||||
private int bestScore;
|
||||
|
||||
public Highscore () {
|
||||
|
||||
// Load scanner with data file
|
||||
@ -62,20 +59,18 @@ public class Highscore {
|
||||
|
||||
/**
|
||||
* Sets new highscore in the data file
|
||||
*
|
||||
* @param newBest New score update
|
||||
*/
|
||||
public void setNewBest (int newBest) {
|
||||
|
||||
// Set new best score
|
||||
bestScore = newBest;
|
||||
|
||||
try {
|
||||
// Write new highscore to data file
|
||||
PrintWriter dataWriter = new PrintWriter(FILE_PATH, "UTF-8");
|
||||
dataWriter.println(Integer.toString(newBest));
|
||||
PrintWriter dataWriter = new PrintWriter(FILE_PATH, StandardCharsets.UTF_8);
|
||||
dataWriter.println(newBest);
|
||||
dataWriter.close();
|
||||
} catch (FileNotFoundException | UnsupportedEncodingException e) {
|
||||
} catch (IOException e) {
|
||||
System.out.println("Could not set new highscore!");
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,3 @@
|
||||
package com.example.flappybird;
|
||||
|
||||
/**
|
||||
* Pipe.java
|
||||
* Handles collisions and rendering for pipes
|
||||
@ -7,6 +5,8 @@ package com.example.flappybird;
|
||||
* @author Paul Krishnamurthy
|
||||
*/
|
||||
|
||||
package com.example.flappybird;
|
||||
|
||||
public class Pipe extends HasPosition {
|
||||
// Placement (top or bottom) of pipe
|
||||
String location;
|
||||
@ -31,7 +31,7 @@ public class Pipe extends HasPosition {
|
||||
super.x = FlappyBird.WIDTH + 5; // Reset x-coordinate
|
||||
|
||||
// Set boundaries for top pipes
|
||||
// This y-coordinte + PIPE_SPACING will be for the bottom pipe
|
||||
// This y-coordinate + PIPE_SPACING will be for the bottom pipe
|
||||
if (location.equals("top")) {
|
||||
super.y = - Math.max((int) (Math.random() * 320) + 30, 140);
|
||||
}
|
||||
@ -46,7 +46,6 @@ public class Pipe extends HasPosition {
|
||||
|
||||
/**
|
||||
* Checks for bird colliding with pipe
|
||||
*
|
||||
* @param bird The bird.
|
||||
* @return If bird is colliding with the pipe
|
||||
*/
|
||||
@ -64,7 +63,6 @@ public class Pipe extends HasPosition {
|
||||
|
||||
/**
|
||||
* Set's pipe's y-coordinate (for bottom pipes)
|
||||
*
|
||||
* @param newY New y-coordinate
|
||||
*/
|
||||
public void setY (int newY) {
|
||||
|
@ -1,14 +1,15 @@
|
||||
package com.example.flappybird; /**
|
||||
/**
|
||||
* Sprites.java
|
||||
* Cuts up the main sprite sheet
|
||||
*
|
||||
* @author Paul Krishnamurthy
|
||||
*/
|
||||
|
||||
package com.example.flappybird;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.Graphics2D;
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
@ -18,13 +19,12 @@ public class Sprites {
|
||||
// Resize factor to match frame size
|
||||
private static final double RESIZE_FACTOR = 2.605;
|
||||
|
||||
private static BufferedImage spriteSheet = null;
|
||||
|
||||
// HashMap of texture objects
|
||||
private static HashMap<String, Texture> textures = new HashMap<String, Texture>();
|
||||
private static final HashMap<String, Texture> textures = new HashMap<String, Texture>();
|
||||
|
||||
public Sprites () {
|
||||
// Try to load sprite sheet, exit program if cannot
|
||||
BufferedImage spriteSheet = null;
|
||||
try {
|
||||
String fontPath = "/res/img/spriteSheet.png";
|
||||
URL fontUrl = this.getClass().getResource(fontPath);
|
||||
|
@ -1,10 +1,12 @@
|
||||
package com.example.flappybird; /**
|
||||
/**
|
||||
* Texture.java
|
||||
* Stores data for game textures
|
||||
*
|
||||
* @author Paul Krishnamurthy
|
||||
*/
|
||||
|
||||
package com.example.flappybird;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.Rectangle;
|
||||
|
||||
@ -36,6 +38,5 @@ public class Texture extends HasPosition {
|
||||
public Rectangle getRect () {
|
||||
return rect;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -41,16 +41,15 @@ public abstract class GameState {
|
||||
|
||||
protected int score; // Player score
|
||||
protected int pipeDistTracker; // Distance between pipes
|
||||
protected boolean inStartGameState = false; // To show instructions scren
|
||||
protected Point clickedPoint = new Point(-1, -1); // Store point when player clicks
|
||||
|
||||
protected boolean scoreWasGreater; // If score was greater than previous highscore
|
||||
protected String medal; // Medal to be awarded after each game
|
||||
|
||||
protected Point clickedPoint = new Point(-1, -1); // Store point when player clicks
|
||||
public static ArrayList<Pipe> pipes; // Arraylist of Pipe objects
|
||||
|
||||
protected static boolean darkTheme; // Boolean to show dark or light screen
|
||||
|
||||
protected static Bird gameBird;
|
||||
protected final static Highscore highscore = new Highscore();
|
||||
|
||||
public GamePanel gamePanel;
|
||||
|
||||
|
@ -2,6 +2,7 @@ package com.example.flappybird.states;
|
||||
|
||||
import com.example.flappybird.FlappyBird;
|
||||
import com.example.flappybird.GamePanel;
|
||||
import com.example.flappybird.Highscore;
|
||||
import com.example.flappybird.Pipe;
|
||||
|
||||
import javax.swing.*;
|
||||
@ -10,13 +11,16 @@ import java.awt.event.KeyEvent;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
public class PlayState extends GameState {
|
||||
protected boolean inStartGameState = false; // To show instructions screen
|
||||
protected String medal; // Medal to be awarded after each game
|
||||
|
||||
protected final Highscore highscore = new Highscore();
|
||||
|
||||
public PlayState(GamePanel panel) {
|
||||
super(panel);
|
||||
}
|
||||
|
||||
public void startGameScreen (Graphics g) {
|
||||
|
||||
// Set bird's new position
|
||||
gameBird.setGameStartPos();
|
||||
|
||||
@ -29,7 +33,6 @@ public class PlayState extends GameState {
|
||||
g.drawImage(textures.get("instructions").getImage(),
|
||||
textures.get("instructions").getX(),
|
||||
textures.get("instructions").getY(), null);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -159,7 +162,7 @@ public class PlayState extends GameState {
|
||||
if (gameBird.isAlive()) {
|
||||
if (p.collide(gameBird)) {
|
||||
// Kill bird and play sound
|
||||
gameBird.kill();
|
||||
gameBird.killBird();
|
||||
audio.hit();
|
||||
} else {
|
||||
|
||||
@ -245,7 +248,7 @@ public class PlayState extends GameState {
|
||||
} else {
|
||||
// Start game
|
||||
pipeHandler(g);
|
||||
gameBird.inGame();
|
||||
gameBird.inGameBorders();
|
||||
}
|
||||
|
||||
drawBase(g); // Draw base over pipes
|
||||
@ -300,12 +303,10 @@ public class PlayState extends GameState {
|
||||
// On game over screen, allow restart and leaderboard buttons
|
||||
if (isTouching(textures.get("playButton").getRect())) {
|
||||
inStartGameState = true;
|
||||
gamePanel.setGameState(new PlayState(gamePanel));
|
||||
restart();
|
||||
gamePanel.setGameState(this);
|
||||
gameBird.setGameStartPos();
|
||||
|
||||
restart();
|
||||
} else if (isTouching(textures.get("leaderboard").getRect())) {
|
||||
|
||||
// Dummy message
|
||||
JOptionPane.showMessageDialog(gamePanel,
|
||||
"We can't access the leaderboard right now!",
|
||||
|
Loading…
Reference in New Issue
Block a user