- boolean closeRequested()
- String getTitle()
- void init(GameContainer container)
- void render(GameContainer container, Graphics g)
- void update(GameContainer container, int delta)
What you will learn
You will learn how and why you should extend the BasicGame class instead of implementing the Game interface directly.
You will learn about the constructors for the AppGameContainer class.
You learned earlier that you need to call the start method on an object of the AppGameContainer class to cause your Slick2D game programto start running. You will learn that the start method calls the following three methods:
- setup
- getDelta
- gameLoop
You will learn about the behavior of the setup and getDelta methods in this module. An explanation of the gameLoop method will be deferred until the next module.
General background information
Listing 6 shows the skeleton code for a basic game class named Slick0120a . This code differs from the skeleton code presented in earlier modules in two important respects:
- The class named Slick0120a extends the Slick2Dclass named BasicGame instead of extending Object and implementing the Slick2Dinterface named Game .
-
The class named
Slick0120a does not override the methods
named
getTitle and
closeRequested .
(They
are overridden with default behavior in the
BasicGame class.) Instead, it overrides only the following methods that are
declared in the Slick2D
Game interface:
- init
- update
- render
The class named BasicGame
Regarding the first item in the above list, while it is technically possible to write a Slick2D game programby implementing the Game interface directly, the Slick2D helper class named BasicGame implements the Game interface and provides a number of useful methods as well. Therefore, byextending the BasicGame class, you not only implement the Game interface, you also get the benefit of the methods that are defined in the BasicGame class.
The methods named init, update, and render
Note, however that the Basic game class does not define concrete versions of the methods named init , update , and render . Therefore, you are still required to provide concrete versions of those methods in your class that extends the BasicGame class (or some subclass of that class) .
The class named Slick0120a does provide concrete versions of methods as indicated in the second item in the above list.
The methods named getTitle and closeRequested
Further regarding the second item in the above list, the class named BasicGame does provide concrete versions of the methodsnamed getTitle and closeRequested . Therefore, unless you need to provide different behavior for those two methods, you don'tneed to override them in your new class that extends the BasicGame class.
Discussion and sample code
Listing 1 shows the main method for our new class named Slick0120a .
Listing 1 . Listing 1 . The main method. |
---|
public static void main(String[] args)throws SlickException{
AppGameContainer app =new AppGameContainer(new Slick0120a());
app.start();//this statement is required}//end main |
We will dissect this code to make certain that we understand what it means and why we need it.