added UI with inventory and some notifications and an end screen + timer

This commit is contained in:
2025-11-27 18:32:08 +01:00
parent 67f2eeddf5
commit 521951129b
3 changed files with 97 additions and 3 deletions

View File

@@ -0,0 +1,83 @@
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;
Font arial_40, arial_80B;
BufferedImage keyImage;
public boolean messageOn = false;
public String message;
int msgC = 0;
public boolean gameFinished = false;
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().image;
}
public void showMessage(String text) {
message = text;
messageOn = true;
}
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
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);
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;
}
}
}