Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (595)
Games in Android Showcase (169)
games submitted by our members
Games in WIP (648)
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  
  How to create a map?  (Read 11429 times)
0 Members and 1 Guest are viewing this topic.
Offline matejm1994

Junior Newbie

« Posted 2011-02-23 17:49:32 »


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.

Offline ra4king

JGO Kernel

Medals: 388
Projects: 3
Exp: 5 years

I'm the King!

« Reply #1 - Posted 2011-02-23 18:54:50 »

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



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 Grin

Offline aazimon
« Reply #2 - Posted 2011-02-23 19:19:10 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matejm1994

Junior Newbie

« Reply #3 - Posted 2011-02-23 22:50:40 »

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

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?

Offline aazimon
« Reply #4 - Posted 2011-02-23 23:32:37 »

Can you post some code? And the error?
Offline Addictman

Senior Devvie

Medals: 3
Projects: 1

Java games rock!

« Reply #5 - Posted 2011-02-23 23:40:59 »


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:
Offline ra4king

JGO Kernel

Medals: 388
Projects: 3
Exp: 5 years

I'm the King!

« Reply #6 - Posted 2011-02-24 01:58:09 »

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()) {

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!

Offline matejm1994

Junior Newbie

« Reply #7 - Posted 2011-02-24 21:06:15 »

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?

Offline matejm1994

Junior Newbie

« Reply #8 - Posted 2011-02-26 22:59:00 »

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. Sad 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) Cheesy

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 Cheesy I was laughing to myself. 

Offline ra4king

JGO Kernel

Medals: 388
Projects: 3
Exp: 5 years

I'm the King!

« Reply #9 - Posted 2011-02-26 23:26:45 »

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! Grin

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PudgeCo

Innocent Bystander

« Reply #10 - Posted 2011-03-18 14:14:05 »

I have also found a tutorial that I stumbled on recently:

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.
Offline matejm1994

Junior Newbie

« Reply #11 - Posted 2011-03-20 18:13:30 »

@ 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 Cheesy And than I register to this forum Cheesy
Offline ra4king

JGO Kernel

Medals: 388
Projects: 3
Exp: 5 years

I'm the King!

« Reply #12 - Posted 2011-03-20 21:54:40 »

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.... Cheesy

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

KaiHH (24 views)
2015-07-07 11:39:58

deepthought (55 views)
2015-06-30 15:39:44

deepthought (58 views)
2015-06-30 15:39:09

deepthought (67 views)
2015-06-30 15:36:52

Za\'Anzabar (45 views)
2015-06-29 05:44:54

TritonDreyja (51 views)
2015-06-24 17:10:40

CopyableCougar4 (53 views)
2015-06-23 00:34:45

BurntPizza (58 views)
2015-06-21 20:36:46

cookiecompiler (100 views)
2015-06-11 15:42:53

cookiecompiler (58 views)
2015-06-11 15:41:14
How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14 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‑
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!