I'm attemptin to add multiple levels to my pong game. I'd like to simply exchange a few elements with each level, nothing crazy. Just the background texture, the color of the AI paddle (the one on the right side), and the music.

It seems that the best way to go about this is by utilizing the StreamReader to read and write the files from XML. If there is a better, or more efficient alternative way then I'm all for it. In looking over the XNA Starter Platformer Kit provided by MS it seems that they've done it in this manner as well. I'm perplexed by a few things, however, namely parts within the Level class which aren't commented.

/// <summary>
/// Iterates over every tile in the structure file and loads its
/// appearance and behavior. This method also validates that the
/// file is well-formed with a player start point, exit, etc.
/// </summary>
/// <param name="fileStream">
/// A stream containing the tile data.
/// </param>
{
// Load the level and ensure all of the lines are the same length.
int width;
List<string> lines = new List<string>();
{
width = line.Length;
while (line != null)
{
if (line.Length != width)
throw new Exception(String.Format("The length of line {0} is different from all preceeding lines.", lines.Count));
}
}


What does width = line.Length mean exactly? I mean I know how it reads the line, but what difference does it make if one line is longer than any of the others?

Finally, their levels are simply text files that look like this:


....................
....................
....................
....................
....................
....................
....................
.........GGG........
.........###........
....................
....GGG.......GGG...
....###.......###...
....................
.1................X.
####################

It can't be that easy..... Can it?

The reader wants to ensure that all your lines are the same width as the first line. So it avoids situations like:

....................
....................
....................
....................
.......................
....................
....................
.............GGG........
.........###........
....................
......GGG.......GGG...
....###.......###...
......................
.1................X.
####################


It's just a simple check to make sure everything is in order. Basically it wants to make sure that you created your "map" file properly. You don't really have to throw an exception, you could just ignore the extra characters, but there would probably be more code involved in that.

I assume you know what each character represents since you have the rest of the code.

• It's not required, but it's better to throw an exception right away so that you can catch the human error as soon as possible. It's commonly called Failing Fast – John McDonald Jun 6 '12 at 0:15
• Oh, I agree that it should be thrown. I'm just saying it's not necessarily a critical error. – MichaelHouse Jun 6 '12 at 0:26
• I figured that is what it was for. Thank you gentlemen. – Dave Voyles Jun 6 '12 at 0:55

There's no reason it couldn't be that easy. It looks like they encoded the level in ASCII graphics. .'s represent empty space, G's represent something (perhaps gold coins?), #'s represent platforms, 1 is the player, and X is the player's destination. You couldn't have a level with an uneven set of widths because it would cause some parts to be drawn off-screen.

• Yes, you are correct, those characters do represent that. And thank you for your response! – Dave Voyles Jun 6 '12 at 0:56