Draw a basic 2d Grid


I’m looking to experiment with some basic AI techniques such as pathfinding and FSA’s.

I’d therefore like some good C# code samples that allow me to draw a 2d grid of tiles that show either some terrain or where a game character is standing. My current thinking is that I can pass the grid to each algorithm I am attempting to simulate so that the algorithm can update or query the grid as necessary.

Any suggestions appreciated.


If its for experimenting and developing algorithm you shouldn’t care that much about performance or clean code, just store your data in a 2d matrix and to draw the grid itself create a .png file of a single grid tile and just draw a grid from from it. you can make the texture white and play with the color to show different info.

eg something like:

for (int i = 0; i < gridSize; ++i)
     for (int j = 0; j < gridSize; ++j)
           _spriteBatch.Draw(tileTexture, new Rectangle(i * tileSize, j * tileSize, tileSize, tileSize), color);

PS. if you’re looking for pathfinding algorithm you can take a look at this: https://github.com/RonenNess/UnityUtils/tree/master/Controls/PathFinding/2dTileBasedPathFinding

Its for Unity but since its C# it should be super easy to use this with MG as well.