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

@@ -1,29 +1,26 @@
package de.miaurizius.jgame2d.core;
import de.miaurizius.jgame2d.object.KeyObj;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.text.DecimalFormat;
public class UI {
GamePanel panel;
Graphics graphics2d;
Font arial_40, arial_80B;
BufferedImage keyImage;
DecimalFormat df = new DecimalFormat("#0.00");
public boolean messageOn = false;
public String message;
int msgC = 0;
public boolean gameFinished = false;
int msgC = 0;
double playTime;
DecimalFormat df = new DecimalFormat("#0.00");
public UI(GamePanel panel) {
this.panel = panel;
arial_40 = new Font("Arial", Font.PLAIN, 40);
arial_80B = new Font("Arial", Font.BOLD, 80);
keyImage = new KeyObj(panel).image;
}
public void showMessage(String text) {
@@ -32,52 +29,28 @@ public class UI {
}
public void draw(Graphics graphics2d) {
if(gameFinished) {
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
this.graphics2d = graphics2d;
graphics2d.setFont(arial_40);
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
playTime += (double) 1/panel.FPS;
graphics2d.drawString("Time: " + df.format(playTime), panel.tileSize*11, 65);
switch (panel.gameState) {
case PLAY:
if(messageOn) {
graphics2d.setFont(panel.getFont().deriveFont(30F));
graphics2d.drawString(message, panel.tileSize/2, panel.tileSize*5);
msgC++;
if(msgC <= 120) return; //text stays 2 seconds
msgC = 0;
messageOn = false;
break;
case PAUSE:
drawPauseScreen();
break;
}
}
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;
}
}