Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to represent the levels in a 2D side-scroller?

I've no games programming knowledge and have oft wondered how the levels in 2D games such as Mario and Sonic (etc) are stored.

'Stored' as in how is the data (ground, platforms, buttons, lifts, etc) stored.

I.e. obviously a level in mario is NOT some kind of very wide image which is moved from left to right.

like image 668
Dynite Avatar asked Jul 16 '09 13:07

Dynite


1 Answers

Thanks to the ROM hacking scene the file format for Super Mario World (and I'd imagine most other popular games of the era) is well known and basically fully understood. There are also documents out there which will describe it all in painful detail. Unfortunately because it means going to less-than-legal sites I'm can't supply you any links from work, but if you google for the .MWL file extension and an editing application called Lunar Magic it may point you in the right direction.

The basic principle though, is fairly simple. First you need your graphics, so you make a single image with tiles for the landscape at a defined size - smaller is more memory efficient, larger gives you more detail, so let's say ours are 32 X 32 pixels. So you end up with something like this (Characters representing different tiles):

    ! " £ $ %
    ^ & * ( )

You can make one title set per "style" of level, so fire world, ice world, cave world etc. This saves you loading in tiles that you aren't going to use.

Next you need a level file, this starts off as consisting of which tile set you want to load and numbers representing each graphic, like so:

   fireworld.img
   2 2 2 2 2 2 2 2
   3 3 2 2 2 2 3 3
   3 3 3 3 3 3 3 3

Merged with the tileset above you'd have (depending on how you number the tiles)

   " " " " " " " "
   £ £ " " " " £ £
   £ £ £ £ £ £ £ £

Obviously you then need to layer more information on top of this: Which tiles are solid, which are deadly etc. This can be done at the image level (a 3 is always solid) or, for more flexibility but larger files, at the map level. Bit flags will do, first number is solid, second is deadly:

   fireworld.img
   200 200 200 200 200 200 200 200
   310 310 200 200 200 200 310 310
   310 310 310 310 310 310 310 310

On top of all of this you need to suffix the level file with a start and end point (a couple of coordinates) and enemies (which graphic they use, which AI routine they use, where they start).

Once all this is done you can look into compression. There are a number of ways to save space and obviously it doesn't matter as much now as it did in the 16 and 8 bit eras, but even so our format above is massively wasteful.

As always these are just the basic principles. Your results may vary...

like image 60
Martin Harris Avatar answered Sep 22 '22 03:09

Martin Harris