Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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 9843 times)
0 Members and 1 Guest are viewing this topic.
Offline matejm1994

Junior Newbie





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

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!
Online ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


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

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

Example:
1  
2  
3  
4  
5  
6  
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 Grin

Offline aazimon
« Reply #2 - Posted 2011-02-23 20: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 23: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-24 00:32:37 »

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

Senior Member


Medals: 3
Projects: 1


Java games rock!


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

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/
Online ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


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

Here is some example code when you parse the file.

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

Offline matejm1994

Junior Newbie





« Reply #7 - Posted 2011-02-24 22: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 23: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. 

Online ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


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

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! 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 15:14:05 »

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

Junior Newbie





« Reply #11 - Posted 2011-03-20 19: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
Online ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2011-03-20 22: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.

 

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

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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!