簡體   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();
    }


}

如何保存高分,以便每次打開應用程序時都會顯示高分。 我希望當應用程序關閉時高分被保存,當應用程序打開時顯示高分。 我希望用戶的高分被保存,所以當他們再次玩時,我希望他們知道他們的高分。 代碼中沒有錯誤。

使用首選項實例。 看這里

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