Use the Game API function to make a two-dimensional action game (1)

zhaozj2021-02-12  147

Use the Game API function to make a 2D action game

Translator: Ai HUA

Author: Jonathan Knudsen

MIDP 2.0 includes an API function to simplify the writing of two-dimensional games. This API function is very simple, including five classes in javax.microedition.lcdui.game. These five classes provide two important features:

l The new Gamecanvas class makes an Screen and response keyboard input to a game cycle body, without calling the system's Paint and Input threads.

l The powerful and complex layer (Layer) API function can easily and efficiently establish complex scenes.

Mutank EXAMPLE

Create a game loop using the Gamecanvas class (Game Loop)

The GameCanvas class is a Canvas class that adds a function, which provides a method of scrutinizing and checking the device buttons. These new methods are packaged in one cycle in one cycle and control it by a single thread. Why is this very attractive? Let us consider how you execute a typical game that uses the Canvas class:

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 not a beautiful picture. Run () method running in the application thread, each time period will refresh the game. A typical task is to refresh the position of the ball or the flyer, draw a person or aircraft animation. Each time the cycle is used, the repaint () method is used to refresh the screen. The system transmits the button event to KeyPressed (), which can properly refresh the game status.

The problem is that everything is in different threads, and the game code is easily confused in the above three different methods. When the active painting cycler in the run () method calls the repaint () method, there is no way to know how the system calls the Paint () method. When the system calls keypressed (), there is no way to know what the program is on. If the code in your keypressed () will refresh the status of the game, while the same time Paint () method will behave the screen, then the screen will continue to be very strange. If the time taken by the performance screen exceeds a single time period, the animation will look bump or weird.

Gamecanvas class allows you to avoid the Painting and button messages (Key-Event) mechanisms, so all game logic can be included in a single loop. First, the GameCanvas class allows you to access the Graphics object directly with the getGraphics () method. Any manifestation of the returned Graphics object can be implemented by a screen outer buffer. You can replicate the buffer to the screen with FlushGraphics () until the screen is refreshed. This approach gives you more perfect control than calling the repaint () method. The repaint () method will return the value immediately, so that your application cannot determine when the system will call Paint () to refresh the screen.

The Gamecanvas class also includes a method for obtaining the current state of the device button, that is, the so-called polling technology. You can immediately determine which button is pressed by calling the GetKeyStates () method of the Gamecanvas class, so that the KeyPRESSED () method is replaced. Below is a typical game cycle body using the Gamecanvas class:

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 next example describes a basic game cycle. It shows you a rotating "X", you can use the arrow keys to move it on the screen. The Run () method here is particularly thin, which is much more than Gamecanvas.

Import javax.microedition.lcdui. *;

Import javax.microedition.lcdui.game. *;

Public Class SimpleGamecanvas

Extends Gamecanvas

Implements runnable {

Private 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 {}

}

}

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 ();

}

}

Continued ............

Translator's dedication: The purpose of translation is to broaden the field of view, cultivate wireless applications developers' interests and hobbies, thereby facilitating the development of domestic wireless Internet. The translator hopes that this article will help the majority of enthusiasts and developers' learning and research and development. Due to the translator's professional and technical level and the limited English level, it is inevitable that there is a lot of advice.

转载请注明原文地址:https://www.9cbs.com/read-6229.html

New Post(0)