.txt map basics

well i have some questions about storing maps in .txt files, i’ve read a lot on the subject and nothing seems to be enough to get me started on it.

well, i’ve always wanted to be a game developer, and to be honest i thought it would be much easier, but realizing how hard it is only makes me want to learn more.

so a part of the process of realizing how hard it was, was trying to get started in this game developing world by making an isometric game, (like final fantasy tactics (fft)), and well it didnt take too long until i noticed starting with an isometric game wasnt the best idea, so i decided to start with simple stuff and made some really crappy simple games like pong.

so i’ve got some experience with how the drawing part works on the screen with java, and got more familiar with the way coordinates work and other stuff, and finally i decided to start working on a harder game, yet not as hard as a fft game, i decided to start making a simple side scroller.

when i say side scroller i mean it like super mario, (not megaman or metroid which are more complex).

so after making some research, i noticed that im going to need something called “tilesets” which contain all different tiles a map would use, (instead of having a single file for each tile, you have all the tiles in one file), so i have read a lot about that and i cant get it.

what i want, is a clear explanation of how it works (how to write/read from a txt file, how to take the parameters there and asign them a tile, and then how to take that tile from the tileset).

and if you cant explain it for some reason, then it might also be helpful if i could get a source code of a game that works with this.

thank you for your patience, i really need to get this mapping technices working so i can advance on my project.

Ah isometric game, should be added to my list :slight_smile:

If you say about txt map, I think you’ll create a huge world sized game. For starting, I think it’s not necessary if it’s not that big. Try to make your game on separate image files first. After done, you can try combine them into one file.

Basiclly, it’s just about cropping. You decide the offset and size like drawing rect at g2d. The txt file is (for me) used for indexing.

[quote]when i say side scroller i mean it like super mario, (not megaman or metroid which are more complex).
[/quote]
platformer

Instead of worrying about .txt files I would start by hard-coding the map directly into your code.

For example:


public class Mapz {

    private char[][] tiles = new char[][] {
        {'*','*','*','*'},
        {'T','.','*','*'},
        {'*','*','*','*'},
        {'*','*','~','~'},
        {'.','.','~','~'},
        {'.','.','~','~'},
    };

    private char getTile(int row, int col) {
        return tiles[row][col];
    }


    public static void main(String args[]) {
        Mapz mapz = new Mapz();

        System.out.println(mapz.getTile(0,0));
        System.out.println(mapz.getTile(1,0));  // T
        System.out.println(mapz.getTile(4,2));  // ~
        System.out.println(mapz.getTile(5,0));  // .
    }
}

Once you have your data structures working then you’ll have a better idea of what files you need and can begin implementing them. It sounds like what you’re trying to do could get a bit complicated and thus the recommendation to get files out of the picture for now.

well that sounds like a good idea, to keep the map inside the code while i worry about other stuff, sounds smart, anyways another question that i forgot to add last time:

how does the paint method work for arrays, like how does it know where to paint each square, and if the character moves, how to move all the tiles with it?

thank you

well, u just treat it as your data for your map, its not really your map.

for example u could have a paint method in your map class where you have a double for loop (a loop in a loop), and then u just cycle throught he length and width of the map, drawing each tile.

and as for movement, just do all of your logic, and then change your map (if necessary) accordingly

There is a small “bug” in Loom_weavers code. The first array index is the Y position. In the getTile(int row, int col) function it should return tiles[col][row]. The second System.out.println() which prints T should print *, right?

If you flip it that way, not only is it easier to relate the raw map file/code to the actual map, but you can draw the map in a console with just


for(int row = 0; row < tiles.length; row++){
    System.out.println(tiles[row]);
}

If you want to have actual 2D graphics, it’s easiest to use an image with a tile map. It’s usually a big image with lots of smaller tiles (the ancient standard is 32x32 tiles). You then map each char in your char[][] map to an index in your tile map (in other words, you say that this char means this tile in the image) and draw that tile in its (x, y) position on the screen. To achieve scrolling, just offset the drawing with the character position.

You can google most basic questions about the java language, if you’re unsure of some functionality :slight_smile:

No loom_weaver’s code is correct. row = y axis, col = x axis :wink:

Yeah. X = Y and Y = X. It’s not a bug, but it makes things much harder to read. Should System.in and System.out switch name? xD