<< Chapter < Page Chapter >> Page >

Two primary objects

You learned in an earlier module that a Slick2D game that runs as an application (not an applet) consists of at least two cooperating objects:

  1. An object instantiated from a subclass of the Slick2D class named GameContainer .
  2. An object instantiated from a class that implements the Slick2D interface named Game .

Behavior of an object of the AppGameContainer class

The GameContainer object ( item 1 above ) manages the program startup and the game play after the game program starts running. For example, this is the object that managesthe game loop.

As shown in Listing 1 , for the program named Slick0120a , this object is an object of the class named AppGameContainer , which extends the class named GameContainer .

The AppGameContainer class provides many public methods (including the method named start , which is called in Listing 1 ) by which you can manipulate the behavior of the container object. Theauthors of the Slick2D library did not intend for you to physically modify the source code in the GameContainer class or the AppGameContainer class.

Behavior of an object that implements the Game interface

The behaviors of the methods of the Game object ( item 2 above ) are what distinguishes one Slick2D game from another Slick2D game.

You need not implement the Game interface directly. The authors of the Slick2D libraryprovided a helper class named BasicGame that implements the Game interface and provides a number of useful methods. They intended for you to extend the BasicGame class and to override at least three of the methods declared in the Game interface in order to provide the desired behavior for your game..

As mentioned earlier, the class named Slick0120a extends the BasicGame class, thereby implementing the Game interface and getting the benefit of methods defined in the BasicGame class.

The code in the main method in Listing 1 instantiates an object of the Slick0120a class and passes that object's reference to the constructor for the class named AppGameContainer . Therefore, Listing 1 instantiates both of the required objects and connects them in the manner intended by the authors of the Slick2D library.

Starting the game

The main purpose of this module is to analyze the behavior of the Slick2D game engine when you start aSlick2D game running.

Listing 1 calls the start method on a reference to the AppGameContainer object. The source code for the start method is shown in Listing 2 .

Listing 2 . The start method of the AppGameContainer class.
public void start() throws SlickException { try {setup();getDelta(); while (running()) {gameLoop(); }} finally { destroy();}if (forceExit) {System.exit(0); }}//end start

Copyright and license information

Copyright and license information:

I was unable to find any copyright or license information in the zip file that I downloaded from (External Link) . I acknowledge that I am not the author of the code in that zip file and the copyrights for thatmaterial are held by someone other than myself.

Constructors and methods

This and the next few modules will explore and discuss the constructors for the AppGameContainer class (see Listing 1 ) along with salient aspects of the following methods that are called in Listing 2 :

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Anatomy of a game engine. OpenStax CNX. Feb 07, 2013 Download for free at https://legacy.cnx.org/content/col11489/1.13
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Anatomy of a game engine' conversation and receive update notifications?

Ask