4 – GAME LOOP

 

 

GAMES IN GENERAL

 

Every game follows the same basic pattern.  We first initialize everything needed for the game (players, starting positions, score, …). Next, we loop until the game is finished. In every pass of the loop, we update the game world (get input, update positions, etc..) then render (draw) our changes to the screen.  This is done so fast that the game seems to be continuous.

 

In pseudo-code, it would look like this:

 

     Create needed variables/objects

     While playing the game

     {

          Get input, update the state of everything in the game, etc..

          Render the game world to the screen

     }

     Display End Game screen

 

OUR PROJECT

 

Inside the core project, there is initially only one class.  It extends ApplicationAdapter.  That class has two methods: the create method and the render method.

 

The create method is automatically called once at the start and is used to initialize any variables needed during the game.

 

The render method is automatically called 60 times per second.  This method is the loop.

 

Any data that you want to initialize in the create method and then use regularly in the render method has to be a datafield for the class.  This will allow the data to be stored from one method call to another.

 

BLANK CLASS

 

In its simplest form, the class would look like this:

 

package pat;

 

import com.badlogic.gdx.ApplicationAdapter;

 

public class PongGame extends ApplicationAdapter

{

   //DATAFIELDS GO HERE

 

 

 

   //INITIALIZE ALL OBJECTS/VARIABLES INSIDE THIS METHOD

   public void create ()

   {

 

   }

 

   //EXECUTE THE CODE IN THIS METHOD 60 TIMES PER SECOND

   public void render ()

   {

 

   }

}

 

Note:  Because both the create and the render methods override a method in their superclass, we will in the future place @overide on the line above the method prototype.  This is simply to help us catch unintended errors.