Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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 617 times)
0 Members and 1 Guest are viewing this topic.
Offline GustavXIII

Junior Devvie


Projects: 1



« Posted 2012-12-26 22: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-26 23:17:29 »

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

JGO Kernel


Medals: 114
Projects: 3


You think about my Avatar right now!


« Reply #2 - Posted 2012-12-26 23: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 Devvie


Projects: 1



« Reply #3 - Posted 2012-12-27 16: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.

rwatson462 (36 views)
2014-12-15 09:26:44

Mr.CodeIt (29 views)
2014-12-14 19:50:38

BurntPizza (61 views)
2014-12-09 22:41:13

BurntPizza (98 views)
2014-12-08 04:46:31

JscottyBieshaar (58 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (76 views)
2014-11-29 21:32:03

toopeicgaming1999 (136 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (37 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!