The Basics of VB.NET Through Text Game Development - Working with the Console
(Page 2 of 4 )
So, now we know how to write a line of text to the console, but this, of course, won't be enough for a project like ours. Recall the nature of the game: we have a dungeon made up of tiles, creatures, and items, all of which are really characters. We need to be able to draw every one of these, and we need to be able to redraw them if they move or are covered up or uncovered. For example, if the player walks from one space to another space, then we need to erase the tile he steps on, replacing it with the image of the player, and we need to redraw the tile that he stepped off of, erasing the old image of the player. Moreover, things may need to be represented in different colors, not just the default gray. Clearly, WriteLine isn't going to accomplish all of this, and, in fact, our use of it in the game will be limited.
WriteLine writes out a string of text and then moves to the next line. In most cases, this isn't what we want at all. We may only need to write one character at a specific location on the screen. To do this, we need a very similar procedure. Write. Write operates much like WriteLine, except it doesn't move to a new line.
Earlier, we wrote “Hello World” out to the screen like this:
Console.WriteLine("Hello World.")
Though it isn't terribly obvious unless you pay attention to the blinking cursor, WriteLine moved to the next line. If we were to write some more text to the screen, it would be more noticeable, since the new text would start on the next line.
However, a combination (of no real-world value) of Write and WriteLine would have achieved the exact same effect:
Console.Write("Hello ")
Console.WriteLine("World.")
First, we make a call to Write, writing “Hello “ (with a space) out to the screen. We then write “World.” out to the screen with WriteLine. Since Write doesn't add a new line, the call to WriteLine continues writing on the same line.
We can break it up even further, into three calls, while still achieving the same effect:
Console.Write("Hello ")
Console.Write("World.")
Console.WriteLine()
While the above sample has no practical purpose, it does illustrate something else we should know: WriteLine can be called with no arguments. If this happens, it simply moves the cursor to the next line. This is useful in some situations.
But, as noted earlier, we can't adopt a line-by-line approach to creating our game's interface. Rather than writing one line and then moving onto another in a linear fashion, we need to be able to update individual characters. So, how does Write allow us to do this? Alone, it doesn't, but when used in conjunction with another procedure, it allows us to write a character in any location in the console. This is exactly what we need.
Next: Working with the Console continued >>
More Visual Basic.NET Articles
More By Peyton McCullough