A game or animation is built according to the principle of repetitively
executing a piece of code.
A D V E R T I S E M E N T
This piece of code tracks the value of instance
variables and updates the game state accordingly. Based on the game state, the
code then draws/paints/repaints the game screen with the elements that make up
the game. The values of the instance variables may change because of either user
interaction or internal game behavior.
The repetitive execution is effected by putting the repetitive code in an
infinite loop. Before entering the loop, an instance variable may be checked to
see if the game should still be running, and if not, the loop may be exited. The
code in the loop should allow the current thread of execution to sleep every few
milliseconds to control the rate at which the update to the instance variables
is done (in effect, how fast the game screen should be refreshed).
To put it in coding terms:
// main class
public MainClass {
private GameCanvas canvas = new MyGameCanvas();
public MainClass() {
// start a thread that will run infinitely
canvas.start();
}
// rest of the code
}
// the actual drawing class
public MyGameCanvas extends GameCanvas implements Runnable {
public MyGameCanvas() {
// instantiation code
}
public void start() {
// do initialization
// and then start a thread
Thread runner = new Thread(this);
runner.start();
}
private void run() {
// or while(keeprunning = true)
// where keeprunning is an instance variable
while(true) {
// checks if the game has reached
// some boundary states or special conditions
verifyGameState();
// gets input from user and
// updates instance variables
// that describe the games elements
checkUserInput();
// paints elements on screen to reflect
// the current game state using the current
// graphics object
updateGameScreen(getGraphics());
// control the rate at which screen updates are done
Thread.sleep(milliseconds);
}
}
}
We will use this structure to develop a game in the following sections.
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
A Very Short Primer on Game Building, j2me introduction, J2ME Tutorial, j2me tutorial netbeans, j2me background process, j2me development kit, J2ME tutorial pdf, history of J2ME, basic J2ME, syntax use in J2ME, J2ME training courses, J2ME download.