Text based adventure game Microsoft Visual Studio c#

Started by
15 comments, last by alexfeetham 10 years, 4 months ago

So i've recently started on a college course for games development, for my programming unit which has so far focused on Event drive programming using c# (about 2 months in now) i have to create a text based adventure game, something along the lines of a zork type game. My initial attempts were using a if statements to manage where the character goes e.g. "If (txtIn.Text = "left")

lblOut.Text = "You have gone left.......Insert next part...;"

My tutor semi hinted at a better way to do this having the information on a different file or page with the locations and the items at those locations which i've narrowed down to either being a class page or XML file. Had a friend briefly explain how to store information on a class page and made a basic one for character information. I have no idea how to take information and use it from these pages in the game itself though which is the issue, google has been of little help in this case (allthough it did allow me to stumble upon this forurm =D). Can anyone point me in the right direction? Will keep looking and trying to figure it out myself regardless.

Cheers, Alex.

Advertisement

Moving you to our For Beginners forum for this question. smile.png

- Jason Astle-Adams

Not sure what a class page is (Google only brought up Microsoft's Page class, which looks like something entirely different). XML is a viable option though and the quickest/easiest way to work with XML is via an available XML parser. One of the easiest to get started with is TinyXML.

Welcome to the forums. This place rocks. I wish I had found it earlier in my career. smile.png

I don't want to get too specific since this is a school assignment and I'm not 100% sure what you're asking. If you're managing your game state (current room location, available exits, items on the ground, etc.) via a huge block of if/else if statements, then yeah there's a better way to do this. And by class page, I think you're just talking about encapsulating this data into a Room class?

Add a Room.cs file to your project and put the data you think you'll be needing there. Name, List of exits, maybe a visited flag, etc.

Take a look at the Dictionary collection. There's a few built in ways to store/access your data. Read a little bit at the top for an overview and scroll down to the bottom to see the sample code in action.

http://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx - Dictionary

In a nutshell, this collection lets you store data and look it up by a unique key. So, at the start of your program, you can create the world via code in a LoadWorld function. Creating your rooms and storing them in your dictionary using your Name as the unique key.

Next hint - OK Eck, what now?

[spoiler]So now you have your world crammed into a dictionary by Name. You can keep up with which room you're currently in by storing the name in a currentRoom variable. When your player tries to move "left", you can access the current room, and see by its list of exits if left is a valid choice. Your exit would need to know which room it takes you to. Then you'd change your current room to this new key.

Allowing the name to serve as the unique room ID is fine for now, but later on you might want to add a RoomID member in your Room class. Using the Name as a unique identifier violates the one-responsibility edict. Consider the Maze in Zork. If your current location was Maze1, Maze2, Maze3, mapping it would have been MUCH simpler. But for now, I recommend keeping Name as your unique key to simplify things during your learning. Later when you want to support a "Maze" you can refactor your code to use RoomID instead which will be a good exercise.

[/spoiler]

You mentioned XML files so maybe you were just talking about data driving your LoadWorld function? When done right, this is really cool because you could have two different text based games just by loading a different xml file. To change the world, you can change the xml instead of recompiling your code. If that's what you were asking about, you'll want to take a look at xml serialization. If you're still pretty new to C# this might be a lot to take in. For now, getting your code working with a "hard-coded world" is perfectly fine.

http://msdn.microsoft.com/en-us/library/182eeyhh(v=vs.110).aspx - XML Serialization

If this isn't what you're asking for help with, please let us know. smile.png

- Eck

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

Wow, thanks for the responses =D Gonna see if i can get a room class going, one thing I don't understand is how I can actually use that in the game, you may have already mentioned in your reply and on one of the links so if you have i'll get to it at some point =) edit: Got a basic dictionary for storing the rooms, now I just gotta figure out how to create a currentRoom variable and how to use it.

Again, this is a school assignment, so I'm going to be specifically vague...

A "Room" class, listing all information (Description, exits (hint: Enums), and items)

