1
\$\begingroup\$

I want to make game level select screen.

There are some stages which open, and some that are not.

So if you clear a second stage and quit the game for a while, when you come back, you can start at stage 3, 2 or 1 whatever you can click, but not from stage 4; you can't play it without completing stage 3.

I want to make something like this, but I have no idea how. What should I use to do it (boolean, database...)?

Can you give me some simple code structure?

\$\endgroup\$
2
\$\begingroup\$

I would first recomend you look into a few tutorial sites like Full Libgdx game tutorial or Games From Scratch.

Having said that, I will tell you how I handle game screens and level systems so that you can start at a level based from previous plays.

Libgdx comes with a Game class which you can extend for your application. This will be the starting point of your app. From this class you switch to different screens(Screen) depending on what task you're trying to accomplish, whether its changing the preferences, viewing high scores or playing the game.

Let's look at a single case that covers all the points in this question.

  1. The user starts the app

  2. The user is presented with a menu and chooses to view the options/prefs

  3. The user goes back to the main menu

  4. The user plays the game and dies at level 2

  5. The end screen is shown to the user

  6. The user is sent back to the main menu

  7. The user plays again but starts at level 2

  8. The user dies at level 3

  9. The end screen is shown to the user

  10. The user is sent back to the main menu

  11. The user quits (and level 3 save point is saved)

Now we know how we want the process to play out we can start implementing this.

First our Game class which will control the changing of screens and saving of the current level. We also know we need a Screen for the menu, the game, the preferences and the end screen so we will add those in the Game class too. In our case shown above, this would be step 1.

MyGameClass

public class MyGameClass extends Game {
    private MenuScreen menuScreen; // has a menu on it
    private PreferencesScreen prefScreen; // has options to change game prefs
    private GameScreen gameScreen; // shows the game
    private EndGameScreen endScreen; // the end screen to show user points etc
    // constants for different screens
    public final static int MENU = 0;
    public final static int PREFERENCES = 1;
    public final static int APPLICATION = 2;
    public final static int ENDGAME = 3;

    // get the app preferences (for loading last level) 
    Preferences prefs = Gdx.app.getPreferences("My Preferences");

    public int currentLevel = 1; // set the level to 1

    @Override
    public void create () {
         // load the level from prefs file(if no value is saved 1 will be used)
         currentLevel = prefs.getInteger("highlevel", 1);
         // change the screen to the menu
         changeScreen(MyGameClass.MENU);

    }

    // the method to change screen
    public void changeScreen(int screen){
         switch(screen){
            case MENU :
                menuScreen = new MenuScreen(this); 
                this.setScreen(menuScreen);
                break;
            case PREFERENCES :
                prefs = new PreferencesScreen(this);
                this.setScreen(prefs);
                break;
            case APPLICATION :
                gameScreen= new GameScreen(this,currentLevel);
                this.setScreen(gameScreen);
                break;
            case ENDGAME :
                endScreen = new EndGameScreen (this);
                this.setScreen(endScreen);
                break;
         }
    }

    public void saveLevel(){
         prefs.putInteger("highlevel", currentLevel);
    }

}

Now the Menu screen. This screen should basically show a menu so the user can click buttons to choose what they want to do, be it look at the preferences, start the game or quit the application. This is step 2-3,6 and 10

MenuScreen

public class MenuScreen implements Screen{
     private MyGameClass parent;        

     public MenuScreen(MyGameClass mgc){
         parent = mgc;   //store the parent class 
         // so we can tell it to change screen 
     }

     @Override 
     public void show(){
         // code to show a menu and buttons etc
         // I usually use Scene2D Stage and TextButtons
         // in the button's listener I would change screen like this
         parent.changeScreen(MyGameClass.APPLICATION);
     }
}

Next the GameScreen which will show your game. This is the only screen that takes the parent class and an int value. The int value is to allow the game screen to choose which level to load. This is step 4, 7-8

GameScreen

public class GameScreen implements Screen{
     private MyGameClass parent;

     public GameScreen(MyGameClass mgc, int startLevel){
         parent = mgc;   //store the parent class 
         // load level goes here using startLevel to identify which level to load.
     }

     @Override 
     public void show(){
        // code to show your game
     }

     @Override
     public void render(float delta) {
         // game code goes here and when player dies
         // use parent.changeScreen(MyGameClass.ENDGAME);

         // if the player gets to the next level
         // update the parent level with 
         parent.currentLevel++;
         // and save the current level to prefs
         parent.saveLevel();
     }
}

I won't go into details about the preferences screen as its explained in many guides. However the basic idea of this screen is:

PreferencesScreen

public class PreferencesScreen implements Screen{
    @Override 
     public void show(){
        // code to show your preferences controls

        // in a button listener you would use
        parent.changeScreen(MyGameClass.MENU);
        // to go back to the menu
     }
}

This should of covered how to switch between screens and allow the game to load certain levels based on previous plays by saving the level in the preferences file.

\$\endgroup\$
  • \$\begingroup\$ Would it be better to have the constants be an enum? \$\endgroup\$ – ND523 Oct 5 '17 at 15:50
  • \$\begingroup\$ You certainly could use enums. I used int values as I usually pass screen changes over a network with kryonet and ints don't need registering or converting. \$\endgroup\$ – dfour Oct 5 '17 at 16:37
  • \$\begingroup\$ Makes sense. Both work in this case, right? \$\endgroup\$ – ND523 Oct 5 '17 at 16:40
  • \$\begingroup\$ Yeah both will work no problem. \$\endgroup\$ – dfour Oct 5 '17 at 16:46
0
\$\begingroup\$

You seem to be asking about at least two different things, how to save and retrieve data from disk (so it sticks around if you quit and come back,) as well as how to structure the data.

Regarding saving, I haven't used libgdx but a quick search turned up this built in method of saving small things.

Regarding how to structure the data, I would suggest thinking about what kind of questions you would like to be able to ask about what the player did, then store the data necessary to answer those questions in a convenient format. For example, if a player always goes from stage to stage consecutively then storing the highest level they got to might be the best option. If instead the player can skip stages and you want to track which specific levels they have and haven't played then a series of booleans is a good fit.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.