How to store levels efficiently in a 2d game

Started by
3 comments, last by Simitus 2 years, 9 months ago

I'm confused as to how I should go about storing my levels in my 2d side scroller. Right now for my background, I am reading from a .txt file that contains 0 if that area is empty or a number from 1 to (however many different sprites I'm using) which correlate to different sprites on my sprite sheet. This file then gets converted into a 2d array. For any number other than 0 I hard code the x and y texture coordinates of each sprite in the sprite sheet that data then gets put in a game object. I then loop through the array of game objects and draw each object with the model position being calculated relative to the position in the 2d array. Even I can tell that this strategy isn't good because it isn't scalable. Each time I want to use more sprites I have to add more numbers or completely change the x and y coordinates for each number if I want to use a completely different sprite sheet (ex. making a new level). I tried looking for tips online but all of the resources use this strategy since the number of sprites they use is small. Any help would be appreciated!

Advertisement

Hai, so you have outgrown the tutorial, great news I'd say ? well on your path to becoming a programmer.

The step you should make is to stop relying on tutorials at the Internet giving you pre-cooked solutions. Instead, get creative and innovative, find a novel solution by yourself!

If you look at the current system, what parts aren't doing their job, and what part do work as you want it? Next, assume you throw out the parts you don't like. So you're left with a file with numbers, describing the level background, and a system that reads those numbers, and then needs an xy coordinate from the sprite sheet. With both, it can paint the entire background.

Where do you want those xy numbers?? What other options do you have? How can you easily write those numbers, load them into the computer, and use them? Say, like the numbers of the background in a file, except you have a group of numbers rather than a single value?

Other option. Assuming your background sprites all have equal size, and your sprite sheet is just a grid of those sprites. Say you assign numbers to the sprites in rows (first the top row left to right, then the second row left to right, etc). If you have 4 sprites in a row, then the first row has sprites 1, 2, 3, and 4. The second row has sprites 5, 6, 7, and 8, etc. Since they all have the same size, could the computer compute the xy coordinate from the sprite number?

As you can see, I an avoiding giving you a ready-to-run solution. You have to learn to break out the box of the tutorial, invent a extension that works for you. I gave you some questions/suggestions to give you a start. Could it work? If not, why not? If it can, how can you program it?

There are many more paths to solve this problem, but finding, exploring, and judging them is your game. I can give you a hand by helping you to tackle problems, and perhaps make next steps.

Another trick is to use a 256 color (8 bit) bitmap to “draw” the level data. Each color from 0-255 represents a sprite, object or special event etc on the level map, and its x,y position in the bitmap represents the location in your game world. So if your sprites are 32 pixels, just multiply the x,y by 32 to go from level map to screen co-ords.

You can also use one pixel in your level map to represent a “cluster” of sprites, for example a repeating 4x4 rectangle of sprites (tree/hill/room etc) in your map.

@abish5 I personally opted for an XML document, which gets parsed by a level loader into individual tags, which in turn get passed as parameters for the runtime classes' constructors. That way if I want to add or remove attributes in the XML, I only have to update code in the corresponding class. The rest of the level loading pipeline doesn't look at or care what's in the tag.

When it comes time to save a game, I do the same thing in reverse. All the runtime classes have .toXml() functions which the level loader composes into an XML document and writes to disk.

None

This topic is closed to new replies.

Advertisement