Movement and Player Statistics in a VB.NET Text-Based Game
(Page 1 of 4 )
Now that we've drawn the map and the player on the screen, we need to work on movement. Movement involves a number of things. We'll also need to give our player some statistics, such as health, attack, and defense. Keep reading for the lowdown as the text game we're creating to learn VB.Net really starts taking shape.
Accepting User Input
First, we need to allow the user to press a key. Second, we need to check to see if this movement is valid. For example, we don't want the player to walk on top of a wall. Checking to see if the movement is valid only involves checking the Passable property of the destination tile. Third, we need to actually make the movement. This involves drawing the player on the new tile, but it also involves erasing the player on the old tile.
Finally, this all needs to be contained within a loop. After all, we want the user to be able to continue making movements, and each time, the game should go through the same steps to respond to the movement.
The first thing we need to do is set up the loop. For now, an infinite While loop will work, but later we'll want to check for certain situations—such as when the player is dead. In this loop, we'll want to accept a keypress from the user. The keypress will be stored in a variable, so we'll need to define this variable. Also, we need to make sure that the cursor is not visible, and that the pressed key is not displayed in the console. Put the game loop at the bottom of Main:
Console.CursorVisible = False
Dim input As ConsoleKeyInfo
While True
input = Console.ReadKey(True)
End While
When you run the game, there will now be no cursor visible, and the console will not display anything you type (recall that this is accomplished by passing True to ReadKey). Now that we have the key stored, we need to check it against valid inputs. If the input is valid, then we need to perform the associated action. Otherwise, we need to go back and let the user try again. The checking will be done in a Select statement. Right now, let's put in a quit action, which will be escape. Pressing this key will exit the game immediately. Put this at the bottom of the loop:
Select Case input.Key
Case ConsoleKey.Escape
Exit While
Case Else
Continue While
End Select
Notice how, if the user has pressed the escape key, we exit the While loop. Otherwise, we use the Continue statement. The Continue statement immediately moves to the next iteration of the loop. In this case, we use it to allow the user to press another key.
Next: Adding Movement >>
More Visual Basic.NET Articles
More By Peyton McCullough