Advertisement
fahad005

highScore

Apr 21st, 2022
1,102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. package menu;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.Input;
  5. import com.badlogic.gdx.Preferences;
  6. import com.badlogic.gdx.Screen;
  7. import com.badlogic.gdx.graphics.Color;
  8. import com.badlogic.gdx.graphics.GL20;
  9. import com.badlogic.gdx.graphics.Texture;
  10. import com.badlogic.gdx.graphics.g2d.Animation;
  11. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  12. import com.badlogic.gdx.graphics.g2d.GlyphLayout;
  13. import com.badlogic.gdx.utils.Align;
  14. import com.badlogic.gdx.utils.ScreenUtils;
  15. import com.mygdx.game.MainGameScreen;
  16. import com.mygdx.game.MyGdxGame;
  17. import org.w3c.dom.Text;
  18.  
  19. import java.awt.*;
  20. import java.util.ArrayList;
  21.  
  22. public class MainMenuScreen implements Screen {
  23.  
  24.     MyGdxGame game; // a class where the SpriteBatch is created
  25.  
  26.     int highscore, score;
  27.     BitmapFont scoreFont;
  28.     public MainMenuScreen(MyGdxGame game) {
  29.         this.game = game;
  30.        
  31.         Preferences prefs = Gdx.app.getPreferences("spacegame"); // this will create a file if not exist
  32.         this.highscore = prefs.getInteger("highscore", 0); // this will read. if not exist default will be 0
  33.  
  34.         //Check if score beats highscore
  35.         if (score > highscore) {
  36.             prefs.putInteger("highscore", score); // putting new score
  37.             this.highscore = score;
  38.             prefs.flush();
  39.         }
  40.         scoreFont = new BitmapFont();
  41.     }
  42.     @Override
  43.     public void show() {
  44.  
  45.     }
  46.  
  47.     @Override
  48.     public void render(float delta) {
  49.         ScreenUtils.clear(0, 1, 1, 1);
  50.  
  51.         game.batch.begin();
  52.  
  53.         GlyphLayout highScoreLayout = new GlyphLayout(scoreFont, "Highscore: " + highscore, Color.GOLD, 0, Align.left, false);
  54.         scoreFont.draw(game.batch, highScoreLayout, 50, 600); // adding to screen
  55.        
  56.         game.batch.end();
  57.     }
  58.  
  59.     @Override
  60.     public void resize(int width, int height) {
  61.  
  62.     }
  63.  
  64.     @Override
  65.     public void pause() {
  66.  
  67.     }
  68.  
  69.     @Override
  70.     public void resume() {
  71.  
  72.     }
  73.  
  74.     @Override
  75.     public void hide() {
  76.  
  77.     }
  78.  
  79.     @Override
  80.     public void dispose() {
  81.        
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement