I am thinking about a new game idea for the iPhone and presumably all the levels would have to be made programatically. So Im wondering what the best method to approach it?

Say I have a game with a standard typical 2d, top down maze. Thise maze is simply made up of walls, multiple rectangles on screen, making paths.

Do I have to manually work out the position of every rectangle and put it in with an x and y position or is there a better way? I see this taking, forever, there must be certain ways developers approach these situations to get by them efficiently and quickly surely?


  • 1
    How you programmatically create levels really depends on what kind of game you are making. Creating a maze for example in a 3x3 grid makes a maze that's very easy to beat. Creating one in a 300x300 grid makes one that's difficult to beat. But again, how you create a dynamic level really depends on what kind of level you're making. – Tim Holt Dec 14 '10 at 23:39
  • 1
    Are you really making a maze game or is that just an example you're giving? – Tim Holt Dec 15 '10 at 2:19
  • It has layouts which resembles mazes. Which is why I used it as an example. – Josh Kahane Dec 15 '10 at 7:59

Are you saying you want to generate the levels, or do you want to create them "by hand" in code (world.addRectangle(10,10,100,20)etc)? If you want to generate the levels procedurally, look inte algorithms for maze construction and adapt them to your needs.

If you want to construct the levels yourself, you can place the data in a separate file and parse it. Can be as simple as rectangles in a csv file, or xml. (Or svg, in which case it would be a small step to creating the levels in a program like Inkscape. But then you'd need to use or write an svg parser.)

Some languages (Lua for example) make it really simple to read data from files and turn them into data structures. Since you're writing a game for the iPhone, you can play with creating your levels using a plist. You can read them into a data object with one method invocation.

I'm confused as to your use of 'programatically'. Do you mean procedurally, that is generate-as-you-go, or in the code, hardcoded? I'm pretty sure you don't need that in iPhone games - how would they have the images?

So instead of having to hardcode the coordinates, you want to save the effort and use an editor. A question on 2D level editors.

I would use a level editor, or just put it in a data file. Literally something like:


It would be quicker than the rectangles definitely, but not as good as a level editor - which would be simple to make or use an existing one, and would generate similar output.

What I'm suggesting is to stop thinking about rectangles and start thinking about walkable and unwalkable tiles. Your walls would obviously be unwalkable, and the floor walkable. You would have a graphic (or just a black square drawn) where a wall is, and you would use those tile coordinates as the collision boundary. Lots of tiles = a wall.

Even if you do mean procedurally - which is not a limitation of the iPhone platform - the above would still work, just without the files. Google anything about maze generation or roguelike maps.

For such a job I figure you'll get away with drawing the maze using a paint program and have a piece of code turning the image into a what data you need. Of course, if you just need a maze you could just write a maze generator, with the added benefit of being able to make random mazes on the fly. There is some interesting talk on the subject here: Reduce the number of edges of a graph, keeping it connected

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.