First of all you will need to download the graphics pack here. These are just really basic examples of tiles you might find in a tile engine.
In Visual Studio create a new windows game and call it SimpleTileEngine. I like to remove all the comments in the Game1 class. I find it makes my code look messy.
For our tile engine we will use a 2 dimensional array of ints. This is the easiest way to represent a tile engine. While were at it we will also create a list of tile textures.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Texture2D> tiles; | |
int[,] tileMap; | |
int tileWidth = 48; | |
int tileHeight = 48; |
Next we need to initialize our variables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tiles = new List<Texture2D>(); | |
tiles.Add(Content.Load<Texture2D>("Sky")); | |
tiles.Add(Content.Load<Texture2D>("Grass")); | |
tiles.Add(Content.Load<Texture2D>("Rock")); | |
tileMap = new int[,]{ | |
{0, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0}, | |
{0, 0, 1, 0, 0}, | |
{1, 1, 2, 1, 1}, | |
{2, 2, 2, 2, 2}}; |
We don't need anything in the update method all our work from here is in the draw. In the draw we need to loop through each tile in the tile array to draw them. In a 2D array the Y value that you see in the code below is actually the row number. We get this number by calling tileMap.GetLength(0). The X is the index of a number in that row like a column. This is reason the x and y are mixed up in the line tiles[tileMap[y, x]]. tileMap[y, x] returns the number in the row y and column x which corresponds to the index of the texture in the textures array as explained above.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spriteBatch.Begin(); | |
for (int y = 0; y < tileMap.GetLength(0); y++) | |
{ | |
for (int x = 0; x < tileMap.GetLength(1); x++) | |
{ | |
spriteBatch.Draw( | |
tiles[tileMap[y, x]], | |
new Vector2(x * tileWidth, y * tileHeight), | |
Color.White); | |
} | |
} | |
spriteBatch.End(); |
After that were finished. Press F5 to run and you should see this.
You can increase the amount of tiles that are drawn by just increasing the size of the array just like below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tileMap = new int[,]{ | |
{0, 0, 0, 0, 0, 0, 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}, | |
{1, 1, 0, 0, 0, 1, 2, 1}, | |
{2, 2, 1, 0, 1, 2, 2, 2}, | |
{2, 2, 2, 1, 2, 2, 2, 2}, | |
{2, 2, 2, 2, 2, 2, 2, 2}}; |
![]() |
It should look bigger like this |
Download the finished solution here.
Were pretty much finished. I hope you found it enjoying. If you have any questions or spot an error let m know in the comments below.
This comment has been removed by the author.
ReplyDelete