Improved map generation methods using MIDP2.0 development games (4)

xiaoxiao2021-03-06  72

The last time we realized the background with Sprite, but this is not convenient to generate a map through the map data. Typically, the maps in the game are divided into NXN checkered, generally only several patterns. Fortunately, Sun is in MIDP 2.0 has taken into account this feature that the game developer needs, TileDlayer is the layer implemented by the map, and its use is equally unusually simple!

First prepare the small block area we need, in the map of the tank war, there are five kinds of 5:

(Note This is an enlarged JPG format, you need to process the 40x8 PNG format)

They are ice, brick, rock, river, trees, and if there is anything to use, I will definitely know.

First reading image: image.createImage ("/ res / img / tile.png");

Then you can create tiledlayer: New TileDlayer (4, 4, Image, 8, 8);

The first two parameters specify the number of NXNs, the latter two parameters specify the size of the square (not necessarily a checkered), we use 8x8 size, so read 40x8 PNG picture is divided into 5 small Block, the number is 1-5. Why not start numbers from 0? Since 0 is used to represent a transparent block.

The following is defined in a two-dimensional array, indicating the map data:

int [] [] map = new int [] [] {{0, 0, 1, 4}, {1, 1, 2, 4}, {0, 1, 2, 4}, {0, 1, 1 , 3},};

Finally, use TileDlayer.Setcell (col, row, index); give each small block to the corresponding picture. If Index is 0, the small block is transparent.

OK, we change the last spritebackground to LayerBackground:

public class TankGameCanvas extends GameCanvas implements Runnable {... private TiledLayer layerBackground; // Constructor and initialization public TankGameCanvas () {... try {layerBackground = new TiledLayer (22, 22, Image.createImage ( "/ res / img / tile .png "), 8, 8); int EMP = 0; int = 1; int BRK = 2; int ROC = 3; int RIV = 4; int TRE = 5; // The map is too big! ! ! Int [] [] map = new int [] [] {// 22x22 {EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP}, {ROC, ROC, ROC, ROC, EMP, EMP, EMP, EMP, EMP, EMP, ICE, ICE, ICE, ICE, ICE, ICE, BRK, BRK, BRK, BRK, BRK, BRK}, {ROC, ROC, ROC, ROC, EMP, EMP, EMP, EMP, EMP, ICE, ICE, ICE, ICE, ICE, ICE, BRK, BRK, BRK, BRK, BRK, BRK} , BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, ROC, ROC, BRK, BRK, BRK, BRK, ROC, ROC, ROC, ROC}, {BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, ROC, ROC, BRK, BRK, BRK, BRK, ROC, ROC, ROC, ROC}, {ROC, ROC, BRK, BRK, BRK, BRK, EMP, EMP, RIV, RIV, RIV, RIV, RIV, BRK, BRK, BRK, BRK, EMP, EMP, EMP, EMP}, {ROC, ROC, BRK, BRK, BRK, BRK, EMP, EMP, RIV, RIV, RIV, RIV, RI V, RIV, BRK, BRK, BRK, BRK, EMP, EMP, EMP, EMP},

{Roc, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, EMP, EMP}, {ROC, ROC , BRK, BRK, BRK, EMP, EMP, RIV, RIV, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, EMP, EMP}, {ROC, ROC, BRK, BRK , BRK, BRK, EMP, EMP, RIV, BRK, BRK, BRK, BRK, EMP, EMP, EMP, EMP, BRK, BRK, EMP, EMP}, {ROC, ROC, BRK, BRK, BRK, BRK, BRK , EMP, BRK, BRK, BRK, EMP, EMP, EMP, EMP, BRK, EMP, BRK, BRK, BRK, EMP, EMP, EMP, BRK, EMP, EMP, EMP , RIV, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, BRK, BRK, BRK, BRK, EMP, EMP, RIV, RIV , EMP, EMP, EMP, EMP, EMP, EMP, EMP, ROC, ROC, EMP, EMP, ICE, ICE, ICE, ICE, ICE, ICE, BRK, BRK, BRK, BRK, BRK, BRK , ROC, ROC, ROC, ROC, ROC, EMP, EMP}, {Ice, Ice, Ice, Ice, Ice, BRK, BRK, BRK, BRK, BRK, BRK, ROC, ROC , ROC, ROC, ROC, ROC, ROC, ROC, EMP, EMP},

{Brk, BRK, BRK, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, BRK, BRK, BRK, BRK, BRK, BRK}, {BRK, BRK , BRK, BRK, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, BRK, BRK, BRK, BRK, BRK, BRK},

{Brk, BRK, BRK, BRK, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, BRK, BRK, BRK, BRK, EMP, EMP}, {BRK, BRK , BRK, BRK, EMP, EMP, EMP, EMP, BRK, BRK, BRK, BRK, EMP, EMP, EMP, BRK, BRK, BRK, BRK, EMP, EMP}, {BRK, BRK, BRK, BRK , EMP, EMP, EMP, EMP, BRK, EMP, EMP, BRK, EMP, EMP, EMP, BRK, BRK, BRK, BRK, EMP, EMP}, {BRK, BRK, BRK, BRK, EMP, EMP , EMP, EMP, EMP, BRK, EMP, EMP, BRK, EMP, EMP, EMP, BRK, BRK, BRK, BRK, EMP, EMP},}; for (int x = 0; x <22; x ) { For (int y = 0; y <22; y ) {layerbackground.setcell (Y, x, map [x] [y]);}}} ...} ...} is also like a method, due to handwriting a 22x22 The array is too trouble, I have not changed.

Compilation operation, the effect is the same last time, but because there is a map [] [] array, the map is generated and the storage is very convenient, we can easily determine if the tank can move (the condition is MAP [x] [Y ] <= 1, why should I put the ice in the first one to judge convenient), and it is convenient to modify the map. For example, a piece of brick is knocked, set MAP [x] [y] = 0 can!

(to be continued... :)

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

New Post(0)