如何在游戏结束屏幕上显示最终分数和高分数?



我正在使用libgdx,我正在尝试弄清楚如何显示我的玩家获得的最终分数,以及当游戏进入屏幕游戏时他们游戏的整体高分。我不确定如何将分数整数从游戏屏幕带到游戏屏幕上。

选项 1:使用首选项存储高分

我认为您希望能够关闭游戏(关闭窗口或杀死应用程序(并存储高分以供下次有人玩游戏(执行游戏或打开应用程序(时使用。

在这种情况下,首选项是要走的路,这里有一个简单的例子:

游戏类

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));
}
}

游戏画面类

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));
}
}

屏幕游戏类

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);
}
}

警告:请注意,从Preferences存储和检索方法是区分大小写的,因此最好将引用值的String放在变量上,以最大程度地减少错误。

选项 2:在屏幕之间传递高分

也许您不需要在游戏关闭时存储高分,因此将高分信息从一个屏幕传递到另一个屏幕应该更容易,下面是一个例子:

游戏类

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));
}
}

游戏画面类

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));
}
}

屏幕游戏类

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);
}
}

这可能不是最好的方法,但您可以将分数保持在扩展的主类中Game.

import com.badlogic.gdx.Game;
public class MyGame extends Game {
private int score;
// Add getters and setters
}

然后在屏幕课程中,您可以执行以下操作:

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

如果要存储多个值,则可以创建一个类并将其实例保留在MyGame类中。您将把score和其他属性放在此类中。

您还可以在类中创建MyGame静态方法,为您执行强制转换:

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

然后你可以做MyGame.get().setScore(score);或:int score = MyGame.get().getScore();

相关内容

  • 没有找到相关文章

最新更新