简体   繁体   中英

How do I display a final score and high score on gameover screen?

I am using libgdx and I am trying to figure out how to display the final score my players achieve, and the overall high score for their games when the game goes to the game over screen. I am unsure as to how to bring the score integer over from the game screen to the game over screen.

Option 1: Storing high score using preferences

I think you want to be able to close the game (close the window or kill the app) and have the high scores stored for the next time someone plays the game (executes the game or opens the app).

In this case preferences are the way to go and here's a simple example:

Game class

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;

public class PreferenceExample extends Game {

    @Override
    public void create() {
        // Initialize the preferences
        Preferences preferences = Gdx.app.getPreferences("examplePreferences");
        // Go to your game screen sending this LibGDX Game and the LibGDX Preferences
        setScreen(new GameScreen(this, preferences));
    }
}

Game screen class

import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.ScreenAdapter;

class GameScreen extends ScreenAdapter {

    private PreferenceExample game;
    private Preferences preferences;

    GameScreen(PreferenceExample game, Preferences preferences) {
        // Store reference to LibGDX Game
        this.game = game;
        // Store reference to LibGDX Preferences
        this.preferences = preferences;
    }

    @Override
    public void render(float delta) {
        if (Gdx.input.isKeyJustPressed(Input.Keys.L)) {
            saveHighScore(MathUtils.random(3));
            goToGameOverScreen();
        }
    }

    // Call this whenever you want to save the high score
    private void saveHighScore(int highScore) {
        preferences.putInteger("High score", highScore);
        preferences.flush();
    }

    // Call this whenever you want to switch to the game over screen
    private void goToGameOverScreen() {
        game.setScreen(new GameOverScreen(preferences));
    }
}

Game over screen class

import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.ScreenAdapter;

class GameOverScreen extends ScreenAdapter {

    private Preferences preferences;
    private int highScore;

    GameOverScreen(Preferences preferences) {
        // Store reference to LibGDX Preferences
        this.preferences = preferences;
    }

    @Override
    public void show() {
        // Load high score, default value is 0 in case you didn't store it properly
        highScore = preferences.getInteger("High score", 0);
    }

    @Override
    public void render(float delta) {
        // Do something with the high score you retrieved
        System.out.println(highScore);
    }
}

Warning : be aware that storing and retrieving methods from Preferences are case sensitive so it is a good idea to put the String referencing the value on a variable to minimize mistakes.

Option 2: Passing high score between screens

Maybe you don't need the high scores stored for when the game is closed so passing the high score information from one screen to another should be easier, here's an example:

Game class

import com.badlogic.gdx.Game;

public class ScreenToScreenExample extends Game {

    @Override
    public void create() {
        // Go to your game screen sending this LibGDX Game and the LibGDX Preferences
        setScreen(new GameScreen(this));
    }
}

Game screen class

import com.badlogic.gdx.ScreenAdapter;

class GameScreen extends ScreenAdapter {

    private ScreenToScreenExample game;
    private int highScore;

    GameScreen(ScreenToScreenExample game) {
        // Store reference to LibGDX Game
        this.game = game;
    }

    // Call this whenever you want to save the high score
    void saveHighScore(int highScore) {
        this.highScore = highScore;
    }

    // Call this whenever you want to switch to the game over screen
    void goToGameOverScreen() {
        game.setScreen(new GameOverScreen(highScore));
    }
}

Game over screen class

import com.badlogic.gdx.ScreenAdapter;

class GameOverScreen extends ScreenAdapter {

    private int highScore;

    GameOverScreen(int highScore) {
        this.highScore = highScore;
    }

    @Override
    public void render(float delta) {
        // Do something with the high score you retrieved
        System.out.println(highScore);
    }
}

It's probably not the best way, but you can keep the score in the main class that extends Game .

import com.badlogic.gdx.Game;

public class MyGame extends Game {
    private int score;

    // Add getters and setters
}

Then in your screen classes you can do:

MyGame myGame = (MyGame) Gdx.app.getApplicationListener();
// Then you can set
myGame.setScore(score);
// or get the score
int score = myGame.getScore();

If you have multiple values to store, you can create a class and keep an instance of it in your MyGame class. You will put the score and other properties in this class.

You can also create a static method in MyGame class that do the cast for you:

public static MyGame get() {
    return (MyGame) Gdx.app.getApplicationListener();
}

Then you can do MyGame.get().setScore(score); or: int score = MyGame.get().getScore();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM