# What is the better way of generating levels in android game using LibGDX

The game uses a png image to create the level. It uses the following code. I am finding it difficult to understanding. The following code is from Level.java:

@SuppressWarnings("unchecked")
public Level (GameScreen screen, int w, int h, int xo, int yo, int xSpawn, int ySpawn) {
this.screen = screen;
this.xSpawn = xSpawn;
this.ySpawn = ySpawn;

walls = new byte[w * h];
entityMap = new ArrayList[w * h];
this.width = w;
this.height = h;

for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
entityMap[x + y * w] = new ArrayList<Entity>();

int col = (Art.level.getPixel(x + xo * 31, y + yo * 23) & 0xffffff00) >>> 8;
byte wall = 0;

if (col == 0xffffff)
wall = 1;
else if (col == 0xFF00FF)
wall = 2;
else if (col == 0xffff00)
wall = 3;
else if (col == 0xff0000)
wall = 4;
else if (col == 0xB7B7B7)
wall = 5;
else if (col == 0xFF5050)
wall = 6;
else if (col == 0xFF5051)
wall = 7;
else if (col == 0x383838)
wall = 8;
else if (col == 0xA3FFFF)
wall = 9;
else if (col == 0x83FFFF) {
BossPart prev = new Boss(x * 10 - 2, y * 10 - 2);
int timeOffs = random.nextInt(60);
((Boss)prev).time = timeOffs;
for (int i = 0; i < 10; i++) {
BossNeck b = new BossNeck(x * 10 - 1, y * 10 - 1, prev);
b.time = i * 10 + timeOffs;
prev = b;
}
} else if (col == 0x80FFFF) {
Gremlin g = new Gremlin(0, x * 10 - 10, y * 10 - 20);
g.jumpDelay = random.nextInt(50);
} else if (col == 0x81FFFF) {
Gremlin g = new Gremlin(1, x * 10 - 10, y * 10 - 20);
g.jumpDelay = random.nextInt(50);
} else if (col == 0x82FFFF) {
Jabberwocky g = new Jabberwocky(x * 10 - 10, y * 10 - 10);
g.slamTime = random.nextInt(30);
} else if (col == 0xFFADF8) {
add(new Hat(x * 10 + 1, y * 10 + 5, xo * 31 + x, yo * 23 + y));
} else if ((col & 0x00ffff) == 0x00ff00 && (col & 0xff0000) > 0) {
add(new Sign(x * 10, y * 10, col >> 16 & 0xff));
} else if (col == 0x0000ff) {
// if (xSpawn == 0 && ySpawn == 0) {
this.xSpawn = x * 10 + 1;
this.ySpawn = y * 10 - 8;
// }
} else if (col == 0x00FFFF) {
Gunner e = new Gunner(x * 10 + 2, y * 10 + 10 - 6, 0, 0);
e.chargeTime = random.nextInt(Gunner.CHARGE_DURATION / 2);
e.xa = e.ya = 0;

}
walls[x + y * w] = wall;
}
}
player = new Player(this.xSpawn, this.ySpawn);
}


I want some advice as to whether this is a good idea or not? This game was developed in 2010 using LibGdx.

The other example I found using TiledMap is at http://chrismweb.com/wp-content/uploads/2013/03/superkoala/ at the offical site of libgdx.

• This is way too broad... what kind of level do you want to generate? – nathan May 25 '13 at 18:46

This level loader is an example of converting an image into level data (basically, they're reading pixels, and instead of interpreting the pixel data as a "color" its being interpreted as information about how the level works).

The main idea for this is to leverage some existing tool you're good at using (e.g., an image editor) to avoid needing to create a custom new "level editor" tool. In this example, any pixel-based image editor will work as a "level editor".

An alternative, along similar lines, would be to use a text editor as the level editor. The level loading code would interpret ASCII characters as level information. (E.g., read a "!" as a wall, and an "X" as the starting point, etc). You don't even need to load the level from an external file, at this point it can just be a String in your source code.

A final option is to just hardcode the level data in your source code (this is handy when you're just getting started and want to get a level working quickly to develop/debug). Imagine hard-coding a walls[5 * 5] = { 1, 1, 1, 1, 1, ...} for the above code.

• One more help please... what does this mean? int col = (Art.level.getPixel(x + xo * 31, y + yo * 23) & 0xffffff00) >>> 8; – Vishal Kumar May 31 '13 at 4:23
• Art.level is a Pixmap. Pixmap.getPixel() gets the pixel value at the given x, y coordinates. The coordinates are offset into the image (I'm guessing the levels are 31x23 or 30x22). The result of getPixel() is a 32-bit integer, most likely encoding the RGBA (red, green, blue, alpha, 8-bits per channel) channels of the pixmap. The "& 0xFFFFFF00" masking zeros the low order 8 bits (i.e., the alpha channel). The ">>> 8" shifts the whole thing right 8 bits, so the constants to compare to are just hex encoded RGB values. Thus col == 0xFF00FF checks if col is pink. – P.T. May 31 '13 at 5:01

IN my opinion there are two main ways to generate game levels.

1. Generate your level and place objects via logic based on world size width and limitation of your game characters

2. Generate and place your objects based on file. Like the code sample you have above, it will iterate through each line or column in your file and generate/place objects based on thier char reference or hex code.

I personally find option 2 better as your have more control over your level and can design it the way you want it. However I use a txt file, string buffer and Char to determine location or objects.

• Thanks Rhys... What tool can I use to draw the level.png bitmap for my levels. – Vishal Kumar May 26 '13 at 8:24