Create a 2D action game using the GAME API
Original source: http://developss.sun.com/techtopics/mobility/midp/Articles/game/
Author: Jonathan Knudsen March 2003 Translator: clapton_xpAThotmailDOTcom2004 July - Source Codes: [SimpleGame source code] [muTank source code]
Mutank example
MIDP 2.0 contains a set of Game APIs, which can simplify 2D games. This set of API is very simple, consisting of five classes in javax.microedition.lcdui.game package. These five classes provide two important features:
The new Gamecanvas class can draw screens or respond input in the body of the game, rather than relying on the system's drawing and input thread.
A powerful and convenient Layer API can easily and efficiently create complex scenes.
Create a loop using Gamecanvas
Gamecanvas is a canvas with more features; it provides a method for an immediate drawing and monitoring device buttons. These new methods can include all the features of the game in the same loop by a thread control. Think about how you use Canvas If you realize a typical game, you will find what is it is x:
Public void microtankcanvas
Extends Canvas
Implements runnable {
Public void run () {
While (true) {
// Update the game stat.
Repaint ();
// delay one time step.
}
}
Public void paint (graphics g) {
// Painting code Goes here.
}
protected void keypressed (int keycode) {
// Respond to key presses here.
}
}
This is a bit uncomfortable. Run () method running in a program thread updates the game every other time. Generally update a ball or spacecraft coordinates, and an animation of a person or vehicle. In the cycle, Repaint ( ) Call and update the screen. The system transmits button time to KeyPressed (), the keypressed () method changes the game status accordingly.
The problem is that everything above is in a different thread, and the code "is confused" is scattered in three different methods. When the active painting cycle in run () is called repaint (), it cannot be exact I know when the system calls the Paint () method. When the system calls keypressed (), it is impossible to know what other parts in the program is doing. If your keypressed () is to update the game status, while the screen is being The Paint () method is drawn, and the screen that is drawn will appear is not right. In addition, if the time is drawn, the time is more than the run () method calls once.
Gamecanvas allows you to leave the above drawing and button mechanism, put all game logics in the same loop. First, GameCanvas allows you to get its Graphics object by calling the getGraphics () method. All garaphics objects Drawing will be done in an offscreen (a background screen) cache. You can use the FlushGraphics () method to copy the entire cache to the real screen, this method will return when the drawing is truly end. This makes you more than the original That call repaint () better control program. Because the repaint () method will return immediately, and you don't know when the screen is really redrawn.
Gamecanvas also contains a method to get the status of the current device button. This technology is called polling. Unlike the mechanism for waiting for the system call keypressed (), you can now call GetKeyStates () method to immediately know which keys are pressed The code using the Gamecanvas cycle is as follows:
Public void microtankcanvas
Extends Gamecanvas
Implements runnable {
Public void run () {
Graphics g = getGraphics ();
While (true) {
// Update the game stat.
INT KeyState = getKeyStates ();
// Respond to key presses here.
// Painting code Goes here.
Flushgraphics ();
// delay one time step.
}
}
}
The following example demonstrates a basic game cycle. It shows a rotating "fork", you can use the arrow keys to move it on the screen. The Run () method is very simple, this is attributed to GameCanvas.
Import javax.microedition.lcdui. *;
Import javax.microedition.lcdui.game. *;
Public Class SimpleGamecanvas
Extends Gamecanvas
Implements runnable {
Private Volatile Boolean mtrucking;
Private long mframedlay;
PRIVATE INT MX, MY;
PRIVATE INT MSTATE;
Public simpleGamecanvas () {
Super (True);
MX = getWidth () / 2;
My = getHeight () / 2;
MSTATE = 0;
Mframedlay = 20;
}
Public void start () {
Mtrucking = true;
Thread T = New Thread (this);
T.Start ();
}
Public void stop () {mtrucking = false;
Public void run () {
Graphics g = getGraphics ();
While (mtrucking == true) {
Tick ();
INPUT ();
Render (g);
Try {thread.sleep (mframedlay);
Catch (InterruptedException IE {stop ();
}
}
Private void tick () {
MState = (MSTATE 1)% 20;
}
Private void INPUT () {
INT KeyStates = getKeyStates ();
IF (KeyStates & Left_PRESSED! = 0)
MX = math.max (0, mx - 1);
IF (KeyStates & Right_Pressed)! = 0)
MX = math.min (getWidth (), MX 1);
IF (KeyStates & Up_Pressed)! = 0)
my = math.max (0, my - 1);
IF (KeyStates & Down_Pressed)! = 0)
my = math.min (getHeight (), my 1);}
Private void render (graphics g) {
G.SetColor (0xfffffff);
G.fillRect (0, 0, getWidth (), getHeight ());
G.SetColor (0x0000FF);
g.drawline (MX, MY, MX - 10 MSTATE, MY - 10);
g.drawline (MX, MX 10, MY - 10 MSTATE);
g.drawline (MX, MX 10 - MSTATE, MY 10);
g.drawline (MX, MX, MX - 10, MY 10 - MState);
Flushgraphics ();
}
}
This example contains this example, trying to run the simpleGamemidlet to see the effect. You will see a starfish that is doing aerobics (although this poor starfish may lack the leg: P)
SimpleGamemidlet screen capture
to be continued
Clapton_XPathotmaildotcom