Map design and drawing in the development of J2ME games

Source: Internet
Author: User
Tags array modify
Design in the development of many types of games, the map system needs good design, intuitively, we need the map system is only a 2D array, and then the fastest way to insinuate the array to the screen.

The map in the game is usually not by the programmer with the keyboard input into the program and then modify in the program and then modify the frenzied process, but generally first by the programmer to do a map editor, in this map editor with a mouse point, and then save the process, Or you can download some mature editors from the network such as: Mappy such tools to generate maps, and then use scripting language for Mappy write a program should be saved in what format. Usually the map is divided into 45 degrees, side angle and overlooking angle, and so on, there are also many kinds of angle of 45 degrees, this angle of view relative to the angle and side view is more complex, we mainly discuss overlooking angle, in fact, look at the main difference between the look and look at the picture is not the same style, such as thunder and Lightning is Mario's game is a side view that can be done with the same map editor. In summary, you need to know that the game map is not written by programmers, you like to write can also be modified more cumbersome, and can not be as dynamic management of resources, but a one-time read into memory, more uncomfortable.

In this article, we assume that our 2D array is read by the resource, and the contents are as follows:

public static byte[][] B_maze_2d_array = {
{
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, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
, {
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
, {
0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
, {
0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}
, {
0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}
, {
0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}
, {
0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0}
, {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0}
, {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 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, 0, 0}

};

This map has 0, 12 byte numbers, 0 represents a block, 1 represents a block, can also be animated tiles, you draw the time difference is OK. Because we are in the program to draw out the entire graph, there are more than N solutions, I gave two more reasonable plan, of course, the second is more optimized. We assume that your protagonist has been in the center of the screen, when you move the protagonist, the map corresponding changes, that is, the protagonist for reference, map move. We know the size of the map to exceed the screen, we need to set a coordinate system, my method is that the upper left corner of the 0,0 is the same as our common canvas coordinate system is the same, our tile size: element_width, Element_height, So the area of our entire map (absolute area) is the number of blocks of element_width * element_height* ordinate. Therefore, when we put such a large picture on the screen, we need to draw the coordinates of the area (that is, the screen area) from the map out of the other places have been removed, which is more efficient.

Method One: Cycle the entire 2-dimensional array, do not need to draw the part that only needs to be painted:

int KI = 0;//Express
int KJ = 0;
Nelestartedx, Nelestartedy indicates which location to start drawing a map from 2D array
for (int i = Nelestartedy i < b_maze_2d_array.length; i++) {
KJ = 0;
Whether you need to draw
Boolean isdrawed = false;
for (int j = Nelestartedx J < B_maze_2d_array[i].length; J + +) {
Plot the required area, N_maze_element_width,n_maze_element_height represents the tile width and height
int BX = Nmapstartedx + J * n_maze_element_width;
int by = Nmapstartedy + i * n_maze_element_height;
Screen_width,screen_height Screen Size
if (BX <= screen_width
&&
by <= Screen_height
&&
BX >=-n_maze_element_width
&&
by >=-n_maze_element_height
) {
G.drawimage (Mapimages[b_maze_2d_array[i][j]], BX,
By,
Graphics.top | Graphics.left);//Drawing blocks
Isdrawed = true;
kj++;
n_max_maze_item_x, n_max_maze_item_y the maximum size of the tiles within the screen area
if (KJ > n_max_maze_item_x + 2) {
Break
}

}
}
if (isdrawed) {
ki++;
}
if (KI > n_max_maze_item_y + 2) {
Break
}

}

Method two: In advance to find the need to draw the horizontal axis of the graph block number (2DArray array subscript), circular screen size of the array:

The position of the 2DArray upper-left corner to be drawn, the coordinates of the nmapstartedx,nmapstartedy on the absolute area of the map
int narrayi = (-n_maze_element_height-nmapstartedy)/
N_maze_element_height;
int narrayj = (-n_maze_element_width-nmapstartedx)/
N_maze_element_width;
for (int i = Narrayi;
I < Screen_height/n_maze_element_height + 2; i++) {
for (int j = Narrayj;
J < Screen_width/n_maze_element_width + 2; J + +) {

if (I < 0 | | | J < 0 | | i > B_maze_2d_array.length | |
J > B_maze_2d_array[0].length) {
Continue
}
else {
int BX = Nmapstartedx + J * n_maze_element_width;
int by = Nmapstartedy + i * n_maze_element_height;
G.drawimage (Mapimages[b_maze_2d_array[i][j]], BX,
By,
Graphics.top | Graphics.left);

}
}
}

According to my test, method one of the larger map area fps off the more severe, and method two basically will not lose FPS, strongly recommended method two.

After the map system is done, you can use the map to do more expressive, as long as you change the nmapstartedx,nmapstartedy, you can draw the corresponding part of the map, the code reuse efficiency is very high. RPG, SLG, Puzzle and other game types can be used. Welcome to talk to me about more game making techniques, and I'm going to write a related thing about animations, but I haven't had much time lately.



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.