Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 make maps in a simple 2D game?  (Read 1147 times)
0 Members and 1 Guest are viewing this topic.
Offline mashikag

Senior Newbie





« Posted 2013-02-26 01:00:37 »

Hello,
I haven't got much experience in game programming and especially in 2D platformers. I am wondering how do you make such maps because I can see them as one long spritesheet but this does not seem to me quite right as I cant see people making such long maps in one spritesheet. Another way I could imagine is split it into smaller images which joined together one after another would make up whole map or another way is generating the enviroment on the run but this would be quite difficult.
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #1 - Posted 2013-02-26 04:00:44 »

What a lot of people do for simple tile-based games is they store levels as images where each pixel corresponds to one tile.

Colors are stores in images as integers, with a component for the red, green, and blue values of the colors (more here http://en.wikipedia.org/wiki/RGB_color_model)

You can make these images in something simple like MS Paint or GIMP or Paint.NET. For example, a floor tile could be represented as a white pixel, while a wall tile could be a black pixel. A health pickup could be red, an enemy could be green, and so on. You can load this image into your game and have the computer make an array of ints, where you then say, "Okay, if this pixel here is white, put a floor tile in the game world at these coordinates. If it's black, make a wall" and so on.

What's nice about this is that you can pretty easily edit levels on the fly by just opening up the paint program.

Here's a method I made for one of my games to get the int[][] of colors out of the original image:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
//this is what's supposed to load the RGB values from the .png into my map[][] array
  public int[][] loadRGBMap(String directory){  
      int[][] map;
     
      BufferedImage img = ResourceLoader.loadImage(directory);

      int w = img.getWidth();
      int h = img.getHeight();
      int[] pixels = new int[w * h];
      img.getRGB(0, 0, w, h, pixels, 0, w);
     
      map = new int[w][h];
      levelMap = new boolean[w][h];
      //this channels the 1 dimensional pixel[] into the two dimensional map[][]
     for(int row = 0; row < h; row++){
         for(int col = 0; col < w; col++){
            map[col][row] = pixels[row*w+col];
         }
      }
     
      return map;
   }


"directory" is just where the map is stored, ie "levels/level1.png"
"ResourceLoader" happens to be my own class, but all it does it load a BufferedImage

Hopefully that should get you started
Offline Agro
« Reply #2 - Posted 2013-02-26 04:07:15 »

Couple of different ways. Like ^ says, you can use tile maps(text files that use characters to represent objects).

For example,
######
#        m #
#    p     #
#          #
######

p represents a player, and m represents a monster, and # represents walls.

Another alternative is color maps. This gives you MANY MANY different possible objects(char tilemaps only give you around 256 i think). How many colors there are are the number of objects you can use.

The last alternative is a binary file. You can do something like this:

[object id] [xcord] [ycord]

There are many other ways to do it, but these are jsut a few

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2013-02-26 04:28:10 »

You might find this article interesting: http://www.tonypa.pri.ee/tbw/start.html

Offline relminator
« Reply #4 - Posted 2013-02-26 15:43:41 »

I just used text files edited in Notepad++ due to lack of time in my latest platformer.

Non-java code:

http://www.rel.phatcode.net/MyProgs/PyromaxDax.zip

For large and bigger projects, I usually use the game engine to make a dedicated map editor.
Offline mashikag

Senior Newbie





« Reply #5 - Posted 2013-02-27 01:24:06 »

Thanks, I have found all of the above replies really helpful.  Smiley
Offline mashikag

Senior Newbie





« Reply #6 - Posted 2013-02-27 01:24:55 »

I just used text files edited in Notepad++ due to lack of time in my latest platformer.

Non-java code:

http://www.rel.phatcode.net/MyProgs/PyromaxDax.zip

For large and bigger projects, I usually use the game engine to make a dedicated map editor.


That's so cool, just out of curiosity how long have u spent on this game?
Offline relminator
« Reply #7 - Posted 2013-02-27 01:36:39 »

Two months worth of my free time. ;*)

Basically I don't code daily but if I have to average, it's probably an hour a day doing it everyday for two months.
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.

Dwinin (29 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (76 views)
2014-09-10 06:39:09

Tekkerue (38 views)
2014-09-09 02:24:56

mitcheeb (58 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (30 views)
2014-09-07 01:12:14

Longarmx (35 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
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

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

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

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!