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();
}
}
How do I save the high score so whenever the app is opened the high score is displayed. I'd like that when the app is closed the high score is saved and when the app is opened the high score is displayed. I want the users high score to be saved so when they play again I want them to know their high score. There are no errors in the code.
Use a preferences instance. See here
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
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.