2
\$\begingroup\$

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>
    private void LoadTiles(Stream fileStream)
    {
        // Load the level and ensure all of the lines are the same length.
        int width;
        List<string> lines = new List<string>();
        using (StreamReader reader = new StreamReader(fileStream))
        {
            string line = reader.ReadLine();
            width = line.Length;
            while (line != null)
            {
                lines.Add(line);
                if (line.Length != width)
                    throw new Exception(String.Format("The length of line {0} is different from all preceeding lines.", lines.Count));
                line = reader.ReadLine();
            }
        }

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?

\$\endgroup\$

2 Answers 2

6
\$\begingroup\$

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ Commented Jun 6, 2012 at 0:15
  • \$\begingroup\$ Oh, I agree that it should be thrown. I'm just saying it's not necessarily a critical error. \$\endgroup\$
    – House
    Commented Jun 6, 2012 at 0:26
  • \$\begingroup\$ I figured that is what it was for. Thank you gentlemen. \$\endgroup\$ Commented Jun 6, 2012 at 0:55
2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Yes, you are correct, those characters do represent that. And thank you for your response! \$\endgroup\$ Commented Jun 6, 2012 at 0:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .