How to create a map?

Hello,

First, I apologize if I wrote a topic in the wrong place (You can move it) , and I also apologize for my bed English.

I started to learn GUI programming not so long ago. So far I’ve done a really basic 2d game. Nothing special. Background, 2 enemies, you have 10 bullets and you have to kill the enemies. If you touch the enemy, it is the end of the game. The character can move left, right, and you can jump once. However, in the “game” there are no barriers. So I searched the Internet, what would be the easiest way to make them. I found a way that it seems to me it is called “tile-based map” or. something like that. Ok, I said to myself, and I try this, but here I get stuck. I know I have to make .txt file, and then with the characters “draw” folder. Okay, but how do you read this folder, and a char replaced with an image?

And another question, how can I make this map so the figure can wake through the barriers? With Collision Detection, or something special?

BTW: It is not for homework.

Thanks!

The .txt file is basically an ASCII visual map. In your code, you import this file and decode it to place “tiles”.

Example:


map.txt

---E----E-
--PPP--PP-
H---E-----
PPPPPPPPPP

Then you take this and your program would decode it to use ‘-’ for space, ‘E’ for enemy, ‘P’ for platform, and ‘H’ for hero, create a world with the specified ‘tiles’, and put the corresponding images on the screen with their appropriate properties.

Hope that helped ;D

For collisions with elements in the map: Let say your tile is 10x10 pixels and your characters are the same. When any character moves, you need to check his bounding box with a location on the map. If there is a block at tile location 3,4, and the character is moving from 2,4 to the left (3,4), you would set the characters position back to the 2,4 location.
Most likely you will have your characters position in pixels, so he would be at 20, 40. When he tries to move, say 3 pixels to the left, to 23, 40. You need to check the x 23 to 33 range for any blocks in the map.
If the character was moving 3 pixels to the left and he was at 19 on the x, you would end up placing him at 20, because of the block at 30.
Makes sense?

Well, thanks to both, now I understand better, but anyway, when I try to write this program, all it does is error :smiley:

Can somebody tell me, if this is the right idea:

  • I need 2 for loops, one for high of map, another for length of each row. Or something like that…
  • We check each number, letter or whatever it is, and we put corresponding image on the screen. I have to use (lots of) If statement?

Can you post some code? And the error?

Hi.

What you are trying to do is pretty well covered in David Brackeen’s book “Developing games in java”. It’s a few years old now, but you should be able to find decent solutions to your problems there.

Link to website with downloadable (free) source code: http://www.brackeen.com/javagamebook/

Here is some example code when you parse the file.


Scanner scan = new Scanner(new File("map.txt"));
ArrayList<String> lines = new ArrayList<String>();
while(scan.hasNextLine()) {
   lines.add(scan.nextLine());
}
scan.close();

MapObject objects[][] = new MapObject[lines.size()][lines.get(0).length()];

for(int a = 0; a < lines.size(); a++) {
   for(int b = 0; b < lines.get(a).length(); b++) {
      char c = lines.get(a).charAt(b);
      
      if(c == 'E') objects[a][b] = new Enemy(a,b);
      if(c == 'P') objects[a][b] = new Platform(a,b);
      if(c == 'H') objects[a][b] = new Hero(a,b);
   }
}

Of course, this is very simplified and building on my previous example but this should give you some insight on how it generally could be designed. (MapObject is a class I just made up that could be the superclass to all objects placed on maps)

Hope that helped!

Well thanks, I will write longer answer in Saturday evening, when I’ll come home. I am not on my computer now. I will post the code and error, If It will not work. But I think now will work. It must work! =)

So, basically I need just 2 classes? (If I want just to draw a Map, with no enemy, no hero, no movement…) One to draw a frame, and one to get a Map from map.txt, and draw it?

Well, I have got a “game” with some classes (Frame, Board, Enemy, Bullet, Dude). In Board class, I have got a method, to load a background (ImageIcon i = new ImageIcon … )And it just work perfectly, but it is just background… (was making the game step by step with tutorial on Youtube. )

I looked at code in David Brackeen’s book, but I don’t understand it. There are “thousands” of classes, and a lot of them are almost equals. Are all of this even necessarily? Maybe I will understand, if I will buy a book, but for now, there is no money for this. :frowning: I need just this, then, moving, collision, enemys and all other stuff are much more easier than this. So, can I please you something? If you know any site, with step by step tutorial, how to make very simple tile based game, can you please post the link of the website here? I was searching the web, but I haven’t found tutorial which explain how to create tile based game in java, step by step. Or if you know a site where I can download a source code of a simple tile based game, and than I will probably understand how to make this game. (But it have to work) :smiley:

Ow, and about error, which I mention some posts above. I was doing something very stupid. My code was a disaster from first to last line :smiley: I was laughing to myself.

http://www.tonypa.pri.ee/tbw/start.html

This is a good and detailed tutorial about tile based games. However, the language used is Flash’s ActionScript but the main concept is still there.

Good luck! ;D

I have also found a tutorial that I stumbled on recently: http://www.cokeandcode.com/collisiontilemaps

This page shows the code he’s used also to create a tile based map, unfortunately it does not use the same *.txt file based concept, but the code is commented and should help you with the ideas of the looping you’re trying to achieve.

NOTE: If you can’t find the code, the *.java files are linked right near the top of the page under the Introduction section.

@ PudgeCo: Thanks. I will try. I am very happy to see tutorial with source code. Now, I think I will make it.

Can I say one funny think, I am almost 17 years old now, and I am in computer School. We learn Java there. Well, but when I asked a professor, what I need to made this kind of game, he didn’t know :smiley: And than I register to this forum :smiley:

Wow, if a Computer Science professor doesn’t know this, then he must be pretty inexperienced. I am 15 years old and my high school CS professor teaches at a college! However, I taught myself all this so… :smiley: