Drawing Tiles randomly - Error

Hey, I just tryed to draw a world random.
So I made a number between 1-700.

This is how I draw the world.


for(int x=0;x<mapArray.length;x++){
            for(int y=0;y<mapArray.length;y++){
            if(mapArray[x][y] <=500 )
                g.drawImage(grass,x*16,y*16);
            else if(mapArray[x][y]> 500 && mapArray[x][y]<= 580)
                 g.drawImage(tree,x*16,y*16);
            else if(mapArray[x][y]> 580 && mapArray[x][y]<= 699)
                 g.drawImage(earth,x*16,y*16);
             else if(mapArray[x][y] == 700){
                 g.drawImage(wall,x*16,y*16);
                    }
            }
         }

The world will be drawn correct.

http://s14.directupload.net/images/121226/temp/z95hcnfj.png

I just wonder why the tree below right has the wall as a background?!?!
But more importent, I wanted to draw beside a wall another wall.
But I just dont get it to work, so can someone tell me how to do it?

Maybe you didn’t add alpha to the tree image?

Ehh… It’s really just a guess, but do you clear the screen?

Also, in the second for loop you should use [icode]mapArray[x].length[/icode]: [icode]for (int y = 0; y < mapArray[x].length; y++)[/icode], else you could run into ArrayIndexOutOfBoundsExceptions (I like to call them aioobe :slight_smile: ) if something is not that right…

And finally I’d do it different, but that’s just me:

I would have initialization code:


public void init() {
	Random rand = new Random();

	for (int x = 0; x < mapArray.length; x++) {
		for (int y = 0; y < mapArray[x].length; y++) {
			int i = rand.nextInt(700);
			if (i == 700) mapArray[x][y] = WALL_ID;
			else if (i > 580) mapArray[x][y] = EARTH_ID;
			else if (i > 500) mapArray[x][y] = TREE_ID;
			else mapArray[x][y] = GRASS_ID;
		}
	}
}

And in the rendering I’d do this:


for (int x = 0; x < mapArray.length; x++) {
	for (int y = 0; y < mapArray[x].length; y++) {
		switch (mapArray[x][y]) {
		case GRASS_ID: g.drawImage(grass, x * TILE_SIZE, y * TILE_SIZE);
		case TREE_ID: g.drawImage(tree, x * TILE_SIZE, y * TILE_SIZE);
		case EARTH_ID: g.drawImage(earth, x * TILE_SIZE, y * TILE_SIZE);
		case WALL_ID: g.drawImage(wall, x * TILE_SIZE, y * TILE_SIZE);
		}
	}
}

And the constants would be written at the top of the class of your choice:


public static int GRASS_ID = 0;
public static int TREE_ID = 1;
public static int EARTH_ID = 2;
public static int WALL_ID = 3;

public static int TILE_SIZE = 16;

But finally this doesn't help with your problem... :persecutioncomplex: That's just me ::)

Ok thanks.
I did it now.
But there is a graphical error and I dont know why, Im using Slick.

When I go left the wall will be drawn in the “void”.
I dont know why it should not be seen there only black?!

This is how I create the walls:

for (int x = 18; x < mapArray.length-17; x++) {
          for (int y = 18; y < mapArray[x].length; y++) {
          if(mapArray[x][y] == value.WALL_ID){
              mapArray[x-3][y-1] = value.WALL_ID;
              mapArray[x-3][y-2] = value.WALL_ID;
              mapArray[x-3][y-3] = value.WALL_ID;
              mapArray[x-3][y-4] = value.WALL_ID;
              mapArray[x-3][y-4] = value.WALL_ID;
              mapArray[x-1][y] = value.WALL_ID;
              mapArray[x-2][y] = value.WALL_ID;
              mapArray[x-3][y] = value.WALL_ID;
              mapArray[x-1][y-4] = value.WALL_ID;
              mapArray[x-2][y-4] = value.WALL_ID;
              mapArray[x][y-4] = value.WALL_ID;
              
          }}}

I made
x = 18; x < mapArray.length-17
because I draw black rects as the border of the map.


//Border above
       for (int x = 0; x < mapArray.length; x++) {
          for (int y = 0; y < 17; y++) 
              mapArray[x][y] = value.END_ID;
          }       
        //Border below 
       for (int x = 0; x < mapArray.length; x++) {
          for (int y = mapArray.length-17; y < mapArray.length; y++) 
              mapArray[x][y] = value.END_ID;
          }
       
        //Border left
       for (int x = 0; x < 17; x++) {
          for (int y = 0; y < mapArray.length; y++) 
              mapArray[x][y] = value.END_ID;
          }
       
       //Border right 
       for (int x = mapArray.length-18; x < mapArray.length; x++) {
          for (int y = 0; y < mapArray.length; y++) 
              mapArray[x][y] = value.END_ID;
          }