Creating and Drawing a Game Map in VB.NET (FIXED PER REQUEST) - Drawing the Map
(Page 3 of 4 )
Now we have a map. That didn't take much, did it? We need to draw it out to the screen now, one tile at a time. This will enable us to see what it looks like for the first time. To draw it out to the screen, we once again need two loops, but rather than going through the map column-by-column, we'll need to go through row-by-row. This way, we can just call Write for each tile, and we only have to manually adjust the cursor's position at the end of each row. Once again, we'll place this code in a separate method, so as not to clog up Main. The method will be called DrawMap:
Sub DrawMap()
For y As Integer = 0 To map.GetUpperBound(1)
Console.SetCursorPosition(0, y)
For x As Integer = 0 To map.GetUpperBound(0)
DrawTile(x, y)
Next
Next
End Sub
As noted, the outer loop goes by row, and the inner loop goes by column. At the start of each iteration of the outer loop, we set the cursor to the beginning of the appropriate row. As we draw the tiles, the cursor will automatically shift right. Notice how I've called a method called DrawTile. We'll be drawing tiles frequently, and so it's best to have a separate method for this. Inside of DrawTile, we apply our knowledge of console output:
Sub DrawTile(ByVal x As Integer, ByVal y As Integer)
Console.ForegroundColor = map(x, y).ForeGroundColor
Console.BackgroundColor = map(x, y).BackgroundColor
Console.Write(map(x, y).Symbol)
Console.ResetColor()
End Sub
We first have to set the colors. Then, we write out the tile's symbol and then clean up by changing back to the default color.
Now we can see the map in action. In Main, call DrawMap:
DrawMap()
It's also a good idea to call ReadKey temporarily. That way, the console won't close before we can see the map. Just remember to erase it afterward:
Console.ReadKey()
Run the program, and, after you enter your name, you should see the map we just created.
Next: Creating the Entity Class >>
More Visual Basic.NET Articles
More By Peyton McCullough