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

zhaozj2021-02-12  215

The code taken in this article includes a MIDlet that uses this Canvas. You can try to run the SimpleGameMidlet this applet to see how it works. You will see something like being a starfish that is doing fitness (perhaps it is looking for your own lost legs).

The game scene is like onions (level)

Typical two-dimensional action games often contain a background and several animated characters. Although you can draw this scene yourself, the Game API function enables you to build a scene with a layer. You can make a city layer of a city, and then do a layer containing a cars. Place the cars on the background, you created a complete scene. Put your car in a separate layer, you can easily manipulate it, not affected by background and other layers.

The Game API function provides flexible support with the following four classes as the layers.

l The layer class is an abstract base class for all layer classes. It defines the basic properties of a layer, including location, size, and whether this layer is visible. Each subclass of the Layer class must define a port () method to perform this layer on an image, which will be drawn to the screen surface. Two exact subclasses Tiledlayer and Sprite should be able to meet your two-dimensional game needs.

l TileDlayer class is used to create a background image. You can use a collection of small source images to efficiently produce large images.

l The Sprite class is an animated layer. You provide a source frame to completely control over the entire animation. The Sprite class also provides images and can be rotated 90 degrees to the source frame.

l The layermanager class is a very useful class that saves all the movements of all layers in your scene. A simple call of the LayerManager class Paint () method is sufficient to control all the layers contained.

Use the Tiledlayer class

Although it contains some unimware, TileDlayer class is still very easy to understand. The basic idea of ​​this class is to provide a set of image patches with a source image, which can be combined into a large scene. For example, the image below is 64 * 48 pixels.

This image is divided into image patchs of 12 blocks 16 * 16. The TileDlayer class is assigned to each image patch number, and the picture in the upper left corner is set to 1, and so on. Each patch of the above source image is numbered:

Creating a TiledLayer class with code is very simple. You need to determine the number of rows and columns, source images, and pixels of each patch in this source image. The following code snippet tells you how to load images and create a Tiledlayer class.

Image image = image.createImage ("/ board.png");

Tiledlayer tiledlayer = new tiledlayer (10, 10, image, 16, 16);

In the example, the new TileDlayer class has 10 lines, 10 columns. These image patch sizes from image are 16 * 16 pixels.

Interesting part or uses these image patchs to create a scene. Using the setcell () method can assign an image patch into an array of cells. You need to provide the number of ranks in this array cell and the number of image patches. For example, you can assign image patch numbered 5 into the third array of cells in the second line by calling the setcelll (2, 1, 5) method. If you think that these parameters look wrong, please note that the image patch number is starting from 1, and the number of rows and columns start from 0. By default, all array of cells in the new TileDlayer class object is 0, which means they are empty.

The following codes are disconnected to you describe an integer array to populate the TileDlayer class object. In actual images, TileDlayer classes can be defined from the resource file, which makes more flexibility in the context of defining the background, and provides new background and level to enhance the playability of the game. Private tiledlayer createboard () {

Image image = NULL;

Try {image = image.createImage ("/ board.png");}

Catch (IOEXCEPTION IOE) {Return Null;}

Tiledlayer tiledlayer = new tiledlayer (10, 10, image, 16, 16);

int [] map = {

1, 1, 1, 1, 11, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 9, 0, 0, 0, 0, 0,

0, 0, 0, 0, 1, 0, 0, 0, 0, 0,

0, 0, 0, 7, 1, 0, 0, 0, 0, 0,

1, 1, 1, 1, 6, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 7, 11, 0,

0, 0, 0, 0, 0, 0, 7, 6, 0, 0,

0, 0, 0, 0, 0, 7, 6, 0, 0, 0

}

For (int i = 0; i

INT column = i% 10;

INT row = (i - column) / 10;

Tiledlayer.Setcell (Column, Row, Map [I]);

}

Return tiledlayer;

}

In order to display this TileDlayer class object on the screen, you need to call a port () method of a Graphics object.

The TileDlayer class also supports animated image posts, which makes it easy to move the cell collection through a series of patchs. To get a more detailed description, see the TileDlayer class related API documentation.

Implement the animation using the Sprite class

Another specific Layer class provided in the Game API function is the Sprite class. On the one hand, the Sprite class is a conceptual reverse of the Tilelayer class. Tiledlayer class uses a palette of the source image patch to create a large scene, while the Sprite class uses a series of source image frames to generate an animation.

