如何在 LIBGDX 中保存高分
Posted
技术标签:
【中文标题】如何在 LIBGDX 中保存高分【英文标题】:How to save the high score in LIBGDX 【发布时间】:2021-09-28 15:05:13 【问题描述】:public class spacegame extends ApplicationAdapter
SpriteBatch batch;
Texture background;
Texture ship;
Texture gameover;
float shipY = 0;
float velocity = 0;
int gamestate=0;
float gravity = 2;
int highscore;
int score = 0;
int scoringrock = 0;
BitmapFont font;
Circle shiprectangel ;
// ShapeRenderer shapeRenderer ;
Rectangle[] rockrectangle;
float gap = 450;
float maxrockoffset;
Random randomgenerator;
float rockvelocity = 8;
int numberofrocks = 4;
float[] rockX = new float[ numberofrocks];
float[] rockoffset = new float[numberofrocks];
float distancebetweenrocks;
Texture rock;
Texture rock2;
@Override
public void create ()
batch=new SpriteBatch();
background=new Texture("background.png");
ship = new Texture("ship.png");
rock = new Texture("rock.png");
rock2 = new Texture("rock2.png");
shiprectangel = new Circle();
// shapeRenderer = new ShapeRenderer();
rockrectangle = new Rectangle[numberofrocks];
gameover = new Texture("gameover.png");
font = new BitmapFont();
font.setColor(Color.RED);
font.getData().setScale(10);
maxrockoffset = Gdx.graphics.getHeight() / 2 - gap / 2 - 100;
randomgenerator = new Random();
distancebetweenrocks = Gdx.graphics.getWidth() * 3/5;
startgame();
public void startgame()
shipY=Gdx.graphics.getHeight() / 2 - ship.getHeight() / 2;
for (int i = 0 ; i < numberofrocks; i++)
rockoffset[i] = (randomgenerator.nextFloat() - 1.5f) * (Gdx.graphics.getHeight() - gap
- 400);
rockX[i] = Gdx.graphics.getWidth() / 2 - rock.getWidth() / 2 + Gdx.graphics.getWidth()
+ i * distancebetweenrocks;
rockrectangle[i] = new Rectangle();
@Override
public void render ()
batch.begin();
batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
if (gamestate == 1)
if (rockX[scoringrock] < Gdx.graphics.getWidth() / 2)
score++;
Gdx.app.log("score", String.valueOf(score));
if (scoringrock < numberofrocks - 1)
scoringrock++;
else
scoringrock = 0;
if (Gdx.input.isTouched())
velocity = -30;
for (int i = 0 ; i < numberofrocks; i++)
if (rockX[i] < - rock.getWidth())
rockX[i] += numberofrocks *distancebetweenrocks;
rockoffset[i] = (randomgenerator.nextFloat() - 1.5f) *
(Gdx.graphics.getHeight() - gap - 200);
else
rockX[i] = rockX[i] - rockvelocity;
rockX[i]=rockX[i] - rockvelocity;
batch.draw(ship, Gdx.graphics.getWidth() / 2 - ship.getWidth(), shipY);
font.draw(batch,String.valueOf(score), 100,1000);
batch.draw(rock, rockX[i], Gdx.graphics.getHeight() / 2 + gap / 3 +
rockoffset[i]);
// batch.draw(rock2, Gdx.graphics.getWidth() / 2 - rock2.getWidth() /
2,Gdx.graphics.getHeight() /2 - gap / 2 - rock2.getHeight() + rockoffset);
rockrectangle[i] = new Rectangle(rockX[i],Gdx.graphics.getHeight() / 2 + gap / 3 +
rockoffset[i], rock.getWidth(), rock.getHeight());
if (score > highscore)
highscore = score;
if (shipY > 0 )
velocity = velocity + gravity;
shipY-=velocity;
else
gamestate = 2;
else if (gamestate == 0)
font.draw(batch,String.valueOf(highscore), 100, 1000);
if (Gdx.input.justTouched())
gamestate = 1;
else if (gamestate == 2)
batch.draw(gameover,Gdx.graphics.getWidth() / 2 - gameover.getWidth() /2
,Gdx.graphics.getHeight() / 2 - gameover.getHeight() /2 );
font.draw(batch,String.valueOf(highscore), 100, 1000);
if (Gdx.input.justTouched())
gamestate = 1;
startgame();
score =0;
scoringrock =0;
velocity = 0;
// shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
// shapeRenderer.setColor(Color.RED);
shiprectangel.set(Gdx.graphics.getWidth() * 1/2, shipY + ship.getHeight() /
2,ship.getWidth() * 3/7);
// shapeRenderer.circle(shiprectangel.x,shiprectangel.y,shiprectangel.radius);
for (int i = 0 ; i < numberofrocks; i++)
//shapeRenderer.rect(rockX[i],Gdx.graphics.getHeight() / 2 + gap / 3 + rockoffset[i],
rock.getWidth(), rock.getHeight());
if (Intersector.overlaps(shiprectangel, rockrectangle[i]))
gamestate = 2;
// shapeRenderer.end();
batch.end();
如何保存高分,以便在打开应用程序时显示高分。我希望在关闭应用程序时保存高分,并在打开应用程序时显示高分。我希望保存用户的高分,所以当他们再次玩时,我希望他们知道他们的高分。代码没有错误。
【问题讨论】:
【参考方案1】:使用首选项实例。看这里
https://www.programmersought.com/article/3503962478/
Preferences prefs = Gdx.app.getPreferences("Highscore");
prefs.putInteger("highscore", 10);
prefs.flush();
int highScore = prefs.getInteger("highscore",0);//0 is default for first run
【讨论】:
在 create() 方法中读取默认值为 0 的高分值(因为它最初没有播放)。在游戏过程中,您已经在处理高分字段。将持久性放在 ApplicationAdapter 的 pause() 方法(您当前未使用)中(对于像 android 这样的某些目标,您不一定知道应用程序何时关闭,但您确实知道它何时暂停)。如果一切正常,请将答案标记为已接受。以上是关于如何在 LIBGDX 中保存高分的主要内容,如果未能解决你的问题,请参考以下文章
如何在 cocos2d iphone 游戏中保存高分和加载高分
如何使用 UserDefaults 快速将高分保存到游戏中? [复制]