Entropy Engine provides several classes to simplify the creation of Java based games. This tutorial will describe how to use those classes. Included is how to create a game, how to control the game loop, and how to use the input system. More information on the Entropy Engine can be found on the Entropy Engine page.
A Basic Game
There are three main components to a basic game. The first is the GameLoop. This class manages the running of the game, calling update and draw at appropriate times. The GameContainer acts as an abstract interface between the game logic and the underlying implementation of the graphics, input, and other low level systems. Finally, the update and draw logic is contained in the class implementing the Game interface. Following is an example of a simple game:
import com.entropyinteractive.Game;
import com.entropyinteractive.GameContainer;
import com.entropyinteractive.GameLoop;
import com.entropyinteractive.WindowedGame;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
public class Main implements Game
{
public static void main(String[] args)
{
// create a stand-alone game
GameContainer gc = new WindowedGame("Basic Game", 640, 480);
// run the game with the default game loop
gc.run(new GameLoop(), new Main());
// exit when the game is finished
System.exit(0);
}
@Override
public void draw(GameContainer gc, Graphics2D g)
{
// add drawing logic here
}
@Override
public void shutdown(GameContainer gc)
{
// add shutdown logic here
}
@Override
public void startup(GameContainer gc)
{
// add startup logic here
}
@Override
public void update(GameContainer gc, double delta)
{
// stop the game if the escape key is pressed
if(gc.getKeyboard().isKeyPressed(KeyEvent.VK_ESCAPE)) gc.halt();
// add update logic here
}
}
The code is very simple. First a new game container is created. In this case the game container is a windowed game (i.e. a game running in AWT frame). Next, run is called on that game container passing in a game loop and a game to be run. This begins the game loop, which will continue until halt is called on the game container. Finally, when the game finishes running, the program exits.
The Main class itself is what implements the Game interface. It must therefore provide the draw, startup, shutdown, and update methods. The startup method is called immediately after the game loop begins running, and shutdown just before the game loop ends. The other two methods, update and draw, are called repeatedly by the game loop while the game is running.
The Game Loop
The game loop in Entropy Engine is intended to be flexible. It is design to robustly run a game in using several styles of update logic. The exact behavior can be set using the following four parameters:
- minDelta: This is the minimum time between updates. Update will never be called with a delta time less than this value, and if both update and draw finish in less time than minDelta, the game loop will sleep until minDelta seconds have passed.
- maxDelta: This is the maximum time between updates. Update will never be called with a delta time greater than this value. Depending on the next two parameters, if the game is running slow, multiple updates may be queued with maxDelta as the delta time to catch up. In this case, some calls to draw may be skipped to help the game catch up.
- maxUpdateDelay: This is the maximum amount of time an update will be delayed. If the game is running greater than maxUpdateDelay seconds behind, the game loop will re-sync with the real time and the queued updates will be skipped.
- maxDrawDelay: This is the maximum amount of time a draw will be delayed. If it has been greater than maxDrawDelay seconds since the last draw, draw will be called regardless of whether or not the game is running slow.
By setting these parameters appropriately, many different behaviors can be achieved. For example, setting minDelta to 0 and maxDelta to infinity would result in the simplest delta time game loop. Setting minDelta and maxDelta to the same value results in a fixed logic rate game loop. If maxUpdateDelay is 0, the game will slow down if the updates and draws are taking to long. Setting maxDrawDelay to 0 will result in a draw being called after every update.
Input
The three input types in Entropy Engine are keyboard, mouse, and mouse wheel. Each game container provides three methods to retrieve each of these. For example, during a call to update, the gc.getKeyboard() could be used to get the current instance of the keyboard input manager.
Each of the input managers provides several ways to access the input. All three include the getEvents method to retrieve a linked list of every input event that occurred since the last update. They also implement the Iterable interface to conveniently iterate over the those events. Each of the input classes includes various methods for common input related tasks. Details on these methods can be found in the Javadoc.
-Eric