Your basic map is 2D. North/South & East/West... A multidimensional array to handle your multi-dimensional map.

(Also, your NORTH, SOUTH, EAST, WEST enumeration should be checked to see if it's a valid exit).

Well i guess im going into object orientated programming much sooner than we were going to be in class, nice to get a head start =P Means whilst the rest of the class is learning I can just sit back and do some programming.

After a few hours of research, talking to a friend (mainly him confusing me with code I can't even comprehend) i've stumbled across a few sites with similar projects that actually explain their code and how it all works giving me the necessary tools to get cracking with my own.

Aspirer I understand what you mean by the 2D map N/S/E/W and how to implement that and i'm currently working on it. Soon as that's up and running and there's some basic navigation within the game I can start to expand it adding objects and how they are needed to progress within the game.

All in all programming rocks, I only wish I had hopped onto this course years ago rather than faffing around with A-Levels and such (god what a waste of time that was, I could have been neck deep in programming by now and at uni).

Your professor is right about that. That style of text adventure is easy to implement as a state machine.Since you are just entering your studies, you probably won't learn about state machines for another year or two.

In an extremely simplified view, imagine a few spreadsheet tables. The first table defines your world. The only thing you need to track is the room number you are currently in.

[table]
[tr]
[th]ID[/th]
[th]Name[/th]
[th]Description[/th]
[th]Destinations[/th]
[/tr]
[tr]
[td]0[/td]
[td]courtyard[/td]
[td]You are standing in the courtyard of a large castle. You can see a guardhouse and an arched entryway...[/td]
[td]1,2[/td]
[/tr]
[tr]
[td]1[/td]
[td]guardhouse[/td]
[td]This is the guardhouse. You can see the lands around the castle are filled with peasants. ...[/td]
[td]0,4[/td]
[/tr]
[tr]
[td]2[/td]
[td]entryway[/td]
[td]This is the entryway to the castle. The high archway is impressive. ...[/td]
[td]0,7[/td]
[/tr]
[/table]
Then you have another data table with all the items in your game and their locations. It can also have information about their current state.
[table]
[tr]
[th]ID[/th]
[th]Name[/th]
[th]Description[/th]
[th]Location[/th]
[th]flags[/th]
[/tr]
[tr]
[td]0[/td]
[td]knife[/td]
[td]0: The knife looks sharp.
1: The knife looks dull.
2: The knife looks covered in blood.[/td]
[td]1[/td]
[td]0[/td]
[/tr]
[tr]
[td]1[/td]
[td]bucket[/td]
[td]0: The bucket is empty.
1: The bucket is full of water.
2: The bucket of water has a fish looking back at you.[/td]
[td]4[/td]
[td]2[/td]
[/tr]
[/table]
Then you write a third and slightly more complicated data table that gives the actions you can take to move between states.

Put them together and you have a generic text adventure engine. It works like magic.


That style of text adventure is easy to implement as a state machine.Since you are just entering your studies, you probably won't learn about state machines for another year or two.
Perhaps a good time to mention your article "State Machines in Games" from earlier this year. Might make for good "further reading". cool.png

- Jason Astle-Adams

Frob, read through your article that jbadams linked. Its for sure something I will look into in the future and have the link bookmarked =). Got the framework for the game built including a way for the user to exit the game and a message being output before they do rather than the game just closing.

In essence all i need to do to have working navigation is add rooms to the game and their respective exits, before I do that though theres plenty more for me to add such ass items that the user can find and ways they interact with the game, also thinking of adding traps to the world e.g. "You have enter blah blah room, in front of you is a trap, there is a chance you can skillfully navigate your way past it but you may fail." With a preset chance for the user to fail or succeed with failure resulting in the user taking a hit to their health.

I would also like to add monsters to the map but thats something I will look into later. Considering when I made this post my knowledge of how to even go about this was slim to non I don't think i've done bad =P

Cheers to everyone who has posted in this thread, some really useful information here.

This topic is closed to new replies.

Advertisement