Creating and Drawing a Game Map in VB.NET (FIXED PER REQUEST) - Creating the Map Array and a Basic Map
(Page 2 of 4 )
Great. We now have three simple tiles to work with, and we can begin to work with the map array. As stated before, the map will be represented by a two-dimensional array. In this way, we can treat the array as a coordinate system of sorts. One dimension's index will represent the x-coordinate of the tile, and the other dimension's index will represent the y-coordinate of the tile. This works out to be nice and neat.
As with player, our map array will be a field of the Game module. This is the easiest way to work with it. Let's go ahead and create a field for the map. Let's make the map 60x20. So, the upper bound for the x-coordinate will be 59, and the upper bound for the y-coordinate will be 19:
Dim map(59, 19) As Tile
Next, we need a map. Ordinarily, we may want to automatically generate a map, or else read an existing map from a file. However, let's keep this basic for now and create a map by hand. More ambitious projects will have to wait until later. Let's start with something simple. We'll make a large square room occupying the entire available space. We can create a room like this with a loop. We'll loop through every space in the map. This will require one loop for the x-coordinates and another embedded within that for the y-coordinates. If we're at the edge of the map, we'll place a WallTile. Otherwise, we need a BlankTile. Let's put this in a method called CreateBasicMap:
Sub CreateBasicMap()
' Get the x- and y- coordinates for the right and bottom
' edges of the map
Dim xLimit As Integer = map.GetUpperBound(0)
Dim yLimit As Integer = map.GetUpperBound(1)
For x As Integer = 0 To xLimit
For y As Integer = 0 To yLimit
If x = 0 Or x = xLimit Or y = 0 Or y = yLimit Then
map(x, y) = New WallTile()
Else
map(x, y) = New BasicTile()
End If
Next
Next
End Sub
We use a For loop because we need to be able to go tile-by-tile and also check to see if we're at an edge. The outer loop iterates over the x values, and the inner loop iterates over the y values. So, the method essentially creates the map column-by-column. Also, notice the use of the Or operator. We check multiple conditions, any of which, if true, indicate that we're at an edge of the map. Go ahead and call CreateBasicMap in Main:
CreateBasicMap()
Next: Drawing the Map >>
More Visual Basic.NET Articles
More By Peyton McCullough