Game Development in Android using Libgdx Part V

This would be a small tutorial regarding the basic structure of the Gameplay screen. We would start by creating the Gameplay class which would extend  the abstract class Screen which we made in Part I of this series. The structure would be similar to the MainMenu class.

public Gameplay(int backScreenid,OrthographicCamera cam,TapResponse tapresponse,int levelNumber){
    spriteBatch=new spriteBatch();
    CurrentLevel=levelNumber;  //Store which level to load
    Init();   //Initialize textures, variables and game state
void Init(){
   Assets.Load(); //in Assets class we would be loading the textures and settings
   //initialize the buttons in the menu
public void update (Application app){
      //Check which button is clicked.
      //Set BackScreenID to the screen which should be shown next
      //depending on the button click
      //and then set IsDone=true
public void render (Application app){;  //Clear the screen
      //Render solid backgrounds without alpha channel
     //Render those textures which dont have any alpha/transparent component
     //As it is faster to draw them while blending is disabled
public void dispose (){
      //Dispose of the textures,fonts
      //dispose of spritebatch
 public void OnPause(){
 public void OnResume(){
 public boolean isDone() {
    return IsDone;

The above code is mostly same as the MainMenu class described in the last tutorial. We just added one more argument in the constructor for the level which is to be loaded when the screen changes from menu screen to this screen.

Over the next tutorials we would be making a simplfied version of our game Polar.

Let’s see what would be the different high level states in the gameplay cycle.

GAME_PRE_INITIALIZE: The initial state of the game when the constructor is called.

GAME_INITIALIZE: After loading the textures and setting the initial values of different objects. After initialization either the game can start automatically or it can wait for a event from the player.

GAME_RUNNING: In this state the player is in the active state.

GAME_PAUSE: This is the state when player pauses the game or the level finishes.

GAME_RESUME: This is the state when the player resumes a paused game or starts/restarts the level.

GAME_OVER: When the player dies or the level is finished.

So we would be making a finite state machine or simply an integer variable holding these different states.

public class Gameplay{
   final int GAME_PRE_INITIALIZE=0;
   final int GAME_INITIALIZE=1;
   final int GAME_RUNNING=2;
   final int GAME_PAUSE=3;
   final int GAME_RESUME=4;
   final int GAME_OVER=5;
   int currentState;
   int lastState;
   public Gameplay(...){ 
   void Init(){
      //After initializing we can set the state. Now if we want to load textures using a different thread
      // then we can check the states to see if the textures are loaded and show a loading bar in the
   void Update(float dt){ 
    void UpdateGameState(){
           // Show loading
           //Wait for user input to start the game
         if(currentState == GAME_RUNNING)
          //Update Game states for enemies collectibles etc.
         //Check for user input for either resume or exit from game
         //For a simple case we will consider game is paused at the init state or in between the level or after game is over
              //Start the level
              //Continue the game
              //Restart the level or load a new level 
              //Stop all the game objects updates. Set the state to Pause. Wait for user input.

So we have a simple base on which we can add our functions for initializing, updating, rendering etc our level.

Next Tutorial:

In our texture tutorial we would discuss more about the initialization part of the game.

Let us know any of your doubts/suggestions; please put them in the comments section and we would try to answer them.

Thank you for the patience.

Tagged with: , , , ,
Posted in Tutorials
Try our games

Error: Twitter did not respond. Please wait a few minutes and refresh this page.