Rama
Rama

Reputation: 4767

Appropriate level representation / data structure for a 2D platform game?

I'm about to program a copy of Mario in Java. I'm thinking on 2 representations/data structures for the levels but I'm not sure which one should I choose:

What are its advantages and disadvantages?

Upvotes: 2

Views: 354

Answers (2)

Ryan Amos
Ryan Amos

Reputation: 5452

Really, it's up to you. One way I can think of doing it is to have an 2d array of map tiles, either clear or an object. Tiles would have a certain size. Of course, then you have the problem of if somethings half a tile. Another option is an array of objects with the object data and location inside. This is less efficient, unless you sort the data.

If you think your maps are going to be very heavy on objects, I'd suggest a 2d tile map. Otherwise, I'd go with an array of objects or parallel arrays (your choice) specifying location and type of an item.

Upvotes: 2

CodeGuy
CodeGuy

Reputation: 28907

Definitely a 2d array of some type. Integers would be a good idea, however characters would be an even better idea.

Consider making a text file which is basically a "map". It could be 10 rows by 10 columns of text. A very simple map, in this case. Maybe you could use an "a" to signify grass, and a "b" to signify brick. In this way, you could even visually understand your map before you put it into action. But you could basically program your application to depend on this text file for the map.

For instance, consider this map:

bbbbbbbbbbbbbbbbb
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa

So, that would look like a long brick road above 3 long rows of grass. Imagine making this scheme much more complicated, using all kinds of characters a-z and A-Z, even punctuation like $ or % or &.

Then, you could create maps on the fly just by altering the text file. If you use integers, you are limited to only 10 characters (or 10 map objects, per se). For instance, what if you have more 10 objects, then how could you make a text file where the digits are next to each other. You won't know how to seperate the digits.

The downside of a quadtree is that it's overly complicated and won't allow for quick access to specific elements.

Upvotes: 5

Related Questions