final/src/GamePanel.java

308 lines
12 KiB
Java
Raw Normal View History

2022-05-30 19:36:18 +01:00
/* Eric Li, ICS4U, Completed 5/29/2022
GamePanel class acts as the main "game loop" - continuously runs the game and calls whatever needs to be called
Child of JPanel because JPanel contains methods for drawing to the screen
Implements KeyListener interface to listen for keyboard input
Implements Runnable interface to use "threading" - let the game do two things at once
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.NoSuchElementException;
import java.util.Scanner;
import javax.swing.*;
public class GamePanel extends JPanel implements Runnable, KeyListener{
//dimensions of window
public static final int GAME_WIDTH = 1200;
public static final int GAME_HEIGHT = 600;
// score needed to win
public static final int WIN_SCORE = 5;
public Thread gameThread;
public Image image;
public Graphics graphics;
public Ball ball;
public Paddle leftPaddle, rightPaddle;
public boolean leftWin, rightWin;
public int leftScore, rightScore;
public String saveFile = "save.txt";
public boolean resumeGame = false;
public int paintNumber = 0;
// constructor initializes one Ball, two Paddles, and starts the Thread
public GamePanel(){
ball = new Ball((GAME_WIDTH-Ball.BALL_DIAMETER)/2, (GAME_HEIGHT-Ball.BALL_DIAMETER)/2, 1); //create a player controlled ball, set start location to middle of screen
// starting position of paddles are randomized
leftPaddle = new Paddle(10, (int)(Math.random() * GAME_HEIGHT), KeyEvent.VK_W, KeyEvent.VK_S);
rightPaddle = new Paddle(GAME_WIDTH - Paddle.PADDLE_WIDTH - 10, (int)(Math.random() * GAME_HEIGHT), KeyEvent.VK_UP, KeyEvent.VK_DOWN);
this.setFocusable(true); //make everything in this class appear on the screen
this.addKeyListener(this); //start listening for keyboard input
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
//make this class run at the same time as other classes (without this each class would "pause" while another class runs). By using threading we can remove lag, and also allows us to do features like display timers in real time!
gameThread = new Thread(this);
gameThread.start();
}
//paint is a method in java.awt library that we are overriding. It is a special method - it is called automatically in the background in order to update what appears in the window. You NEVER call paint() yourself
public void paint(Graphics g){
//we are using "double buffering here" - if we draw images directly onto the screen, it takes time and the human eye can actually notice flashes of lag as each pixel on the screen is drawn one at a time. Instead, we are going to draw images OFF the screen, then simply move the image on screen as needed.
image = createImage(GAME_WIDTH, GAME_HEIGHT); //draw off screen
graphics = image.getGraphics();
draw(graphics);//update the positions of everything on the screen
g.drawImage(image, 0, 0, this); //move the image on the screen
}
//call the draw methods in each class to update positions as things move
public void draw(Graphics g){
// set style of line (i.e., dashed)
Stroke dashed = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0);
// call draw methods of two Paddles and Ball
ball.draw(g);
leftPaddle.draw(g);
rightPaddle.draw(g);
// if the game hasn't started yet, set color of middle line and score to gray
if (paintNumber <= 3) {
g.setColor(Color.gray);
}
// draw dashed line
((Graphics2D)g).setStroke(dashed);
g.drawLine(GAME_WIDTH/2, 0, GAME_WIDTH/2, GAME_HEIGHT);
// set font to Windows default (Segoe)
Font font = new Font("Segoe UI", Font.PLAIN, 64);
g.setFont(font);
// draw score
drawCenteredString(g, 100, 0, GAME_WIDTH/2, Integer.toString(leftScore));
drawCenteredString(g, 100, GAME_WIDTH/2, GAME_WIDTH/2, Integer.toString(rightScore));
// draw game over text on win
if (leftWin) {
drawCenteredString(g, 200, 0, GAME_WIDTH/2, "Winner");
drawCenteredString(g, 200, GAME_WIDTH/2, GAME_WIDTH/2, "Loser");
g.setFont(new Font("Segoe UI", Font.ITALIC, 24));
drawCenteredString(g, 400, 0, GAME_WIDTH, "Press ENTER to play again");
} else if (rightWin) {
drawCenteredString(g, 200, 0, GAME_WIDTH/2, "Loser");
drawCenteredString(g, 200, GAME_WIDTH/2, GAME_WIDTH/2, "Winner");
g.setFont(new Font("Segoe UI", Font.ITALIC, 24));
drawCenteredString(g, 400, 0, GAME_WIDTH, "Press ENTER to play again");
}
// if game hasn't started yet, draw game start text
g.setColor(Color.white);
g.setFont(new Font("Segoe UI", Font.BOLD, 64));
if (paintNumber == 0) {
if (resumeGame) {
drawCenteredString(g, 200, 0, GAME_WIDTH, "Resuming...");
} else {
drawCenteredString(g, 200, 0, GAME_WIDTH, "Starting in...");
}
} else if (paintNumber == 1) {
drawCenteredString(g, 200, 0, GAME_WIDTH, "3");
} else if (paintNumber == 2) {
drawCenteredString(g, 200, 0, GAME_WIDTH, "2");
} else if (paintNumber == 3) {
drawCenteredString(g, 200, 0, GAME_WIDTH, "1");
}
}
// draws a centered string
private void drawCenteredString(Graphics g, int y, int xOffset, int xWidth, String text) {
int x;
// get font size
FontMetrics metrics = g.getFontMetrics();
// determine x for the text
x = xOffset + (xWidth - metrics.stringWidth(text)) / 2;
// draw centered string
g.drawString(text, x, y);
}
//call the move methods in other classes to update positions
//this method is constantly called from run(). By doing this, movements appear fluid and natural. If we take this out the movements appear sluggish and laggy
public void move(){
ball.move();
leftPaddle.move();
rightPaddle.move();
}
//handles all collision detection and responds accordingly
public void checkCollision(){
// reverses ball y direction if ball hits top or bottom of screen
if(ball.y <= 0 || ball.y >= GAME_HEIGHT - Ball.BALL_DIAMETER){
ball.yVelocity *= -1;
}
// updates score if ball hits border of screen
if(ball.x <= 0){
// prevents ball from glitching through the paddles
// it doesn't look great but it prevents the player from being unhappy
wait(3, 0.0);
if (!leftPaddle.checkCollision(ball, 'L')) {
// increment score
rightScore += 1;
// write score to file to allow resuming
try {
writeScoreToFile();
} catch (IOException ignored) {}
regenerateBall('L');
}
}
if(ball.x + Ball.BALL_DIAMETER >= GAME_WIDTH){
wait(3, 0.0);
if (!rightPaddle.checkCollision(ball, 'R')) {
leftScore += 1;
try {
writeScoreToFile();
} catch (IOException ignored) {}
regenerateBall('R');
}
}
// delegates paddle remaining paddle checks to
leftPaddle.checkCollision(ball, 'L');
rightPaddle.checkCollision(ball, 'R');
}
//run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen
public void run(){
double delta = 0;
Scanner fileReader;
// load score of previous game if it exists
try {
File playedFile = new File(saveFile);
if (!playedFile.createNewFile()) {
fileReader = new Scanner(playedFile);
leftScore = Integer.parseInt(fileReader.next());
rightScore = Integer.parseInt(fileReader.next());
// display resume text only if both scores are not equal to zero
resumeGame = leftScore + rightScore != 0;
// fileReader is never used again, so it is closed here
fileReader.close();
}
} catch (IOException | NoSuchElementException | NumberFormatException ignored) {}
// text to be painted before game starts
repaint(); // Resuming/starting text
try {
Thread.sleep(2000);
} catch (InterruptedException ignored) {}
paintNumber++; // pN = 1
repaint(); // 3...
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {}
paintNumber++; // pN = 2
repaint(); // 2...
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {}
paintNumber++; // pN = 3
repaint(); // 1...
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {}
paintNumber++; // pN no longer matters
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {}
while (true) {
// wait 0.5 ticks per cycle to prevent the game from running too fast
// 0.5 was used instead of 1 to reduce lag
delta = wait(0.5, delta);
// only repaint if game is resumed after win
if (leftWin || rightWin) {
continue;
}
//only move objects around and update screen if enough time has passed
move();
checkCollision();
checkForWin();
repaint();
}
}
// sets leftWin or rightWin to true if their score is greater than WIN_SCORE
private void checkForWin() {
if (leftScore >= WIN_SCORE) {
leftWin = true;
}
else if (rightScore >= WIN_SCORE) {
rightWin = true;
}
}
//if a key is pressed, we'll send it over to the PlayerBall class for processing
public void keyPressed(KeyEvent e){
// resumes game and resets win booleans and scores when enter is pressed
if (e.getKeyCode() == KeyEvent.VK_ENTER && (leftWin || rightWin)) {
leftWin = false;
leftScore = 0;
rightWin = false;
rightScore = 0;
}
leftPaddle.keyPressed(e);
rightPaddle.keyPressed(e);
}
//if a key is released, we'll send it over to the PlayerBall class for processing
public void keyReleased(KeyEvent e){
leftPaddle.keyReleased(e);
rightPaddle.keyReleased(e);
}
//left empty because we don't need it; must be here because it is required to be overridded by the KeyListener interface
public void keyTyped(KeyEvent e){
}
// regenerates ball after it scores
public void regenerateBall(char winner) {
// overwrite old ball object with new ball that starts in middle
ball = new Ball((GAME_WIDTH-Ball.BALL_DIAMETER)/2, (GAME_HEIGHT-Ball.BALL_DIAMETER)/2, winner == 'L' ? -1:1);
}
// write score to file after each goal
public void writeScoreToFile() throws IOException {
File playedFile = new File(saveFile);
FileWriter fileWriter = new FileWriter(playedFile);
fileWriter.write(((leftScore < 5 && rightScore < 5) ? leftScore:0) + " " + ((leftScore < 5 && rightScore < 5) ? rightScore:0));
fileWriter.close();
}
// public static function that waits for deltaAmount - previousTime ticks
// returns additional ticks if wait() took longer than deltaAmount - previousTime
// this allows it to run faster in response to lag
public static double wait(double deltaAmount, double previousTime) {
//the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen.
long lastTime = System.nanoTime();
double amountOfTicks = 60;
double ns = 1000000000 / amountOfTicks;
double delta = previousTime;
long now;
while (true) { // this is the delay loop
now = System.nanoTime();
delta = delta + (now - lastTime) / ns;
lastTime = now;
if (delta >= deltaAmount) {
return delta - deltaAmount;
}
}
}
}