<< Chapter < Page | Chapter >> Page > |
Listing 1 . Simplest Slick2Dprogram. |
---|
/*Slick0110a.java
Copyright 2012, R.G.BaldwinPossibly the simplest game that can be coded to use the
Slick2D game engine and run as a Java application.
Compile and run the program by executing the file namedCompileAndRun.bat.
Tested using JDK 1.7 under WinXP and Win 7*********************************************************/
import org.newdawn.slick.AppGameContainer;import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;import org.newdawn.slick.SlickException;
import org.newdawn.slick.Game;public class Slick0110a implements Game{
public static void main(String[]args)
throws SlickException{AppGameContainer app =
new AppGameContainer(new Slick0110a());app.start();//this statement is required
}//end main//----------------------------------------------------//
public void init(GameContainer gc)throws SlickException {
//empty body}
//----------------------------------------------------//public void update(GameContainer gc, int delta)
throws SlickException{//empty body
}//----------------------------------------------------//
public void render(GameContainer gc, Graphics g)throws SlickException{
//empty body}
//----------------------------------------------------//public String getTitle(){
return "Optional title";}//end getTitle
//----------------------------------------------------//public boolean closeRequested(){
return false;}//end closeRequested
}//end class Slick0110a//======================================================// |
A batch file named CompileAndRun
Listing 2 shows the contents of a batch file that you can use to compile and execute the code in Listing 1 as was explained in an earlier module.
Listing 2 . The file named CompileAndRun.bat. |
---|
echo off
del *.classrem refer to jar files in the folder named jars
javac -cp .;../jars/slick.jar;../jars/lwjgl.jar Slick0110a.javarem set the java.library.path and the classpath and run the program
java -Djava.library.path=../lwjglbin -cp .;../jars/slick.jar;../jars/lwjgl.jar Slick0110apause |
Execute the batch file
If you double-click the batch file named CompileAndRun.bat (or execute it in whatever manner you prefer), two new windows should appear on your computer screen.
Slick2D output during startup
The first window to appear should look similar to Figure 2 .
Figure 2 . Output from Slick2D during program startup. |
---|
Figure 2 shows typical information produced by Slick2D when the compiled Slick2D program starts running.
A default Slick2D game window
The second window to appear should look something like Figure 3 .
Figure 3 . A default Slick2D game window. |
---|
Figure 3 is a default Slick2D game window. This Slick2D program (see Listing 1 ) has no interesting behavior. In effect, it is an "empty" game program. Therefore, the only thing showing in the game window is a counter in the topleft corner that shows the execution rate in frames per second. (I will show you how to control the execution rate in a future module.)
Notification Switch
Would you like to follow the 'Anatomy of a game engine' conversation and receive update notifications?