Creating and Drawing a Game Map in VB.NET (FIXED PER REQUEST) - Creating the Entity Class
(Page 4 of 4 )
The map is missing something very important, though-the player. Fortunately, displaying the player is easy enough, but first we need to consider something else first. Yes, we'll have the player walking around the screen, but we'll also have other entities (monsters, etc.) moving around the screen as well. Here, we have some common behavior shared between players and other entities. Both groups have a location, both groups are represented by a symbol, and both groups have names associated with them. This is a great opportunity for inheritance to play a role. Let's create a class called Entity that represents all the entities on the map, including the player. Then, let's rework Adventurer to inherit from this class.
The Entity class will have fields and properties for a name, a location, a symbol, and a foreground color. Let's go ahead and create the class-it's a little lengthy:
Public Class Entity
Private _name As String
Private _symbol As Char
Private _color As ConsoleColor
Private _x As Integer
Private _y As Integer
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Symbol() As Char
Get
Return _symbol
End Get
Set(ByVal value As Char)
_symbol = value
End Set
End Property
Public Property Color() As ConsoleColor
Get
Return _color
End Get
Set(ByVal value As ConsoleColor)
_color = value
End Set
End Property
Public Property X() As Integer
Get
Return _x
End Get
Set(ByVal value As Integer)
_x = value
End Set
End Property
Public Property Y() As Integer
Get
Return _y
End Get
Set(ByVal value As Integer)
_y = value
End Set
End Property
End Class
We also need a constructor to assign initial values to the fields:
Public Sub New(ByVal name As String, ByVal symbol As Char, _
ByVal color As ConsoleColor, ByVal x As Integer, _
ByVal y As Integer)
_name = name
_symbol = symbol
_color = color
_x = x
_y = y
End Sub
The Entity class is long, but we can now drastically reduce the size of Adventurer, since the functionality we provided previously is now provided in Entity. All we need to do now is call the constructor. Replace the entire Adventurer class with this:
Public Class Adventurer
Inherits Entity
Public Sub New(ByVal name As String, ByVal x As Integer, _
ByVal y As Integer)
MyBase.New(name, "@", ConsoleColor.Cyan, x, y)
End Sub
End Class
There, that's shorter than before. However, since the constructor's parameters have changed, we need to modify Main a bit, where we create the Adventurer:
player = New Adventurer(name, 1, 1)
Now we can create a method that will draw not only the player, but every other entity as well. The method will accept an Entity object and will then draw it on the proper location on the screen:
Sub DrawEntity(ByVal toBeDrawn As Entity)
Console.SetCursorPosition(toBeDrawn.X, toBeDrawn.Y)
Console.ForegroundColor = toBeDrawn.Color
Console.Write(toBeDrawn.Symbol)
Console.ResetColor()
End Sub
The method is similar to DrawTile, except it specifies a parameter and sets the cursor to the proper location itself. In Main, after the call to DrawMap, call DrawEntity and pass player (since, after all, an Adventurer is an Entity):
DrawEntity(player)
This will draw the player on the screen. Run the program, and you should see the player in the top left-hand corner of the screen.
Now that we have drawing taken care of, it's time to move on to movement. The game is finally starting to take shape.
| 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. |