From f7aaaeb50680904195dfbcab773d00098e18b8b0 Mon Sep 17 00:00:00 2001 From: Liam Ek Date: Fri, 3 May 2024 18:49:24 +0200 Subject: [PATCH] Moved duplicate actions into a function (setting message length, x and y). Improved readability on double digits and where brackets end. --- src/main/java/com/app/Ui.java | 44 +++++++++++++++++------------------ 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/app/Ui.java b/src/main/java/com/app/Ui.java index 892d1e0..cf696cb 100644 --- a/src/main/java/com/app/Ui.java +++ b/src/main/java/com/app/Ui.java @@ -26,20 +26,29 @@ public Ui(GamePanel gamePanel) { public void healthDisplay(Graphics2D g2D) { - int x = (int) (gamePanel.screenWidth * 0.1d); - int y = (int) (gamePanel.screenHeight * 0.1d); + int x = (int) (gamePanel.screenWidth * 0.10d); + int y = (int) (gamePanel.screenHeight * 0.10d); g2D.drawString("Health: " + gamePanel.health, x, y); } public void scoreDisplay(Graphics2D g2D) { - int x = (int) (gamePanel.screenWidth * 0.1d); + int x = (int) (gamePanel.screenWidth * 0.10d); int y = (int) (gamePanel.screenHeight * 0.15d); g2D.drawString("Score: " + gamePanel.score, x, y); } + private void showMessage(Graphics2D g2D, String message, double y) { + + int messageLength = (int) g2D.getFontMetrics().getStringBounds(message, g2D).getWidth(); + int xMessage = gamePanel.screenWidth / 2 - messageLength / 2; + int yMessage = (int) (gamePanel.screenHeight / 2 * y); + + g2D.drawString(message, xMessage, yMessage); + } + public void draw(Graphics2D g2D) { g2D.setFont(arial_20); @@ -51,22 +60,12 @@ public void draw(Graphics2D g2D) { if (gamePanel.gameState == gamePanel.finishState) { String finishMessage = "You got " + gamePanel.score + " points!"; - - int finishMessageLength = (int) g2D.getFontMetrics().getStringBounds(finishMessage, g2D).getWidth(); - int x = gamePanel.screenWidth / 2 - finishMessageLength / 2; - int y = (int) (gamePanel.screenHeight / 2 * 0.25d); - - g2D.drawString(finishMessage, x, y); + showMessage(g2D, finishMessage, 0.25d); String resetMessage = "Press F to reset!"; + showMessage(g2D, resetMessage, 0.35d); - int resetMessageLength = (int) g2D.getFontMetrics().getStringBounds(finishMessage, g2D).getWidth(); - int xResetMessage = gamePanel.screenWidth / 2 - resetMessageLength / 2; - int yResetMessage = (int) (gamePanel.screenHeight / 2 * 0.35d); - - g2D.drawString(resetMessage, xResetMessage, yResetMessage); - - int xScoreDisplay = (int) (gamePanel.screenWidth * 0.1d); + int xScoreDisplay = (int) (gamePanel.screenWidth * 0.10d); int yScoreDisplay = (int) (gamePanel.screenHeight * 0.30d); if (!gamePanel.leaderboardCalculated) { @@ -77,19 +76,20 @@ public void draw(Graphics2D g2D) { for (int i = 0; i < topScores.size(); i++) { g2D.drawString("Top " + (i + 1) + ": " + topScores.get(i), xScoreDisplay, yScoreDisplay + (i * gamePanel.tileSize)); } + } if (gamePanel.gameState == gamePanel.pauseState) { g2D.setFont(arial_40_bold); - String pauseMessage = "Game is paused!"; + String pauseMessage = "Game is paused"; + showMessage(g2D, pauseMessage, 0.80d); - int messageLength = (int) g2D.getFontMetrics().getStringBounds(pauseMessage, g2D).getWidth(); - int x = gamePanel.screenWidth / 2 - messageLength / 2; - int y = gamePanel.screenHeight / 2 + (gamePanel.tileSize * 2); - - g2D.drawString(pauseMessage, x, y); + String unpauseMessage = "Press ESC to unpause"; + showMessage(g2D, unpauseMessage, 0.95d); } + } + }