Use MIDP2.0 to develop game (4) improved map generation method

Source: Internet
Author: User
Tags setcell

We used Sprite to implement the background last time, but it is not easy to generate a map through map data. Generally, maps in the game are divided into NxN squares, and there are only several patterns. Fortunately, SUN has taken into account the features required by game developers in MIDP 2.0. TiledLayer is the layer implemented by textures, and its usage is equally simple!

First, prepare the small area we need. There are five types of map in the tank war:

(Note that this is the jpg format to be enlarged. You need to process it in png format of 40x8)

They are ice, bricks, rocks, rivers, and trees. I will not say much about them.

First, read the 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 square number of NxN, and the last two Parameters specify the square size (not necessarily a square). We use the 8x8 size, in this way, the 40x8 png image is divided into 5 small pieces, numbered 1-5. Why not start from 0? Because 0 is used to represent transparent blocks.

The following defines a two-dimensional array to represent 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); to attach the corresponding image to each small block. If the index is 0, the small part is transparent.

OK. We changed 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 ICE = 1;
Int BRK = 2;
Int ROC = 3;
Int RIV = 4;
Int TRE = 5;
// The map is too large !!!
Int [] [] map = new int [] [] {// 22X22
{EMP, EMP, EMP, EMP, EMP },
{EMP, EMP, EMP, EMP, EMP },
{ROC, ROC, EMP, ICE, BRK, BRK, BRK, BRK },
{ROC, ROC, EMP, ICE, BRK, BRK, BRK, BRK },

{BRK, BRK, ROC, ROC, BRK, ROC, ROC, ROC, ROC },
{BRK, BRK, ROC, ROC, BRK, ROC, ROC, ROC, ROC },
{ROC, ROC, BRK, EMP, EMP, RIV, BRK, EMP, EMP, EMP, EMP },
{ROC, ROC, BRK, EMP, EMP, RIV, BRK, EMP, EMP, EMP, EMP },

{ROC, ROC, BRK, EMP, EMP, RIV, RIV, BRK, BRK, EMP, EMP },
{ROC, ROC, BRK, EMP, EMP, RIV, RIV, BRK, BRK, EMP, EMP },
{ROC, ROC, BRK, EMP, EMP, RIV, RIV, BRK, EMP, BRK, BRK, EMP, EMP },
{ROC, ROC, BRK, EMP, EMP, RIV, RIV, BRK, EMP, BRK, BRK, EMP, EMP },

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

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

{BRK, BRK, EMP, BRK, BRK, EMP, EMP },
{BRK, BRK, EMP, BRK, EMP, BRK, BRK, EMP, EMP },
{BRK, BRK, EMP, BRK, EMP, BRK, BRK, EMP, EMP },
{BRK, BRK, EMP, BRK, EMP, 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]);
}
}
}
...
}
...
}

The outlook is also like the way it is done. Because it is too troublesome to write a 22x22 array, I have not changed it.

Compilation and running, the effect is the same as the previous one, but with the map [] [] array, map generation and storage are very convenient, we can easily determine whether the tank can be moved (the condition is the corresponding map [x] [y] <= 1. Why do we need to put ice cubes first for convenience ), you can also easily modify the map. For example, if a brick is destroyed, you can set map [x] [y] to 0!

(To be continued...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.