Hey guys here is a quick update on my platformer game. I've been able to add in some decent player physics, modeled after the Platformer Starter Kit on the Xna website. I've also spent most of my time on the editor lately, getting it up to speed with my engine and designing it in such a way that I will be able to create platformer games much quicker than just hard-coding in the levels for this one game.
Thanks for taking the time to check up on my game. I'm really looking forward to developing the engine and editor even more. One of the things I'm in the process of adding is a dedicated object layer for the maps. Right now, the only thing on that object layer (which is loaded in from a file by the way) is the player. Most likely, the next thing I will add to the layer is an exit tile, which will load up the next level (tile map in my case). This, I feel, will make the game start to feel more like an actual game than just a level demo.
Stay tuned for more updates!
Showing posts with label tile map. Show all posts
Showing posts with label tile map. Show all posts
Sunday, February 5, 2012
Xna Platformer Update
Labels:
2d,
C#,
demo,
game development,
platformer,
player,
tile engine,
tile map,
update,
xna,
XNA Game Studio
Thursday, February 2, 2012
Loading Objects from a File
Hey guys, so I just finished getting object loading to work in my platformer game and I thought I would share some code with you and explain my technique. I realize there are probably a hundred ways to do this and do it better, but this is just my first iteration for this method. As the game progresses I fully expect to go back and make this loading code more efficient.
This method is based off of Nick Gravelyn's Tile Engine series where he loads in basic Tile Layers for his map. I take his method and expand upon it to include a dedicated layer of the map for objects (i.e. players, npcs, items, chests, portals, etc). I'll show you the code first and then explain it.
The first thing we do here is declare our List<List<int>> for our tempLayout of the map layer. This will allow us to read in each cell of the map from our file. I also declare a string for the player's Texture because the constructor for the player requires a Texture2D and I can't access objectLayer.player until after objectLayer has been instantiated. After the playerTexture, we declare that we're using the StreamReader class and begin reading in our file.
First off inside the while loop we read the first line of the file with string line = reader.ReadLine().Trim();. This reads in the first line of the ObjectLayer file. Here is that file for reference:
As you can see, the first line is "[Player]". This sets the "readingPlayer" variable to true and moves to the "if (readingPlayer)" section of the loop. We then simply read in the path to the player's texture file. Next we move onto reading in the Layout of the map.
This section is very similar to Nick's method, except that after we read in the map to the tempLayout list of list<int>, we loop through the map to do two things. The first is to set each cell index, and the second to run a switch statement on each cell, which at this point only recognizes and looks for a '1' (the player starting cell). Once we have found the player starting cell, we instantiate the objectLayer's player variable and set it's position to that of the cell where it was located in the objectLayer's map.
We use this method in our game1.cs file where we call:
This line of code is identical to the method used in Nick's Tile Engine, except it is a type ObjectLayer instead of TileLayer. I hope this has been fairly easy to understand, but I understand if it is a lot of code to take in. If you are confused, please read over the code a few times and really ask yourself what each piece is doing. As always, I am willing to answer basic questions about this code and will try to help you out as best I can. Please leave me a comment if you have a question you simply cannot find the answer to.
Until next time!
This method is based off of Nick Gravelyn's Tile Engine series where he loads in basic Tile Layers for his map. I take his method and expand upon it to include a dedicated layer of the map for objects (i.e. players, npcs, items, chests, portals, etc). I'll show you the code first and then explain it.
private static ObjectLayer ProcessFile(ContentManager content, string fileName)
{
ObjectLayer objectLayer;
List<List<int>> tempLayout = new List<List<int>>();
string playerTexture = null;
using (StreamReader reader = new StreamReader(fileName))
{
bool readingLayout = false;
bool readingPlayer = false;
while (!reader.EndOfStream)
{
string line = reader.ReadLine().Trim();
if (string.IsNullOrEmpty(line))
continue;
if (line.Contains("[Player]"))
{
readingPlayer = true;
readingLayout = false;
}
if (line.Contains("[Layout]"))
{
readingLayout = true;
readingPlayer = false;
}
else if (readingPlayer)
{
playerTexture = reader.ReadLine();
}
else if (readingLayout)
{
List<int> row = new List<int>();
string[] cells = line.Split(' ');
foreach (string c in cells)
{
if (!string.IsNullOrEmpty(c))
row.Add(int.Parse(c));
}
tempLayout.Add(row);
}
}
}
int width = tempLayout[0].Count;
int height = tempLayout.Count;
objectLayer = new ObjectLayer(width, height);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
objectLayer.SetCellIndex(x, y, tempLayout[y][x]);
switch (objectLayer.GetCellIndex(x, y))
{
case 1:
objectLayer.player = new Player(content.Load<Texture2D>(playerTexture));
objectLayer.player.Position = new Vector2(x * Engine.TileWidth,
y * Engine.TileHeight);
objectLayer.player.OriginOffset = new Vector2(32, 64); // NEED TO NOT HARDCODE THESE VALUES
break;
case 0:
break;
}
}
}
return objectLayer;
}
The first thing we do here is declare our List<List<int>> for our tempLayout of the map layer. This will allow us to read in each cell of the map from our file. I also declare a string for the player's Texture because the constructor for the player requires a Texture2D and I can't access objectLayer.player until after objectLayer has been instantiated. After the playerTexture, we declare that we're using the StreamReader class and begin reading in our file.
First off inside the while loop we read the first line of the file with string line = reader.ReadLine().Trim();. This reads in the first line of the ObjectLayer file. Here is that file for reference:
[Player]
Sprites/playerSheet
[Layout]
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
As you can see, the first line is "[Player]". This sets the "readingPlayer" variable to true and moves to the "if (readingPlayer)" section of the loop. We then simply read in the path to the player's texture file. Next we move onto reading in the Layout of the map.
This section is very similar to Nick's method, except that after we read in the map to the tempLayout list of list<int>, we loop through the map to do two things. The first is to set each cell index, and the second to run a switch statement on each cell, which at this point only recognizes and looks for a '1' (the player starting cell). Once we have found the player starting cell, we instantiate the objectLayer's player variable and set it's position to that of the cell where it was located in the objectLayer's map.
We use this method in our game1.cs file where we call:
tileMap.ObjectLayer = ObjectLayer.FromFile(Content, "Content/Layers/Objects.olayer");
This line of code is identical to the method used in Nick's Tile Engine, except it is a type ObjectLayer instead of TileLayer. I hope this has been fairly easy to understand, but I understand if it is a lot of code to take in. If you are confused, please read over the code a few times and really ask yourself what each piece is doing. As always, I am willing to answer basic questions about this code and will try to help you out as best I can. Please leave me a comment if you have a question you simply cannot find the answer to.
Until next time!
Labels:
2d,
C#,
code,
game development,
how,
nick gravelyn,
platformer,
player,
programming,
read a file,
StreamReader,
tile map
Saturday, January 28, 2012
The Progress of My Platformer
Hey guys, it's been a few days and I thought I would update you on my current project, a classic platformer modeled after that oh so famous plumber we've all heard about before. Again, I'm back to using Xna for this project. For me, it's the simplest and quickest way to make a game the way I want to...through programming. I thought I would show you some video of the game so far. I'm also making an editor for the engine. Both of these started with Nick Gravelyn's Tile Engine series and have since branched off, as he made his engine for an rpg and mine is for a platformer.
On to the good stuff!
Here's a quick demo of the "game" so far (this is more of a tech demo than anything ;) )
I've done quite a few things with this game so far and I plan on going over some specifics with you soon, but for now I leave you with the flashy eye-candy that you've just seen. If you would like for me to cover something specifically that I'm doing in this game, please let me know in the comments. I like writing about game making and I like helping you guys out when I can.
That's all for now!
On to the good stuff!
Here's a quick demo of the "game" so far (this is more of a tech demo than anything ;) )
I've done quite a few things with this game so far and I plan on going over some specifics with you soon, but for now I leave you with the flashy eye-candy that you've just seen. If you would like for me to cover something specifically that I'm doing in this game, please let me know in the comments. I like writing about game making and I like helping you guys out when I can.
That's all for now!
Labels:
2d,
C#,
demo,
game development,
nick gravelyn,
tile map,
tiles,
update,
XNA Game Studio
Thursday, January 5, 2012
Revisiting Nick's Tile Engine Series
Hello and welcome back to this revamped look at game development. The very first thing that I believe every old school rpg needs is a virtual world in which to exist. This world needs to be self-contained, but not flawless. The first iteration of this world need not have any bells or whistles and only needs to display (in my case) a map of tiles that make it look like an actual world.
This is where Nick Gravelyn's old Tile Engine Series comes into play. I'm going to be going through his tutorial series, modifying where needed to suite my own designs, at least in the first parts of my rpg. I'll try to comment where needed.
The first thing that we need in order to display our tiles is a list of textures for those tiles. We do this by loading in a list of textures:
These lines load individual texture images into a list of Texture2D textures. So far, we are following along with Nick's tutorial exactly.
After we have our list of textures we are free to create a map:
Using this map we only have to iterate through the 2D array of 1's and 0's to draw our map:
The code here sets the width of the tile map to the second dimension of the array, while the height is the first dimension. We then grab the number at each spot in the array and draw the corresponding image indexed to the value of the spot on the 'map' (array). For my map, tileWidth and tileHeight are set to 64.
This is a very basic way of drawing a map in Xna. There is much room for improvement/development and we will be taking our time getting the features of this game fleshed out in order to ensure that this game is fun to play and fun to develop. I appreciate you reading this and welcome any comments, suggestions, and questions gladly.
This is where Nick Gravelyn's old Tile Engine Series comes into play. I'm going to be going through his tutorial series, modifying where needed to suite my own designs, at least in the first parts of my rpg. I'll try to comment where needed.
The first thing that we need in order to display our tiles is a list of textures for those tiles. We do this by loading in a list of textures:
texture = Content.Load<Texture2D>("Tiles/grass1");
textureList.Add(texture);
texture = Content.Load<Texture2D>("Tiles/dirt1");
textureList.Add(texture);
These lines load individual texture images into a list of Texture2D textures. So far, we are following along with Nick's tutorial exactly.
After we have our list of textures we are free to create a map:
int [,] tileMap = new int[,]{
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,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 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
};
Using this map we only have to iterate through the 2D array of 1's and 0's to draw our map:
int tileMapWidth = tileMap[1].GetLength();
int tileMapHeight = tileMap[0].GetLength();
for (int x = 0; x < tileMapWidth; x++) {
for (int y = 0; y < tileMapHeight; y++) {
int textureIndex = tileMap[y, x];
Texture2D texture = textureList[textureIndex];
spriteBatch.Draw(
texture,
new Rectangle(
x * tileWidth,
y * tileHeight,
tileWidth,
tileHeight),
Color.White);
}
}
The code here sets the width of the tile map to the second dimension of the array, while the height is the first dimension. We then grab the number at each spot in the array and draw the corresponding image indexed to the value of the spot on the 'map' (array). For my map, tileWidth and tileHeight are set to 64.
This is a very basic way of drawing a map in Xna. There is much room for improvement/development and we will be taking our time getting the features of this game fleshed out in order to ensure that this game is fun to play and fun to develop. I appreciate you reading this and welcome any comments, suggestions, and questions gladly.
Labels:
C#,
nick gravelyn,
rpg,
texture,
tile,
tile engine,
tile map,
tiles,
xna
Subscribe to:
Posts (Atom)
