Skip to content

Commit

Permalink
Add highscore
Browse files Browse the repository at this point in the history
  • Loading branch information
manolol1 committed Jan 17, 2024
1 parent 1f53711 commit fcab588
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
16 changes: 12 additions & 4 deletions core/src/xyz/manolol/squarecollector/GameOverScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
public class GameOverScreen extends ScreenAdapter {

private int score;
private boolean isNewHighscore;

SpriteBatch batch;
TextWriter textWriter;

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);
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion core/src/xyz/manolol/squarecollector/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class GameScreen extends ScreenAdapter {
private long lastDifficultyIncreaseTime;

private int score = 0;
private boolean isNewHighscore = false;

Array<Rectangle> squares;

Expand Down Expand Up @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions core/src/xyz/manolol/squarecollector/SquareCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit fcab588

Please sign in to comment.