Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Drawing Tiles randomly - Error  (Read 507 times)
0 Members and 1 Guest are viewing this topic.
Offline GustavXIII

Junior Member


Projects: 1



« Posted 2012-12-26 23:48:36 »

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

This is how I draw the world.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
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.


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?


Offline tyeeeee1
« Reply #1 - Posted 2012-12-27 00:17:29 »

Maybe you didn't add alpha to the tree image?
Offline matheus23

JGO Wizard


Medals: 97
Projects: 3


You think about my Avatar right now!


« Reply #2 - Posted 2012-12-27 00:22:09 »

Ehh... It's really just a guess, but do you clear the screen?

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

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

I would have initialization code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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:
1  
2  
3  
4  
5  
6  
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;


<edit>
But finally this doesn't help with your problem...  persecutioncomplex That's just me Roll Eyes

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GustavXIII

Junior Member


Projects: 1



« Reply #3 - Posted 2012-12-27 17:22:05 »

Ok thanks.
I did it now.
But there is a graphical error and I dont know why, Im using Slick.
http://www.youtube.com/watch?v=fF6pK5e6QtI&feature=youtu.be

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
//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;
          }
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (49 views)
2014-04-15 18:08:23

BurntPizza (45 views)
2014-04-15 03:46:01

UprightPath (61 views)
2014-04-14 17:39:50

UprightPath (43 views)
2014-04-14 17:35:47

Porlus (59 views)
2014-04-14 15:48:38

tom_mai78101 (83 views)
2014-04-10 04:04:31

BurntPizza (141 views)
2014-04-08 23:06:04

tom_mai78101 (241 views)
2014-04-05 13:34:39

trollwarrior1 (201 views)
2014-04-04 12:06:45

CJLetsGame (208 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!