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

xiaoxiao2021-03-06  131

[Reproduced in mobile future] author: Jonathan Knudsen translator: Mackie hua MIDP 2.0 which includes a simplified API function to write a two-dimensional game. This API function is very simple, including five classes in javax.microedition.lcdui.game. These five classes provide two important functions:  New Gamecanvas classes make it possible to draw a Screen and response keyboard input in a game cycle body without calling the system's Paint and Input threads.  The powerful and complex layer (Layer) API function can easily and efficiently establish complex scenes. Mutank EXAMPLE uses the GameCanvas class to create a Game Loop Gamecanvas class is a Canvas class attached to a function, which provides a method of scrutinizing and checking 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 us () {wheppaint {// update the game stat. Repaint (); / / Delay One Time Step.}} PUBLIC VOID PAINT (GRAPHICS G) {// Painting Code Goes 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 outbound buffer (Offscreen 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. The following is a typical game loop using GameCanvas class: public void MicroTankCanvas extends GameCanvas implements Runnable {public void run () {Graphics g = getGraphics (); while (true) {// Update the game state int keyState = getKeyStates. (); // Respond to key presses 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 mFrameDelay; private int mX, mY; ​​private int mState; public SimpleGameCanvas () {Super (true); mx = getWidth () / 2; my = getHeight () / 2; mstate = 0; mframedelay = 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 (mframedelay);} 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, MX, MX - 10 MSTATE, MY - 10 ); G.drawline (MX, MX 10, MY - 10 MX); G. Drawline (MX, MX 10 - MSTATE, MY 10); g.drawline (MX, MX, MX - 10, MY 10 - MState; flushgraphics ();}} 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).

SimpleGamemidlet Screen Shot Game Scene is like the onion (level) typical two-dimensional action game often contains 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  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.  Tiledlayer class is used to create a background image. You can use a collection of small source images to efficiently produce large images.  The Sprite class is an animation 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.  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. Using the Tiledlayer class Although the TileDlayer class is still easy to understand, although there is some subtle differences that are not obvious. 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. Source Image This image is divided into image patchs of 12 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. The individual patches of the upper source image are as follows: Tile Numbering Create 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, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 6, 0, 0, 0, 0, 0 , 0, 0, 0, 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

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 perpendicular to the mirror transform, and rotates 90 degrees: // sprite sprite = ... sprite.settransform (sprite.trans_mirror_rot90); application transformation method, so that the reference pixel of the Sprite class does not move . 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 TileDlayer, Sprite, and LayerManager class. 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.

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

New Post(0)