Movement and Player Statistics in a VB.NET Text-Based Game - Adding Movement
(Page 2 of 4 )
Now it's time to add movement. We need to first set up a function, TryMove, that will try to move an Entity (not necessarily the player) a certain number of spaces. This function will accept the entity to be moved, the change in the entity's X value, and the change in the entity's Y value. It will then check to see if the move is valid. If it is valid, then the movement will be made, and True will be returned. If the movement cannot be made, then False will be returned. We need TryMove to return these values because we don't want to end the player's turn if he tries to make an invalid move. Rather, we want him to be able to try again before the other entities get their turns.
So far, we've never actually worked with functions before. They're very simple, however. A Func, as it is termed in the language, is similar to a Sub, only we must return a value, and we can explicitly specify what type that value will be.
We're also going to need to create yet another method called RedrawTile. DrawTile will draw a tile, but since it was originally built to be used to draw the entire map, it assumes that the cursor is already in the correct position. RedrawTile will manually set the cursor in the proper position and then call DrawTile. First, though, here is TryMove, which calls RedrawTile:
Function TryMove(ByVal toBeMoved As Entity, ByVal xChange As Integer, ByVal ychange As Integer) As Boolean
Dim x As Integer = toBeMoved.X + xChange
Dim y As Integer = toBeMoved.Y + ychange
If map(x, y).Passable = True Then
RedrawTile(toBeMoved.X, toBeMoved.Y)
toBeMoved.X = x
toBeMoved.Y = y
DrawEntity(toBeMoved)
Return True
End If
Return False
End Function
The function accepts, as I said before, the change in the entity's X value and the change in the entity's Y value. So, if the entity is trying to move right, then the change in the X value is 1, and the change in the Y value is 0. If the entity is trying to move up, then the change in the X value is 0, and the change in the Y value is -1. The changes are added to the player's current location to determine the destination tile. The tile is then checked to see if the player can walk on it, and if the player can, then the player's old location is redrawn, and the player is drawn on the new location.
Now let's write RedrawTile, which is very simple, as was already noted:
Sub RedrawTile(ByVal x As Integer, ByVal y As Integer)
Console.SetCursorPosition(x, y)
DrawTile(x, y)
End Sub
Great. Now the methods required for movement are in place, and we can wire them up to our loop. Recall how I said that the player's turn will only be used up if the player has made a move. This is determined by what TryMove returned. We're going to need a Boolean variable to store the value returned by TryMove. Add this definition before the Select statement:
Dim playerMoved As Boolean = False
We assume that the player has not made a move. If the player does indeed made a move, then we can change this. This also eliminates the need for the Case Else block, so you can go ahead and delete it. It will be replaced by a conditional that checks to see whether or not the player has moved. If the player has not moved, then we'll need to immediately move to the next iteration, just like we did in the Case Else block. Put the conditional below End Select:
If Not playerMoved Then
Continue While
End If
Now we can check for movement keys and respond accordingly. We'll use the numpad for movement. Here's what the entire Select block should now look like:
Select Case input.Key
Case ConsoleKey.Escape
Exit While
Case ConsoleKey.NumPad8
playerMoved = TryMove(player, 0, -1)
Case ConsoleKey.NumPad2
playerMoved = TryMove(player, 0, 1)
Case ConsoleKey.NumPad4
playerMoved = TryMove(player, -1, 0)
Case ConsoleKey.NumPad6
playerMoved = TryMove(player, 1, 0)
End Select
Above, we check for movement keys and then call TryMove, passing the appropriate values. The result of the move is stored in playerMoved. Run the program and press the directional keys on the numpad. The player should now move around the map.
Next: Player Statistics >>
More Visual Basic.NET Articles
More By Peyton McCullough