J2ME game development instance explanation

xiaoxiao2021-03-06  16

Skip J2ME, let's talk about the theory of games. Specifically to Huairong Road, there are three main aspects, stickers

Fig. Game operation. Logical judgment. Here, talk about the map, and other two aspects are in the profile design and detailed design. So-called

The map, actually the picture, is to output a pair of pictures at the location of the graph, (if you involve the animation, you have to trouble

Some, you can use Timertask.Thread or RanNable technology), this pair of pictures can be ready for preparation

It can be temporary. There is an Image class in J2ME, specifically used to manage pictures, it has a CreateImage () party

Method, you can directly read image files (J2ME only supports pictures of PNG format), or take part of existing images.

(So ​​we can put a lot of pictures together, then a piece of cut, the advantage is saving storage space and document reading

Take time, all of the mobile phone is a bottleneck of performance).

J2ME has a Graphics class, specializes in drawing, it has a DrawImage () method, you can put a picture

The specified location is displayed, it also has a DrawRect () method and setColor () method, these two methods are behind us

It will be used when the game is operational, here is here. With pictures and drawings, you need to know who to draw pictures.

On, J2ME provides a Canvas class, which means canvas, it has a Paint () method for refreshing the page,

There is a repaint () method for calling the post () method. Listening to some confused, don't tighten, I will combine specific procedures.

Explain. For the convenience of future programming, we create two types of Images and Draw, Images to save some constants.

Values ​​and pictures, DRAW is mainly used to draw, the source code of these two classes is as follows.

The source code of the image is as follows:

Package huarroad;

Import javax.microedition.lcdui. *;

Import javax.microedition.lcdui.game. *;

Public class images {// save constant

// Drawing position constant

Public Static Final Int Unit = 32; // Unit length

Public static final int LEFT = 10; // The left boundary vertex of the drawing

Public Static Final INT TOP = 9; // Top of the top boundary

// Map location constant

Public static final int width = 4; // Map width

Public Static Final INT Height = 5; // The height of the map

// Map mark

Public static final byte caocao = (byte) / 'a /'; Map mark

Public Static Final Byte Machao = (Byte) / 'B /'; // Ma Chao Map

Public static factory byte huangzhong = (byte) / 'c /'; // Huang Zhong's map mark

Public static final byte guanyu = (byte) / 'd /'; // Guan Yu's map markup public static final byte zhangfei = (byte) / 'e /'; // Zhang Fei's map mark

Public static factory byte zhaoyun = (byte) / 'f /'; // Zhao Yun's map mark

Public Static Final Byte Zu = (Byte) / 'g /'; // Stroke Map Mark

Public Static Final Byte Blank = (Byte) / 'h /'; // Blank Map Tag

Public Static Final Byte Cursor = (Byte) / 'I /'; // Code Map Mark

// Map combination marking regularly

Public static final byte dleft = (byte) / '1 /'; file: // group

Public static final byte dup = (byte) / '2 /'; file: // group

Public static final byte dleftup = (byte) / '3 /'; file: // group

// Picture constant

Public static image image_base; // Basic image

Public static image image_zhaoyun; // Zhao Yun's picture

Public static image image_caocao; // Cao Cao's picture

Public static image image_huangzhong; // Huang Zhong's picture

Public static image image_machao; // Ma Chao picture

Public static image image_guanyu; // Guan Yu's picture

Public static image image_zhangfei; // Zhang Fei's picture

Public static image image_zu; // Picture of Stroke

Public static image image_blank; // blank picture

Public static image image_frame; // Picture of Game Frame

Public images () {// Constructor

}

Public static boolean init () {// Initialize the picture used in the game

Try {

Image_base = image.createImage ("/ huarongroad / bitback.png");

Image_frame = image.createImage (image_base, 126, 0, 145, 177,

Sprite.Trans_none;

/ / The Sprite class is used to flip the picture, which is the characteristics of the newly added support game for MIDP2.0.

Image_zhaoyun = image.createImage (image_base, 0, 0, unit, 2 * unit,

Sprite.Trans_none;

Image_caocao = image.createImage (image_base, unit, 0, 2 * unit,

2 * Unit, sprite.trans_none; image_huangzhong = image.createImage (image_base, 3 * unit, 0, unit,

2 * Unit,

Sprite.Trans_none;

Image_machao = image.createImage (image_base, 0, 2 * unit, unit,

2 * Unit,

Sprite.Trans_none;

Image_guany = image.createImage (image_base, unit, 2 * unit,

2 * Unit, Unit,

Sprite.Trans_none;

Image_zhangfei = image.createImage (image_base, 3 * unit, 2 * unit,

Unit, 2 * Unit,

Sprite.Trans_none;

Image_zu = image.createImage (image_base, 0, 4 * unit, unit, unit,

Sprite.Trans_none;

Image_blank = image.createImage (image_base, 1 * unit, 4 * unit, unit,

Unit,

Sprite.Trans_none;

Return True;

} catch (exception ex) {

Return False;

}

}

}

The source code for the DRAW class is as follows:

Package huarroad;

Import javax.microedition.lcdui. *;

Public class draw {

/ / Draw the picture in the game

Public Draw (canvas canvas) {// constructor

}

Public Static Boolean Paint (Graphics G, Byte IMG, INT X, INT Y) {

// Draw an image of an img specified in the map of X, Y

Try {

Paint (g, img, x, y, images.Unit); // transform the map x, y point into an absolute coordinate of the canvas, drawing

Return True;

}

Catch (Exception EX) {

Return False;

}

}

Public Static Boolean Paint (Graphics G, Byte IMG, INT X, INT Y, INT Unit) {

Try {copy images.caoca // painted Cao Craft

/ / Becomes an absolute coordinate, and adjust

g.drawimage (images.image_caocao, images.left x * Unit,

Images.top y * Unit,

Graphics.top | graphics.Left);

Break;

Case images.guanyu: // Painting Yu Yu

g.drawimage (images.image_guanyu, images.left x * Unit,

Images.top y * Unit,

Graphics.top | graphics.Left);

Break;

Case images.huangzhong: // Painting Huangzhong

g.drawimage (images.image_huangzhong, images.left x * Unit,

Images.top y * Unit,

Graphics.top | graphics.Left);

Break;

Case images.macha // painting horse super

g.drawimage (images.image_machao, images.left x * Unit,

Images.top y * unit, graphics.top | graphics.Left);

Break;

Case images.zhangfei: // Painting

g.drawimage (images.image_zhangfei, images.left x * Unit,

Images.top y * Unit,

Graphics.top | graphics.Left);

Break;

Case images.zhaoyun: // Painting Zhao Yun

g.drawimage (images.image_zhaoyun, images.left x * Unit,

Images.top y * Unit,

Graphics.top | graphics.Left);

Break;

Case images.zu: //

g.drawImage (images.image_zu, images.left x * Unit,

Images.top y * Unit,

Graphics.top | graphics.Left);

Break;

Case images.blank: // Draw blank

g.drawimage (images.image_blank, images.left x * Unit,

Images.top y * Unit,

Graphics.top | graphics.Left);

Break;

Case images.cursor: //

g.drawRect (images.left x * Unit,

Images.top y * unit, images.Unit, images.Unit;

Break;

}

Return True;

} catch (exception ex) {

Return False;

}

}

}

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

New Post(0)