Entity Creation and Messaging in a VB.NET Text-Based Game
(Page 1 of 4 )
The player can't be alone in the world. Obviously, he's going to have to have some non-human company. Let's put some non-human characters into the game. In this ninth and final part of our series teaching VB.NET through the creation of a text-based game, we're also going to create a messaging system.
Drawing Other Entities
The non-human characters will be derived from Entity, just as the player character is, because they're going to share some of the same properties. They are going to have names, heatlh, attack, defense, a character symbol, a position on the map, and so forth.
Let's go ahead and implement an entity who will pace back and forth. This entity is completely pointless as far as gameplay is concerned, but implementing him will help us with non-human entity functionality because we have to deal with drawing him and moving him around.
Create a class called PacingManEntity (in the file PacingManEntity.vb) to house our new entity:
Public Class PacingManEntity
End Class
The first thing we need to do with this new class is inherit from Entity and then set up an appropriate constructor, which will call Entity's constructor and pass the appropriate values:
Public Class PacingManEntity
Inherits Entity
Sub New(ByVal x As Integer, ByVal y As Integer)
MyBase.New("Pacing Man", "P", ConsoleColor.Red, _
x, y, 10, 1, 1)
End Sub
End Class
As you can see above, his name is Pacing Man, and his symbol is a red “P.” His starting location is, of course, not hard-coded into the class. Rather, the starting location is accepted as a parameter in the constructor.
We have an entity, and now we need to draw him. Drawing is done the exact same way as with the player—through DrawEntity. However, we need a way to draw all of the non-player entities at once. The best way to do this is to store the entities in a collection and then create a new method that will loop through the collection and call DrawEntity for each entity. Plus, with a collection, we can keep track of all the entities.
We'll use a List(Of T) to store the entities because that is what's most appropriate for the situation. Create the collection as a field of the Game module, right under the player and map definitions:
Dim entities As New List(Of Entity)
Notice how we also instantiate the collection.
Next, we need to add a PacingManEntity to the collection. Put the following line at about the location that we create the Adventurer object (the line where we create the PacingMan and add it to the entities collection needs to be before the line where we draw the entities):
entities.Add(New PacingManEntity(10, 10))
Now we need a method that will loop through entities and draw everything. We'll call this procedure DrawEntities, and it will use a simple For Each loop to get the job done:
Sub DrawEntities()
For Each toBeDrawn As Entity In entities
DrawEntity(toBeDrawn)
Next
End Sub
Next, simply call the new procedure at around the location that the player is drawn through DrawEntity:
DrawEntities()
Run the program, and you should see a PacingManEntity somewhere off to the bottom right of the player.
Next: Implementing Movement >>
More Visual Basic.NET Articles
More By Peyton McCullough