added gamestates and started pause screen

This commit is contained in:
2025-11-28 16:03:32 +01:00
parent b890eb7a58
commit 1bc9ad9923
4 changed files with 52 additions and 50 deletions

View File

@@ -31,7 +31,7 @@ public class GamePanel extends JPanel implements Runnable {
// SYSTEM // SYSTEM
TileManager tileM = new TileManager(this); TileManager tileM = new TileManager(this);
KeyHandler keyH = new KeyHandler(); KeyHandler keyH = new KeyHandler(this);
Sound se = new Sound(); Sound se = new Sound();
Sound music = new Sound(); Sound music = new Sound();
public CollisionHandler collisionH = new CollisionHandler(this); public CollisionHandler collisionH = new CollisionHandler(this);
@@ -43,6 +43,9 @@ public class GamePanel extends JPanel implements Runnable {
public Player player = new Player(this, keyH); public Player player = new Player(this, keyH);
public SuperObject[] obj = new SuperObject[10]; public SuperObject[] obj = new SuperObject[10];
// GAME STATE
public GameState gameState;
public GamePanel() { public GamePanel() {
this.setPreferredSize(new Dimension(screenWidth, screenHeight)); this.setPreferredSize(new Dimension(screenWidth, screenHeight));
this.setBackground(Color.black); this.setBackground(Color.black);
@@ -54,6 +57,7 @@ public class GamePanel extends JPanel implements Runnable {
public void setupGame() { public void setupGame() {
assetSetter.setObject(); assetSetter.setObject();
playMusic(0); //Play main theme playMusic(0); //Play main theme
gameState = GameState.PLAY;
} }
public void startGameThread() { public void startGameThread() {
@@ -91,7 +95,13 @@ public class GamePanel extends JPanel implements Runnable {
} }
public void update() { public void update() {
player.update(); switch(gameState) {
case PLAY:
player.update();
break;
case PAUSE:
break;
}
} }
public void paintComponent(Graphics graphics) { public void paintComponent(Graphics graphics) {

View File

@@ -0,0 +1,8 @@
package de.miaurizius.jgame2d.core;
public enum GameState {
PLAY,
PAUSE;
}

View File

@@ -6,9 +6,14 @@ import java.awt.event.KeyListener;
public class KeyHandler implements KeyListener { public class KeyHandler implements KeyListener {
public boolean upPressed, downPressed, leftPressed, rightPressed; public boolean upPressed, downPressed, leftPressed, rightPressed;
public GamePanel panel;
// DEBUG // DEBUG
public boolean checkDrawTime = false; public boolean checkDrawTime = false;
public KeyHandler(GamePanel panel) {
this.panel = panel;
}
@Override @Override
public void keyTyped(KeyEvent e) {} public void keyTyped(KeyEvent e) {}
@@ -16,11 +21,17 @@ public class KeyHandler implements KeyListener {
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
int code = e.getKeyCode(); int code = e.getKeyCode();
switch (code) { switch (code) {
// CONTROLS
case KeyEvent.VK_W, KeyEvent.VK_UP -> upPressed = true; case KeyEvent.VK_W, KeyEvent.VK_UP -> upPressed = true;
case KeyEvent.VK_S, KeyEvent.VK_DOWN -> downPressed = true; case KeyEvent.VK_S, KeyEvent.VK_DOWN -> downPressed = true;
case KeyEvent.VK_A, KeyEvent.VK_LEFT -> leftPressed = true; case KeyEvent.VK_A, KeyEvent.VK_LEFT -> leftPressed = true;
case KeyEvent.VK_D, KeyEvent.VK_RIGHT -> rightPressed = true; case KeyEvent.VK_D, KeyEvent.VK_RIGHT -> rightPressed = true;
// DEBUG OPTIONS
case KeyEvent.VK_T -> checkDrawTime = !checkDrawTime; case KeyEvent.VK_T -> checkDrawTime = !checkDrawTime;
// GAME STATES
case KeyEvent.VK_ESCAPE -> panel.gameState = (panel.gameState == GameState.PAUSE) ? GameState.PLAY : GameState.PAUSE;
} }
} }

View File

@@ -1,29 +1,26 @@
package de.miaurizius.jgame2d.core; package de.miaurizius.jgame2d.core;
import de.miaurizius.jgame2d.object.KeyObj;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage;
import java.text.DecimalFormat; import java.text.DecimalFormat;
public class UI { public class UI {
GamePanel panel; GamePanel panel;
Graphics graphics2d;
Font arial_40, arial_80B; Font arial_40, arial_80B;
BufferedImage keyImage; DecimalFormat df = new DecimalFormat("#0.00");
public boolean messageOn = false; public boolean messageOn = false;
public String message; public String message;
int msgC = 0;
public boolean gameFinished = false; public boolean gameFinished = false;
int msgC = 0;
double playTime; double playTime;
DecimalFormat df = new DecimalFormat("#0.00");
public UI(GamePanel panel) { public UI(GamePanel panel) {
this.panel = panel; this.panel = panel;
arial_40 = new Font("Arial", Font.PLAIN, 40); arial_40 = new Font("Arial", Font.PLAIN, 40);
arial_80B = new Font("Arial", Font.BOLD, 80); arial_80B = new Font("Arial", Font.BOLD, 80);
keyImage = new KeyObj(panel).image;
} }
public void showMessage(String text) { public void showMessage(String text) {
@@ -32,52 +29,28 @@ public class UI {
} }
public void draw(Graphics graphics2d) { public void draw(Graphics graphics2d) {
if(gameFinished) { this.graphics2d = graphics2d;
graphics2d.setFont(arial_40);
graphics2d.setColor(Color.white);
String text = "You found the treasure!";
int textLength = (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth();
int x,y;
x = panel.screenWidth/2 - textLength/2;
y = panel.screenHeight/2 - (panel.tileSize*3);
graphics2d.drawString(text, x, y);
text = "Your time is: " + df.format(playTime) + "!";
textLength = (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth();
x = panel.screenWidth/2 - textLength/2;
y = panel.screenHeight/2 + (panel.tileSize*4);
graphics2d.drawString(text, x, y);
graphics2d.setFont(arial_80B);
graphics2d.setColor(Color.yellow);
text = "Congratulations!";
textLength = (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth();
x = panel.screenWidth/2 - textLength/2;
y = panel.screenHeight/2 + (panel.tileSize*2);
graphics2d.drawString(text, x, y);
panel.gameThread = null;
return;
}
// MESSAGE
graphics2d.setFont(arial_40); graphics2d.setFont(arial_40);
graphics2d.setColor(Color.white); graphics2d.setColor(Color.white);
graphics2d.drawImage(keyImage, panel.tileSize/2, panel.tileSize/2, panel.tileSize, panel.tileSize, null);
graphics2d.drawString("x " + panel.player.hasKey, 74, 65);
// TIME switch (panel.gameState) {
playTime += (double) 1/panel.FPS; case PLAY:
graphics2d.drawString("Time: " + df.format(playTime), panel.tileSize*11, 65);
if(messageOn) { break;
graphics2d.setFont(panel.getFont().deriveFont(30F)); case PAUSE:
graphics2d.drawString(message, panel.tileSize/2, panel.tileSize*5); drawPauseScreen();
msgC++; break;
if(msgC <= 120) return; //text stays 2 seconds
msgC = 0;
messageOn = false;
} }
} }
public void drawPauseScreen() {
String text = "PAUSED";
int y = panel.screenHeight / 2;
graphics2d.drawString(text, getCenteredX(text), y);
}
public int getCenteredX(String text) {
return panel.screenWidth / 2 - (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth() / 2;
}
} }