Austin
Austin

Reputation: 109

Creating and saving maps with tiles in XNA

I'm trying to make a "dungeon crawler" type game demo, and this is my first time really making anything beyond Pong & Pac-Man clones. My big hang up right now is creating the actual level. I've gone through a tutorial on how to draw tiles onto the screen, but I can't find anything about where to go from there.

How do I go from one screen to making a larger dungeon? Any help is appreciated.

Upvotes: 6

Views: 4015

Answers (3)

AvrDragon
AvrDragon

Reputation: 7479

It will be much easier to design your map, if you implement save/load for tiled map format.

Upvotes: 1

Napoleon
Napoleon

Reputation: 1102

The PSK http://msdn.microsoft.com/en-us/library/dd254918(v=xnagamestudio.31).aspx is a demo created by Microsoft that uses an ASCII file to create the levels. There are also many demo's and tutorials and other things for it. It's the same approach that Jon described.

example:

.......
.....e.
xx...xx
..s....

Where a dot is empty space, s is the start location and e is the enemy. Because you know that every tile is (for example) 32x32 pixels you simple position the 's' at coordinate (2x32,3*32).

There are many many ways to handle this tough.

For making more than one screen you need to implement a camera class. What you probably want is a scrolling level. But I'd advise you to first learn to make a single screen from an array, ascii file, etc. before going any further.

Upvotes: 1

jgallant
jgallant

Reputation: 11273

You should consider starting off by using 2 dimensional arrays. In this manner, you can represent your data visually quite easily.

Start off with an initialization:

//2D array
int[,] array;

Some sample data:

array= new int[,]
        {
            {0, 2, 2, 0},
            {3, 0, 0, 3},
            {1, 1, 1, 1},
            {1, 0, 0, 0},
        };

Create yourself an enumeration, which will index each integer in your map:

enum Tiles
{
    Undefined = 0,
    Dirt = 1,
    Water = 2,
    Rock = 3
}

Then load your textures and what not by looking through your array one item at a time. Based on your texture size, you can easily draw your textures on screen as presented in your map:

for (int i = 0; i < array.Count; i++)
{
    for (int j = 0; j < array[0].Count; j++)  //assuming always 1 row
    {
       if (array[i][j] == (int)Tiles.Undefined) continue;

       Texture = GetTexture(array[i][j]);  //implement this

       spriteBatch.Draw(Texture, new Vector2(i * Texture.Width, j * Texture.Height), null, Color.White, 0, Origin, 1.0f, SpriteEffects.None, 0f);
    }
}

Upvotes: 6

Related Questions