简体   繁体   English

如何在LIBGDX中保存高分

[英]How to save the high score in LIBGDX

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/ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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