This series of articles will teach Visual Basic through the development of a text-based game. Its intended audience is someone familiar with programming (object-oriented programming at that) but not familiar with the .NET Framework, so that type of person will benefit most from reading. With this little disclaimer aside, however, anyone is free to follow along.
Visual Basic (or Visual Basic .NET, or VB.NET, to be clear that it targets the .NET Framework) is one of several languages that can be used for .NET Framework development. It combines a BASIC-style syntax with the power and functionality of .NET, and it serves as a great alternative to the other major .NET language, C#, especially for people more familiar and comfortable with its syntax, perhaps as a result of coming from Visual Basic 6.0.
The .NET Framework
We'll start with the .NET Framework. (If you're impatient and want to hear about the project, though, feel free to skip ahead a few paragraphs first). What exactly is .NET? To be brief, it's an environment or framework that allows for the development and execution of Windows-based applications. .NET provides a large Framework Class Library allowing developers to accomplish many tasks without reinventing the wheel. In addition, the .NET Framework allows Web development through ASP.NET.
There are a few very interesting alternatives to the .NET Framework for other environments, such as Mono, sponsored by Novell. Check it out if you want to deploy your applications across multiple platforms.
The .NET Framework is also the target of numerous programming languages. The two biggest languages are C# and Visual Basic, and with the new Dynamic Language Runtime, implementations of Python and Ruby are available (IronPython and IronRuby). This allows for the accommodation of a wide range of developers.
Another significant feature is the ability to mix multiple languages within a single project without any extra effort. A library can be written in C# and then used in Visual Basic, for example. Of course, with this article, we'll be using Visual Basic, but the code could easily be ported to, for instance, C#, and the same effect could be achieved with no fundamental structural changes.
A few IDEs exist for .NET development, but the most popular by far is Visual Studio. The current version is Visual Studio 2008. There are several editions of Visual Studio, but you'll want to look into the Express Editions. The Express Editions are free, yet still very usable and very powerful. You'll want Visual Basic 2008 Express Edition:
Note that there are other Express Editions for different languages (Visual C# 2008 Express Edition, for example), and there is a Visual Web Developer 2008 Express Edition for Web development.
Now let's take a look at what we'll be creating in this series of articles. We'll be creating a role playing game with a text interface, similar to Rogue or NetHack, but, of course, much simpler (sorry, but NetHack, for example, has had a few decades to develop into what it is today). Here's a screenshot of NetHack in action:
The dungeon and its inhabitants, items, etc. are represented by various characters so that the game can be played in a standard terminal. The player is represented by the @ symbol and is in a room. The walls of the room can easily be made out, and each of the periods represents a regular floor tile. The “d” in the room represents a dog, and the “%” represents a corpse. Messages are displayed on a single line at the top of the screen, and information about the player can be found at the bottom.
The player is shown in a single room, but there are multiple rooms linked by corridors. The player progresses by going down staircases (“<” and “>” represent staircases). When the player moves, he uses a turn, and then the various creatures in the dungeon take their turns.
Fancy? No, but it does have its own special charm, and something similar to this is within reach. Besides, this is a language learning exercise. We'll be shooting for something similar to this, but with our own variations, and, as noted earlier, with significantly less features. In order to get a better idea of what we're creating here, you should go play a little bit of NetHack (which runs on multiple platforms) or something similar.
So, we need to create a player with a few attributes, such as strength and agility, that can be used for combat. The player also needs to have an inventory. Next, we need to create a dungeon for the player. Then, we need to populate the dungeon with monsters for the player to fight with and items for the player to pick up. All of this needs to be wrapped in an attractive and functional interface.
Now that you know the basics of the .NET Framework and understand where we're going, let's get started with Visual Basic. Open up Visual Studio and create a new project. Call it whatever you want, but make sure that you use the Console Application template—our game is, of course, console-based. The project will automatically open. The Console Application template creates a minimal project. Only one file, Module1.vb is created for you. This is where we'll start. The file's contents look something like this:
Module Module1
Sub Main()
EndSub
EndModule
Since Visual Basic, naturally, features a BASIC-style syntax, instead of using brackets as one would expect in a C-style language, plain words are used. Here, you can easily see that we have something called a “sub” inside of something called a “module.” The “module” begins with the keyword Module and ends with the keywords End Module. Similarly, the “sub” begins with the keyword Sub and ends with the keywords End Sub. That, of course, prompts the question, what exactly are subs and modules?
We'll start with the module. A module is akin to a static class in other languages, and it's what is used by default to contain the main “method” (we can't really use that language here, but we'll get to that in a few paragraphs) of a Visual Basic application. Everything is shared (“static” in other languages, meaning that the member is not specific to an instance of the class but, instead, belongs to the class itself) and is, by default, public. This is important to remember! Again, Visual Basic starts us off with a module in which to create our program, but we can create more modules in our program if we need to, and we could also wrap our program in a class, as is done in C#:
PublicClass Class1
PublicSharedSub Main()
EndSub
EndClass
This should look more familiar to many people, with the exception of the word “shared,” which was covered earlier. This will compile and has the potential to produce the same effect as with the module. (Note that in Visual Studio you must first go to Project->Properties and change the startup object.) This code snippet also demonstrates how classes are defined in Visual Basic.
Just because we can, however, doesn't mean that we should. Modules are infinitely more “VB-ish” and are what we're going to use here. The module of a new project is by default called Module1 and is contained within a file called Module1.vb. We can change this, however, to reflect the nature of our project. Change the filename to Game.vb. In Visual Studio, this will automatically change the module's name to Game (neat!):
So, now that we know what a module is, what's the “sub” thing? It stands for subroutine and is equivalent to a function with a void return type—that is, it returns no value and instead performs some sort of operation with a desirable side effect. The parenthesis, of course, can be filled with parameters, but we'll get to that later. It's part of a broader group of procedures, which includes functions as well. This is why you'll hear this particular procedure be called a Sub procedure.
This particular procedure, Main, is special. It's what is called when the program executes, so it's the heart of the program. In Visual Studio, press the green arrow in the toolbar, or press F5. This will start debugging, a process we're not interested in right now, but we are interested in running the program, which is what will happen. You may notice a console appear real fast and then disappear. This is our program executing. The console disappears so fast because, of course, our program does nothing at the moment. Let's fix this. Let's print out “Hello World” and then wait for a key to be pressed before exiting:
Sub Main()
System.Console.WriteLine("Hello World!")
System.Console.ReadKey()
EndSub
Now start the program up. The program will print “Hello World” in the console and then wait for a key to be pressed before terminating, as expected.
The above example shows two procedures being called. Nothing is strikingly odd about the two calls—they are done in the same manner as in other languages. What is worth nothing, however, is our use of the Framework Class Library here. In the example, we access the System namespace, which is where the base classes are stored. We can actually omit the namespace here, but I just wanted to point out its existence. The following code is equivalent:
Sub Main()
Console.WriteLine("Hello World!")
Console.ReadKey()
EndSub
In the System namespace, we access the Console type. This type provides shared (static) procedures that we can use to control the console, displaying information to the user and reading information from the user. We'll be using this type a lot since our application is console-based.
Finally, we access the procedures WriteLine and ReadKey. WriteLine does what its name implies: it simply writes a line of text out to the console. ReadKey reads a key press and returns information about the key pressed (which we have no use for and therefore do not store).
I'm afraid that's all I have room for in this article. Be sure to check back next week though. I'll be picking up where I left off, with more information about WriteLine, and move on to more complex matters. See you then!