Listing 6 . The main method. |
---|
public static void main(String[] args)throws SlickException{
try{AppGameContainer app = (
new AppGameContainer(new Slick0130a(),400,200,false));
app.start();}catch(SlickException e){
e.printStackTrace();}//end catch
}//end main |
A different constructor
Listing 6 calls a different overloaded constructor for the AppGameContainer class than I have used in earlier modules.
This version of the constructor allows for setting the width and height of the game window. In this case, the game window is set to a width of 400 pixelsand a height of 200 pixels as shown in Figure 1 .
The last parameter to this constructor is described as a boolean parameter that allows for the selection of a full-screen game window. As of thiswriting, I have been unable to get this to work. When I set the third parameter to true, I get a compiler error. However, I haven't spent any time investigatingwhat I might be doing wrong.
The overridden init method
The overridden init method is shown in Listing 7 .
Listing 7 . The overridden init method. |
---|
public void init(GameContainer gc)
throws SlickException {//Set the frame rate in frames per second.
gc.setTargetFrameRate(2);}//end init |
Set the target frame rate
Listing 7 calls the setTargetFrameRate on the GameContainer object passing 2 as a parameter. The description of this method in the javadocs is "Set the target fps we're hoping to get,"
The overridden update method
Listing 8 shows the overridden update method.
Listing 8 . The overridden update method. |
---|
public void update(GameContainer gc, int delta)
throws SlickException{//Compute and save total time since start in seconds.
totalTime += delta/1000.0;//Save delta for display in render method.incrementalTime = delta;}//end update |
Compute total elapsed time
Listing 8 converts the incoming value of delta in milliseconds into seconds and adds it to the value stored in the instancevariable named totalTime that is declared in Listing 5 . The totalTime value will be used to display the first line of text near the center of Figure 1 .
Save the value of delta
Listing 8 saves the incoming value of delta in the instance variable named i ncrementalTime that was also declared in Listing 5 . The value stored in incrementalTime will be used to display the second line of text near the center of Figure 1 .
The overridden render method
Listing 9 shows the overridden render method.
Listing 9 . The overridden render method. |
---|
public void render(GameContainer gc, Graphics g)
throws SlickException{//Truncate totalTime to one decimal digit and
// displaydouble time = (int)(totalTime*10)/10.0;
g.drawString("totalTime: "+time,100.0f,100.0f);//Display incremental time.g.drawString("incrementalTime: " + incrementalTime,
100.0f,120.0f);}//end render |
Truncate and draw the total time
Listing 9 begins by truncating the value of totalTime to only two decimal digits and saving the truncated value in a local variable named time . I will leave it as an exercise for the student to analyze the code that I used todo that.