Basic Slick Game
Basic Slick Game
Basic Slick Game
Introduction
Game Libraries consist of low-level programming code that is used to create computer games. These libraries typically
include utility programs. Java game libraries consist of a set of Java classes and (in some cases) native-level executable
code which is called by the library files. The Slick 2D Game Library is a wrapper library around the LWJGL library (Lightweight Java Game Library). Using Slick 2D makes accessing the LWJGL code much easier.
In order to use Slick 2D you will have to get the library code, configure this code to work with Eclipse, and then learn the
structure of the library code. The lecture for downloading and installing Slick 2D is posted on this site and so this topic
will not be covered here.
Basic Game Structure in Slick
The basic structure of your game in Slick is to extend either the Basic Game class or the State-based Game class. These
two classes are different because the Basic Game class, which is typically used for simpler games, does not allow for
multi-window games. The state-based game class allows for multiple windows in your game.
Computer games use a Game Loop. This is a coding loop structure ( for example, while(true). ) within which your game
will operate. When the code leaves this game loop the game stops. Inside the game loop most computer games perform
a few distinctive actions. These include:
For example, suppose you are writing a top-down tank game and the player has pressed an arrow key on the keyboard
to move the tank. Inside your game loop you would:
1.
2.
3.
4.
Get the input from the keyboard and read this input.
Calculate the direction and distance to move your tank.
Update the tanks status to the new location.
Redraw the screen with the tanks new position.
This is just one element in the game. Other game-events might include checking to see of other game elements have
collided, if the fuel of the tank is adequate for the move, if a monster on patrol has seen the tank, and a number of other
items. Each of these will have to be handled inside the game loop. Once the game as finished evaluating and updating
the games data the screen is redrawn to show the results. These actions inside the game loop could be taking place up
to 60 times a second (the Frames-per-second rate for the game).
Since we are extending an existing class you will have to provide your own code for those abstract methods in the base
class that must be defined in your extended class. You can use the Eclipse tools to automatically create methods.
Another aspect of Java games is the reliance on Threads. Since Java has a very good built-in Threading library many Java
games take advantage of this feature. Games use Threads for screen rendering and for character operations. Screen
rendering is the method of quickly drawing and updating the screen. This is typically done by creating a Screen Buffer,
onto which the actual drawing takes place. Once the drawing is completed the Screen Buffer is quickly blitted onto the
regular computer display. This rapid updating technique prevents the appearance of video tearing and slowdown.
Threads are also used to handle multiple game characters. Rather than put a characters state inside the main game loop
(running on its own Thread) a game developer will create a separate Thread for characters. When the characters status
changes (for example, it collides with a rock), the Character puts a message on the Event Queue which is picked up by
the main game Thread listener. (This will be explained in more detail later).
Basic Game Workflow
The game loop is the main focus of a game. Inside this loop all of the game related code is executing. The Workflow
describes the logical steps a game goes through when it first starts and when it starts running. This gameflow looks like:
Before the game loop starts (the updaterender loop) the game must be initialized. This is done so that graphics and
other resources are setup prior to starting the game.
1. Start Eclipse and make sure that the Slick2D libraries have been installed (see the lecture on Slick2D).
2. Click File, choose New, and choose Java Project.
3. Create a new Java Project named Slick2DGames.
4. Select this project in the Package Explorer window and click the right mouse button.
5. Choose Properties from the popup menu. You will attach the Slick2D Library to this project.
6. In the Properties dialog box click the Java Build Path option.
7. Click the Add Library button. You will see a list of Library categories:
When you create your own class you want to extend it from the BasicGame class. You also want to override some of the
abstract methods in the base class. Eclipse can be used to simplify some of these steps.
15. For the Class Name enter BasicGameOne.
16. Click the Browse button next to the Superclass option.
17. In the Choose a Type text field type BasicGame. Eclipse finds the classes that have this name.
20. Click Finish. Your class code is created with various methods from the Base class.
package edu.fullcoll.dac.slickgames;
import
import
import
import
org.newdawn.slick.BasicGame;
org.newdawn.slick.GameContainer;
org.newdawn.slick.Graphics;
org.newdawn.slick.SlickException;
This basic class contains the methods you must use for your game. Understand that the init() method will be called once
before the game loop starts. Once the game loop starts the update() and render() methods are called.
Creating Main()
When your game is started the main() method will be executed. This method will start your game class running.
1. Click in the main() method and type the following lines of code:
You notice these lines of code have a red underline indicating an error. Moving the mouse over this code indicates that it
needs a try/catch block.
2. Select these three lines of code.
3. Right click the mouse over the selected block and choose Surround With and then choose Try/Catch block:
The lines of code now have a try/catch block. Eclipse has figured out what Exception is being thrown.
The Main() method should now look like:
Exceptions are thrown by methods. The three methods in main will throw a SlickException. This exception must either
be caught in a try/catch block or it must be rethrown.
Look at the code in this method. Any Basic Game you write will use this exact code for the main() method.
An AppGameContainer is created with a new statement. This is one of the classes in the base library class. The
parameter to this class is an instance of the BasicGameOne class. Notice how an anonymous object is used when
initializing the AppGameContainer object. You could have written the code so it looked like:
However, the line that creates temp is just placed as a parameter inside the constructor for AppGameContainer:
Which results in the same outcome. Java programmers will usually not bother with creating a temp variable that will
only be used once. They will rely on the fact that expressions are evaluated before they are passed as parameters. In this
case, an instance of BasicGameOne is created before being passed into the Constructor for AppGameContainer.
The next command is:
agc.setDisplayMode(400,400,false);
This sets the game window. But how do we know what the parameters are? If you setup the Slick2D library correctly you
linked over to the Slick2D documentation library.
2. Click on the class named AppGameContainer. Expand the window at the bottom of the screen and click the JavaDoc
tab.
If you dont see the JavaDoc window click the Window menu, choose Show View, and choose JavaDoc.
4. With the AppGameContainer class selected in the editor window, click the button in the JavaDoc window that
displays documentation in a browser:
You see a new window open in the editing area displaying the documentation for the class.
5. Scroll down to the setDisplayMode() option and click. This tells you how to use this method. If the library comes
with a set of documentation elements you can display these in Eclipse. Notice that the options for this method
are (width, height, fullscreen).
Notice that the next line of code is agc.start(). This indicates that the AppGameContainer contains a Thread and that it
will be running as a separate Thread. Once started the game continues to run until you shut it down.
We will next define some objects that we will use in the Render step.
6. Click just below the code that starts with public class BaseGameOne and type the following text:
private Image img;
private String msg;
7. There will be a red underline below img. Move the mouse over this variable until you see the suggested
import list:
where XXXXX is the name of the image file you copied into your project.
This code retrieves the Graphics object from the container. This object is used to set the background color of the
window with the setBackground(Color.lightGray) command.
The img object is created from the new Image() statement which reads the file from the Project. The msg
variable is set.
13. Click in the Render method and enter the following lines of code:
This displays the string at X/Y position 100 and the image at X/Y position 200. Notice how the graphics object is
used to display the text and image.
org.newdawn.slick.AppGameContainer;
org.newdawn.slick.BasicGame;
org.newdawn.slick.Color;
org.newdawn.slick.GameContainer;
org.newdawn.slick.Graphics;
org.newdawn.slick.Image;
org.newdawn.slick.SlickException;
}
@Override
public void update(GameContainer container, int delta) throws SlickException {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
AppGameContainer agc = new AppGameContainer(new BasicGameOne("My Game"));
agc.setDisplayMode(400,400,false);
agc.setShowFPS(false);
agc.start();
} catch (SlickException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Note: try removing the setShowFPS(false) statement in main() and see what happens.
8. Click the Add Jars button and choose the Jar file you just exported from Eclipse.
9. Click the Add Jars button and choose the slick.jar and lwjgl.jar file.
10. Click the Add Natives button and choose all the files in the Slick2D folder that end with .DLL.
11. Click the Main Class button and enter the complete class name of your project. This consists of the full package
name and the class name. For example, if your package was edu.fullcoll.dac.slickgames and your main class was
name BasicGameOne you would enter:
12. Click option 4 Create Fat Jar and then click the Create Fat Jar button and select a location and name for your jar
file. Your file should now be self-contained and can be executed by double-clicking the file.
Alternatively, you can click the EXTRA (WINDOWS.EXE) button to create an exe file that contains your game
program.