# How to create levels (tile maps) [closed]

I am new to game programming and I am having so much hard time learn on my own. My goal is to make a game with different levels.

so far my plan:
i have 2 classes
1 - main.java
2 - level_01.java
3 - more levels if i want

in my level_01.java:
1- make 2d array of map(done)
2- Do i need to store the make in arraylist or list?????
3- print in paint method?

public class level_01 extends levels
{
private int map01[][] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{2,2,2,2,2,2,2,0,0,2,2,2,2,2,2}};

//Is a good idea to store this map in list or arraylist?
//if yes should I use ArrayList or list?
//or should i just print the right now?

• Judging by the way you wrote the names of the classes and the fact you want to build one level per class, I'd say you should probably start learning Java first. Get used to the language, then learn object oriented programming. Otherwise you're gonna have a hard time starting with gamedev. Mar 22 '13 at 10:14
• There are many questions on the site about creating tile maps. Check them out. Also look for a tutorial on creating tile maps. I think this question is not constructive, you want a discussion, someone to talk with about Java tile maps. Check the FAQ to learn where to ask such discussion oriented questions. Mar 22 '13 at 13:06

If you're having this problem, maybe it's time to load maps from files. Create a Level class and have it load data from file. Typing levels into external files is much more flexible than creating a separate class for each level.

Two approaches are fairly simple (and commonly used):

• create a level1.txt file, in a format similar to the Java code you posted. You can edit in Notepad.
• create a level1.png file (yes, an image!), where each pixel represents a type of tile (pure red maps to tile type 0, pure green to tile type 1 etc). You can load it and read each pixel, similarly to reading a text file. You can edit in a paint-like graphics editor; the advantage is, it's really easier to "see" what's going on in an image, and paint is a better tool for this than notepad (because you can use a mouse). As weird as this sounds, it's a pretty decent approach (used for instance by Legend of Grimrock).

If this sounds to complicated for you, maybe you should try enhancing your general programming skills. Game programming is pretty complex, so it's usually a bad idea to learn programming through gamedev. Try doing some more algorithmic exercises, code kata or general Java tutorials.

• Instead of abusing Notepad or Paint as a map editor, you should rather use a dedicated tool. The productivity of your map design will be much better. And writing an XML or JSON parser which understands the output format of Tiled isn't much more work than writing one which parses PNG files. Mar 22 '13 at 15:17

You could use an editor tool.

Tiled comes to my mind. It allows you to create your map in a graphical interface and save it to a file. You just need to learn how to load and work its format into your game.

Hardcoding levels is OK during the early stage of development, but in the long run it will be inadequate. You will soon realize that it is a good idea to separate programming from game content.

Instead of using an own class for each level, you should rather use one generic Level class which takes a filename in the constructor and loads the map layout from a file.

Regarding the data structure to store your map data in your level class: You will find that you frequently access tiles by index, so using a two-dimensional array or two-level nested ArrayLists should be the best choice.

There is, however, a special case where a HashMap or a multidimensional tree might be better: When you have very large but sparse levels. Levels which span a very large area, but most of it is empty. In that case arrays would be very memory-inefficient, because they would have to waste space for all the empty areas.

For editing the level files themself, you should use a what-you-see-is-what-you-get editor. It's much more productive than entering the pure numbers and then loading the map in the game to see how it actually looks. Especially when your map format grows more complex and is more than just a single layer of map tiles. You could write your own map editor, or you could use an existing map editor like the free, open source editor Tiled.

When you program your own editor, you can have the map files be exactly how you want them, but when you use a 3rd party program like Tiled you either have to write a parser which is able to read the map format or write an export plugin which creates files in your preferred format. But that's ususally still a lot more work than writing your own mapping application from scratch. Unless you have very special requirements for your map editor and lots of manpower to spare, you should better use an existing solution.

• great advice guys just one question. when i load a map from txt file do i need to store it in list or array? or should i just load the txt and paint on screen? if answer is use array or list. than which list should i use. arraylist or list or 2d array?
– dave
Mar 22 '13 at 20:56
• @dave You certainly want the players to interact with the map in some way (as in different tiles having different properties), so you need to store it somewhere. List is an interface. You can't create instances of it directly. ArrayList is one class which implements this interface. Another one is the LinkedList which I wouldn't recommend in this case. Mar 22 '13 at 21:00
• @dave About the question when to use an array and when to use the class ArrayList: stackoverflow.com/questions/716597/… Mar 22 '13 at 21:04