Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Text based adventure game Microsoft Visual Studio c#


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 alexfeetham   Members   -  Reputation: 140

Like
0Likes
Like

Posted 15 November 2013 - 03:09 AM

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.



Sponsor:

#2 jbadams   Senior Staff   -  Reputation: 19059

Like
0Likes
Like

Posted 15 November 2013 - 05:23 AM

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



#3 jHaskell   Members   -  Reputation: 1053

Like
1Likes
Like

Posted 15 November 2013 - 06:54 AM

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.



#4 Eck   Crossbones+   -  Reputation: 3096

Like
4Likes
Like

Posted 15 November 2013 - 09:16 AM

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

 

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

 



#5 alexfeetham   Members   -  Reputation: 140

Like
0Likes
Like

Posted 15 November 2013 - 01:29 PM

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.


Edited by alexfeetham, 15 November 2013 - 02:23 PM.


#6 Aspirer   Members   -  Reputation: 544

Like
2Likes
Like

Posted 15 November 2013 - 02:45 PM

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).



#7 alexfeetham   Members   -  Reputation: 140

Like
0Likes
Like

Posted 15 November 2013 - 10:47 PM

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).



#8 frob   Moderators   -  Reputation: 22255

Like
4Likes
Like

Posted 15 November 2013 - 11:48 PM

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.

 

ID Name Description Destinations
0 courtyard You are standing in the courtyard of a large castle. You can see a guardhouse and an arched entryway... 1,2
1 guardhouse This is the guardhouse. You can see the lands around the castle are filled with peasants. ... 0,4
2 entryway This is the entryway to the castle. The high archway is impressive. ... 0,7
 
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.
 
ID Name Description Location flags
0 knife 0: The knife looks sharp.
1: The knife looks dull.
2: The knife looks covered in blood.
1 0
1 bucket 0: The bucket is empty.
1: The bucket is full of water.
2: The bucket of water has a fish looking back at you.
4 2
 
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.


Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#9 jbadams   Senior Staff   -  Reputation: 19059

Like
0Likes
Like

Posted 16 November 2013 - 03:52 AM


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

#10 alexfeetham   Members   -  Reputation: 140

Like
1Likes
Like

Posted 17 November 2013 - 01:05 AM

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.



#11 jbadams   Senior Staff   -  Reputation: 19059

Like
1Likes
Like

Posted 17 November 2013 - 01:20 AM

Great progress Alex, congratulations!

 


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 [...]

 

If you haven't already done so I'd add at least two or three rooms and exits to the game to at least verify everything is really working correctly.  It shouldn't take you long, and you can then proceed to add all those other cool ideas, but if you add all the other features first you might have more trouble tracking down any problems with your existing code. It's always good to verify that what you've done seems to be working correctly before proceeding. smile.png



#12 alexfeetham   Members   -  Reputation: 140

Like
1Likes
Like

Posted 17 November 2013 - 01:56 AM

Already done that =) Added a few filler rooms to test the code works, so far everything works fine. Had a few little issues early on that I fixed easily enough.



#13 alexfeetham   Members   -  Reputation: 140

Like
1Likes
Like

Posted 26 November 2013 - 09:37 AM

Well I feel that C# is beating me right now. here's my progress so far.

 

Done:

- Method for creating rooms in the game and adding exits to them.

- Game loop

- Intro including storing character name as string (dunno if i'll use it again its more a hello so and so your adventure starts here type thing)

- Put the level list into the room class rather than having it in the program class (was easier than I thought)

- Put a few tester levels in to make sure everything works and navigation is possible

- Basic commands such as "help", "go", "quit" all working as intended.

To Do;

- Add items in (currently working on and its frustrating)
- Add inventory display to the game

- Add Player health to the game (shouldn't be hard to do to be honest, just gonna do it later)

- Add Traps to the game that have can affect player health

- Add monsters that need to be defeated to progress (will add weapons, easy once items are in)

- Finish the game!!

 

My first idea for adding items in was using a method within a separate class as I did with the rooms to list the items in the game and link them together. I can list them but i'm currently working out how to actually link them together e.g. If the players writes the command "take item" it will look to see if there is an item linked to this room and if so display that the item has been picked up and also write that it has been taken. I can't seem to link a level to an item (from class to class). 

Also this is due in next week so I have decided I will hand in the bare minimum required to pass the criteria and keep this as an ongoing personal project. I want to eventually have this as a finished game that I can keep adding to =P

Starting object orientated programming next month so im sure a lot of this stuff will become clearer on how I should approach them.

Cheers for any help that anyone has given so far it's been much appreciated.

TL;DR?

 



#14 Satharis   Members   -  Reputation: 1060

Like
3Likes
Like

Posted 26 November 2013 - 05:33 PM

Keep in mind one of the most important and fundamental rules of games(and programming as a larger profession) is to seperate data from code. Using that line of thinking we can go, well in the text adventure game what is data? Clearly the rooms themselves are data, the information about their exits and description and such, also data.

Usually when people start off with games they tend to do the "hard code" approach, i.e. you make a function that displays the text for one room and you write the description right into that function. While it works, its a lot of wasted code whether you're doing functional or OOP. Sure you could make a function for every room but the rooms are all so similar, instead why not consider the things that the rooms share to be data and have the function operate on that plain data.

Now you probably already know this quite a bit but I'm more emphasizing the principle behind it, because it basically works for everything you're going to do. Example being rooms, what might you do in a room? "look?" well what does that do really? Thinking of it logically we can deduce the look function probably needs to grab the room title, its description, and check its exits to display which exits are open to you. So how can we do that? Well we'll need a collection of rooms for one, as was stated above you can use something like an array or a collection to contain a bunch of "room" data objects. What else do we need? Well we have to know which room the character is in so we can grab its information, there's a few ways to do that as well. For instance you could create a number index system, i.e. your player has a variable saying it is in room 7 and room 7 either is an index into the room array or maybe an id number that you search for. You also could just have a "room" variable for the player object that will be a reference to the room they are currently in.

Sorry if that was a lot of text, since this is homework I don't want to give too much concrete examples, but if you think about the logical deconstruction of the "look" function, you can apply that to other things, items, monsters, doors. It's all basically the same.

Edited by Satharis, 26 November 2013 - 05:34 PM.


#15 alexfeetham   Members   -  Reputation: 140

Like
2Likes
Like

Posted 03 December 2013 - 04:00 PM

Well as of tomorrow (provided I finish it up in time to a good standard) this becomes a personal project =) So no time restraints can just change it to my hearts content =D



#16 Eck   Crossbones+   -  Reputation: 3096

Like
0Likes
Like

Posted 03 December 2013 - 07:50 PM

Congratulations on completing the project sir. :)

 

I'd also like to say that this thread was much better than many of the other homework help threads out there. So thumbs up there as well. 

 

- Eck



#17 alexfeetham   Members   -  Reputation: 140

Like
0Likes
Like

Posted 04 December 2013 - 08:20 AM

Let me guess Eck you get a lot of folks just expecting you to hand them the code on a plate? =P






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS