From fcab5882dafbd3dba3d93389026543a68fab81fd Mon Sep 17 00:00:00 2001 From: Mario Ebenhofer Date: Wed, 17 Jan 2024 17:21:47 +0100 Subject: [PATCH] Add highscore --- .../manolol/squarecollector/GameOverScreen.java | 16 ++++++++++++---- .../xyz/manolol/squarecollector/GameScreen.java | 7 ++++++- .../manolol/squarecollector/SquareCollector.java | 9 +++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/core/src/xyz/manolol/squarecollector/GameOverScreen.java b/core/src/xyz/manolol/squarecollector/GameOverScreen.java index 5a5022d..5389a0c 100644 --- a/core/src/xyz/manolol/squarecollector/GameOverScreen.java +++ b/core/src/xyz/manolol/squarecollector/GameOverScreen.java @@ -11,6 +11,7 @@ public class GameOverScreen extends ScreenAdapter { private int score; + private boolean isNewHighscore; SpriteBatch batch; TextWriter textWriter; @@ -18,8 +19,9 @@ public class GameOverScreen extends ScreenAdapter { OrthographicCamera camera; FitViewport viewport; - public GameOverScreen(int score) { + public GameOverScreen(int score, boolean isNewHighscore) { this.score = score; + this.isNewHighscore = isNewHighscore; this.camera = new OrthographicCamera(); this.viewport = new FitViewport(1920, 1080, camera); @@ -50,11 +52,17 @@ public void render(float delta) { batch.setProjectionMatrix(camera.combined); batch.begin(); - textWriter.drawTextCenterXY("GAME OVER", 120, 300, 1, 0, 0); - textWriter.drawTextCenterXY("Score: " + score, 90, 160); + textWriter.drawTextCenterXY("GAME OVER", 120, 320, 1, 0, 0); + textWriter.drawTextCenterXY("Score: " + score, 90, 180); + + if (isNewHighscore) { + textWriter.drawTextCenterXY("NEW HIGHSCORE: " + GAME.getHighscore(), 90, 50); + } else { + textWriter.drawTextCenterXY("Highscore: " + GAME.getHighscore(), 90, 50); + } if (Gdx.app.getType() == Application.ApplicationType.Desktop) { - textWriter.drawTextCenterXY("Press SPACE to try again!", 60, 0); + textWriter.drawTextCenterXY("Press SPACE to try again!", 60, -120); if (GAME.getMouseControls()) { textWriter.drawTextCenterXY("Mouse Controls: ON", 45, -320); diff --git a/core/src/xyz/manolol/squarecollector/GameScreen.java b/core/src/xyz/manolol/squarecollector/GameScreen.java index 8dc3de7..48a0ab6 100644 --- a/core/src/xyz/manolol/squarecollector/GameScreen.java +++ b/core/src/xyz/manolol/squarecollector/GameScreen.java @@ -43,6 +43,7 @@ public class GameScreen extends ScreenAdapter { private long lastDifficultyIncreaseTime; private int score = 0; + private boolean isNewHighscore = false; Array squares; @@ -118,7 +119,11 @@ public void render(float delta) { square.y -= squareSpeed * delta; if (square.y + SQUARE_SIZE < 0) { - GAME.setScreen(new GameOverScreen(score)); + if (score > GAME.getHighscore()) { + GAME.setHighscore(score); + isNewHighscore = true; + } + GAME.setScreen(new GameOverScreen(score, isNewHighscore)); return; } diff --git a/core/src/xyz/manolol/squarecollector/SquareCollector.java b/core/src/xyz/manolol/squarecollector/SquareCollector.java index 8c61823..0389b85 100644 --- a/core/src/xyz/manolol/squarecollector/SquareCollector.java +++ b/core/src/xyz/manolol/squarecollector/SquareCollector.java @@ -25,6 +25,15 @@ public void setMouseControls(boolean mouseControls) { PREFS.flush(); } + public int getHighscore() { + return PREFS.getInteger("highscore", 0); + } + + public void setHighscore(int highscore) { + PREFS.putInteger("highscore", highscore); + PREFS.flush(); + } + public void toggleMouseControls() { if (getMouseControls()) { setMouseControls(false);