You created a Sprite class just the source image and the size of each frame. In the TileDlayer class, the source image is divided into image patchs of the same size; in the Sprite class, the child image is referred to as a frame. In the following example, the source image TANK.PNG is used to create a Sprite class object with a frame size of 32 * 32 pixels.

Private microtanksprite createtank () {

Image image = NULL;

Try {image = image.createImage ("/ tank.png");}

Catch (IOEXCEPTION IOE) {Return Null;}

Return New Microtanksprite (image, 32, 32);

}

Each frame in the source image has a number, starting from 0, and it is accumulated. (Do not confuse here, remember the number of the image patch is starting from 1) The Sprite class has a frame sequence, which determines the order of the frame display. The default frame sequence of a new Sprite class object simply be accumulated from 0 from 0. Using the NEXTFRAME () method of the Sprite class and the prevframe () method, the frame can be moved forward or backward in the frame sequence. These methods connect the head tail of the frame sequence. For example, if the SPRITE class object has displayed the frame at the end of the frame sequence, if the nextframe () method will display the header of the frame sequence.

Call the setFramesequence () method, you can determine differently than the default frame sequence by the sequence specified by the integer array.

You can also call a setFrame () method to jump to a certain frame in the current frame sequence. You can't jump to a specific frame number, you can only jump to a specific point of the frame sequence.

When using the Paint () method inherited from the Layer class, only the change in the frame is really realized when the Sprite class is expressed in the next time period.

The Sprite class also converts the source frame. You can rotate the frame 90 degrees, or make mirror transformations, or both. These possibilities are enumerated in the constant in the Sprite class. The current transformation method of the Sprite class can be set by passing one of these constants to the SetTransform () method. The following example is that the current frame is performed on the vertical center to perform mirror transform, and rotate 90 degrees:

// sprite sprite = ...

Sprite.settransform (sprite.trans_mirror_rot90);

The transformation method is applied, so that the reference pixel of the Sprite class is not moved. Under the default, the reference pixel of the Sprite class is located at the (0, 0) point in the Sprite class coordinate system, namely the upper left corner. When a transform method is applied, the location of the reference pixel is also transformed. The position of the Sprite class is adjusted, so reference pixels are still in the original position.

You can change the location of the reference pixel point by calling the DEFINEREFERENCEPIEL () method. For most types of animations, you can define the reference pixel point on the center of Sprite.

Finally, the Sprite class provides several collideswith () methods to detect collisions with other sprites, itledlayers, or images objects. You can detect collisions using the detection rectangle (fast but rough) or pixel level (slow but accurate). The subtle differences of these methods are difficult to describe; if detailed, you can see the API documentation.

Mutank example

Mutank examples illustrate the usage of TileDlayer, Sprite, and LayerManager classes.

The most important class is a MicrotankCanvas class that contains most of the code and the MicrotankSprite class that encapsulates the tank behavior.

The MicrotankSprite class has produced a lot of transformation. It uses a source image containing only 3 frames to display a tank pointing to 16 directions. Two common methods of TURN () and forward () make the tank easily control.

The Microtankcanvas class is a subclass of the Gamecanvas class, which contains an animation cycler that you should be familiar with you in the Run () method. The Tick () method is used to detect whether the tank is touched on the partition. If you encounter, you call the Undo () method of the MicrotankCanvas class to keep it backwards. The input () method simply detects whether the button is pressed and adjusts the direction or position of the tank. The render () method uses a LayerManager class object to process the painting. The LayerManager class contains two layers, tank layers, and partition layers.

From the debug () method called from the game cycle, it is used to compare the time and expected cycle time (80 milliseconds) used by the game cycle, and the percentage of time is displayed on the screen. It is only used as a debug diagnostic purpose, which will be deleted before the game is sent to the user. The timing of the game cycle is more complicated than the previous SimpleGamecanvas class. For more accurate, a game cycle body is repeated every 80 milliseconds, the MicrotankCanvas class is measured for Tick (), input (), and render () methods. Then stop the remaining time in 80 milliseconds, so that the total time used by each cycle is close to 80 milliseconds.

to sum up

The MIDP 2.0 Game API function provides a framework for simplifying the development of two-dimensional action. First, GameCanvas provides a compact painting and input method that enables the game cycle. Second, the layer architecture makes it possible to create a complex scene. TileDlayer combines a large background or scene from the palette of the source image cluster. Sprite is suitable for animated characters and detects collisions in the game with other objects. LayerManager binds all layers together. The Mutank example provides a set of basic work code to illustrate the use of the GAME API function.

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. Original source: http://wireless.java.sun.com/midp/articles/game/

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

New Post(0)