Working with Classes and Properties for Game Development in VB.NET - Creating a Tile Class
(Page 4 of 4 )
The next thing we need to do is determine how to lay out the "tiles" (which are, remember, actually composed of single characters) for our maps. Since a map itself will be rectangular (though not all of the rectangle may be used, of course), the best way to represent it is through a two-dimensional array. Think of the map as a table of tiles. One dimension of the array contains the rows, and the other contains the columns. This is the most straightforward approach to it.
We need to have an array of something, though, and despite the tiles being represented as characters, an array of characters won't due because each tile has special properties, such as color or the ability to be walked on (as opposed to being a wall or some other obstruction). These properties will be best represented as part of a Tile class. Then, we can just create an array of Tile objects.
Create a new Tile class (inside Tile.vb) in Visual Studio, the same way we did with the Adventurer class:
Public Class Tile
End Class
We'll need fields and properties for the tile's character symbol, its foreground color, its background color and whether or not the user can pass over it. The symbol needs to be a Char, the two colors need to be of type ConsoleColor, and a Boolean can tell us whether or not the user can pass over the tile. Let's go ahead and create all of this at once (it's a bit lengthy, but it's very simple, and there's nothing new):
Private _symbol As Char
Private _foregroundColor As ConsoleColor
Private _backgroundColor As ConsoleColor
Private _passable As Boolean
Public Property Symbol() As Char
Get
Return _symbol
End Get
Set(ByVal value As Char)
_symbol = value
End Set
End Property
Public Property ForeGroundColor() As ConsoleColor
Get
Return _foregroundColor
End Get
Set(ByVal value As ConsoleColor)
_foregroundColor = value
End Set
End Property
Public Property BackgroundColor() As ConsoleColor
Get
Return _backgroundColor
End Get
Set(ByVal value As ConsoleColor)
_backgroundColor = value
End Set
End Property
Public Property Passable() As Boolean
Get
Return _passable
End Get
Set(ByVal value As Boolean)
_passable = value
End Set
End Property
As I said, it's lengthy, but we're just creating four fields and then creating four properties to access those fields. Now we need to create a constructor to set initial values for everything:
Public Sub New(ByVal symbol As Char, _
ByVal foregroundcolor As ConsoleColor, _
ByVal backgroundColor As ConsoleColor, _
ByVal passable As Boolean)
_symbol = symbol
_foregroundColor = foregroundcolor
_backgroundColor = backgroundColor
_passable = passable
End Sub
We won't be using the Tile class like this, however. Rather, we're going to subclass it in order to easily create different types of tiles. And that is where we will pick up next week!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |