C#–Text Adventure–Part 1

Wanting to learn C#? I figure this would make a great set of tutorials for beginners. What we’re going to be doing is using the Windows Forms feature to create a text adventure all using Visual C# 2010 straight out of the box. I will try to clearly explain everything.

First off, lets create a new project:


I’ve called mine “Pulse” after a text adventure I started in DBP. It’ll need to be a ‘Windows Form Application.

Creating Forms

You’ll end up with a form and it’ll look something like this:


So what we want to do first is just design our little ‘form’ or ‘window’ for our little game. Click on the form and if the ‘Properties’ window in the right panel, does not show, click this button:


Inside of the properties, we’ll want to change the text in the form’s title from ‘Form1’ to whatever you want to call it.


On the left pane, you’ll notice a button labelled, ‘tool box’, we’ll want to use this to get the components to build our interface. It’s pretty basic.

Create a TextBox. In the properties, make sure it’s multiline.


Scale the box and in the properties make sure it anchors Top, left, right and bottom. The anchor will mean that when the user resizes the window, the box will adjust.


Rename the textbox to ‘Body’. And make ‘ReadOnly’ True. (All in the properties). This text box will contain all of the text for the game.

Add another textBox, we only need to anchor this left, right and bottom. Rename it ‘command’. No other properties need to be changed. This text box is where the user will type in a command.


Now add a button with the text ‘Confirm’ and anchor it bottom right. Also, rename it ‘confirm’.


That’s all the form design you need to do. Now double click on your new button. It’ll take you into ‘Form1.cs’. The reason I said to double click on the button instead of opening ‘Form1.cs’ from the explorer is because by double clicking on it, C# will generate some code which has conditions should the user click on the button. We won’t worry about that yet.

The next bit will look confusing. It’s the actual code of the program.


Introduction to Programming


Think of programming at talking to your computer and you’re telling it to do things for you. Computers are very literal and mathematically minded. This means you have to spell everything out for them and mostly in mathematical equations. C# actually cuts out a lot of the work for you, in that you don’t quite have to go into depth explaining everything to the computer. For example, those forms we’ve designed? We didn’t actually need to code any of that.

A programming language is just that, a language. If we want to give a computer some instructions to tell it what to do, it helps to be able to refer to certain objects. So, you might say, “Pick up that can of Dr Pepper”. For a computer to understand what a ‘can of Dr Pepper’ is, we need to be able to define that object. Now we have 2 choices here: use objects that as already define or create our own. You see at the top of the code there’s “using” followed by a label. What that is essentially doing is saying to the computer, “use these definitions”. These definitions all point to various system commands and objects. Whilst “PickUp” is not a command and “DrPepper” is not an object, there’s a lot we can do.

It’ll make more sense in demonstration. But we’re not going to have to worry as much about ‘System’ ourselves. Because the commands and objects we’re mainly using will be made by ourselves.

Namespaces and Classes

Think of these like containers. Going with the ‘object’ analogy. You might think of the namespace as the world’, or heck to simplify things, a room. Inside of that room are lots of objects like your hi-fi system, your TV, your cooker, your books, your sink and so on. These are classes. You could even think of classes as rooms inside of a house. For a visual representation:


So like a TV your TV class would have certain properties you can give it – your TV might be a certain colour, size or shape, it might be of a certain brand or of a certain model. These are all ‘variables’, like: tv.size or tv.colour or tv.make. TVs also have different functions. In C# we call these ‘methods’. So you might have tv.turnOn(), tv.turnOff(), tv.nextChannel(), tv.GotoChannel(ChannelNumber) and so on

In code, you’ll notice the curly braces { }, these just represent the boundaries of the container, so everything within ‘class TV’ will be inside of those curly braces. So our ‘namespace’ in the game itself, all of our objects are contained within the game. Then we have a class called ‘Form1’, which is our form object. All of the pieces your added to your form as also objects, though you don’t have to define them yourself, as C# has taken care of that for you.

So, to demonstrate what the hell I’m on about, I’ll allow you to code a line. Everything that’s happening will be inside of the ‘Form1’ partial class, but the good news is, we can communicate to all sorts of other classes. Inside of the class is a method also called ‘Form1’, this is where the computer will read our logic. Anything we want to tell the computer to do will be inside of here, even if we’re telling it to look at a different class and read a different method.

For now, just add this:


Remember the textbox we renamed ‘Body’? It is also a class. One of its properties (which you might have spotted in the editor) is its ‘Text’. We can change that property in our code. When you hit ‘Start Debugging’ (F5), you’ll find that the text box will say “Hello World”.


Part 2 will cover classes a bit more and will show off variables and maybe even methods.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s