Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Moved duplicate actions into a function (setting message length, x an…
Browse files Browse the repository at this point in the history
…d y).

Improved readability on double digits and where brackets end.
  • Loading branch information
Liam Ek authored and Liam Ek committed May 3, 2024
1 parent ed824db commit f7aaaeb
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/main/java/com/app/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
}

}

}

0 comments on commit f7aaaeb

Please sign in to comment.