Creating and Drawing a Game Map in VB.NET (FIXED PER REQUEST)
(Page 1 of 4 )
This is the seventh part of a nine-part article series that teaches you Visual Basic.Net through the development of a text-based PC game similar to Rogue or Nethack. In this installment, you'll learn how to build the map, starting with the creation of some basic tiles. We'll be dealing with arrays, entity classes and more. Let's get started.
Creating Some Basic Tiles Through Subclassing
Before we start working with the map as a whole, let's create a few basic tiles to work with by subclassing the Tile class. The first tile we'll create is a basic floor tile. It won't be flashy at all. It will have a gray foreground and a black background, it will be represented by a period, and the user will, of course, be able to walk over it. Create a new class in Visual Studio called BasicTile (in BasicTile.vb):
Public Class BasicTile
End Class
The first thing we need to do is set our class to inherit from the Tile class. This is very easy. All we have to do is use the Inherits keyword and then specify the parent class. As is characteristic of BASIC languages, the result is very straightforward and readable:
Public Class BasicTile
Inherits Tile
End Class
Notice how the Inherits statement is put on a new line. This is required.
Immediately, Visual Studio should complain that we have to create a constructor because the parent class does not have a parameterless constructor which can be called by default. This is easy to do. Let's create a parameterless constructor that sets up everything. In this constructor, we will have to call Tile's constructor, which will set the fields to the values we specify. Let's go ahead and create it:
Public Sub New()
MyBase.New(".", ConsoleColor.Gray, ConsoleColor.Black, True)
End Sub
One of the first things you should notice is the use of the MyBase keyword. The MyBase keyword simply provides access to the parent class. Above, we use MyBase to call the parent class's constructor. Also, notice how, when working with characters, we do not use single quotes as we might in other languages. Instead, we just use double quotes, which other languages often reserve for strings. Besides, the single quote mark in Visual Basic is taken to be a comment.
Now we have a basic floor tile. Next is a wall tile. It will be represented by the number sign, will have a gray foreground and a black background, and it will, of course, not be passable by the player. Create a class called WallTile (in WallTile.vb). The process is the same as before, except the name of the class is, obviously, different, and the values we pass to Tile's constructor are different:
Public Class WallTile
Inherits Tile
Public Sub New()
MyBase.New("#", ConsoleColor.Gray, ConsoleColor.Black, False)
End Sub
End Class
The final basic tile we'll need is a blank tile. This tile will have a blank space associated with it, and it will be used to fill in if the physical map isn't as big as the available size. Make a class called BlankTile:
Public Class BlankTile
Inherits Tile
Public Sub New()
MyBase.New(" ", ConsoleColor.Black, ConsoleColor.Black, False)
End Sub
End Class
Next: Creating the Map Array and a Basic Map >>
More Visual Basic.NET Articles
More By Peyton McCullough