Quantcast
Channel: YouChew Community Blog List
Viewing all articles
Browse latest Browse all 480

tabull's Blog - Makin' a Text Adventure Game

$
0
0
It’s been awhile since my last blog post, but since I’m doing a disk image backup (Hey, I talked about this before), I might as well do something to pass the time. This post will be about a little bit into the thought process I went through in creating a very simple text adventure game. This started off from Supreme Slayer’s secret santa gift thing last year, and since I can’t draw worth crap, I thought I’d make a game to whoever was to be my person.

The decision to do a text based adventure did not take very long. They’re a simple concept and I do not have to do anything graphical (except for all the explicit content, of course), so it was an obvious choice. I also figured I could get an engine created while waiting for signups to close and the names of the secret santas distributed. There was one thing I did not take into account, which were my extreme procrastination skills and as such, I did not actually work on it until 2 or 3 days prior the due date. And so, the following will be the chronicles of these 2 days to create a text based adventure game entirely from scratch.

The first question would be why I didn’t use a third party engine already and freely available on the internet. That would be simple: what kind of fun would that be? So with that, everything I made was entirely from my knowledge of text based adventures and so came the four major ideas: items, locations, actions, and events. Items and location are simple enough. Items just need a name, a description when looked at, whether or not it can picked up, and whether or not it should be shown on the screen. And really that’s it; in fact, here’s the code for an Item object
class Item
{
    private string _name;
    private string _lookAtDesc;
    private bool _pickable;
    private bool _show;

    public string name
    {
        get { return _name; }
        set { _name = value; }
    }

    public string lookAtDesc
    {
        get { return _lookAtDesc; }
        set { _lookAtDesc = value; }
    }

    public bool pickable
    {
        get { return _pickable; }
        set { _pickable = value; }
    }

    public bool show
    {
        get { return _show; }
        set { _show = value; }
    }

    public Item(string name, string lookAtDesc, bool pickable, bool show)
    {
        this._name = name;
        this._lookAtDesc = lookAtDesc;
        this._pickable = pickable;
        this._show = show;
    }
}
What an item does however, was a bit more complicated. That’s where my ItemUtilites class comes in handy. It contains a list of all the Items in the game and a map of which items can be used on other items. It also contains some methods on picking up items and checking whether or not using an item on another item is valid.

Next comes the locations (or Areas as I called them). The Area class contains things such as the name of the area, the text that should be displayed when a person reaches that area, what areas are nearby, and a list of items that are in the area. There is also another thing, which is the event name. As I wanted to make it simple, it’s possible to make it so just one event is required to reach a new place. That way if I wanted the player to enter a room, they may need a key first. When they use the key on a door, then the “door unlocked” event could fire and the player may enter the unlocked room. The event name can be added by the developer by putting the event name in parentheses after the area’s name. Probably not the best way to do require events, but hey, I had 2 days to make a relatively bug free game with a beginning and end. As with Items, Areas also had a utility class, but this one is much simpler. All it contained was a list of all the areas and a method to get an area.

The Event class was also simple. All I need to know is the name of the event and whether or not it is set. The utility class I made for events was also simple. It contained a list of all the events and methods to check if an event is set or to actually set an event as fired.

This would be a good time to say that these utility classes are very unoptimized, which I can also chalk up to “hey, you’re making a game in 2 days for someone, no one is going to care that the method took 10ms rather than 2ms.” These methods to find and set variables scanned the list of items/areas/events based on the name and returns the item/area/event if found.

Now comes the main program class, which took the most time. If anything everything I’ve discussed so far probably took an hour at most with some tweaks here and there. Some of these methods were unnecessary, but also allowed the player to be slightly amused. One of these methods is scrolling text, which I used for the title screen and game over screen. The scroll was fairly simple, split the text into an array of strings based on the newline character, then add each line to an array one at a time, and finally display the text at the bottom of the console.
public static void scroll(string s, string subtitle, int ms)
{
    String[] title = s.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

    int height = title.Count();
    int i = 1;
    for (int j = Console.BufferHeight - 2; j >= 0; i++, j--)
    {
        String[] currTitle ;
        if (i < height)
        {
            currTitle = new String[i];
            Array.Copy(title, 0, currTitle, 0, i);
        }
        else
            currTitle = title;

        Console.Clear();
        Console.WriteLine(new String(' ', j).Replace(" ", Environment.NewLine));
        foreach (string str in currTitle)
        {
            WriteLine(str);
        }
        System.Threading.Thread.Sleep(ms);
        
    }
	
    //Not includeing subtitle portion of method
}
Other things I added that wasn’t necessary but beneficial to the player was a wordwrap since consoles don’t do that, they just display characters until they reach the end of the window buffer. I thought I’d at least make it so the player doesn’t need to read a single word on two lines, the player is here to have a good time, not use more brainpower to try and connect two strings together.

Now of course comes the most difficult part of making the engine and the majority of the time I spent doing: the language parser. I need to take the player’s text input (their only means of control) and make it do something. So, with that in mind, I made the number of actions very small and limited. These actions were moving, looking, picking up items, and using items. Using items was the most difficult as it required typing in two items connected by the word “on”. To make things easier on me, I did not care about the order of operations, so if you wanted to say “Use door on key” you are more than capable of doing that. Because why not, here’s the code I made for using items. I won’t go into detail on what each line does, but it’s something to look at if anyone is interesting in trying to decipher what I was trying to accomplish.

Spoiler


So now it’s the due date, and I finished my engine and parser, but I needed to actually get the game created with stuff to do in two hours. The site’s down right now, but from what I remember the list I got on things to add included Deus Ex, John Carpenter movies (especially featuring Kurt Russell), and giant robots. I think there was a fourth item, but I never added it to the game. For Deux Ex, I added the “A bomb!?” scene (more or less verbatim) to the end of the game. For Kurt Russell, I added a computer to the starting room and when you looked at it, you got a really crappy ASCII art picture of him. And finally for the giant robot, you are required to hop into one to leave the first two areas. After that set in stone, I padded the game with useless items and locations and sent it on its way to be given out.


Posted Image

Amazing screenshot from an amazing game

For anyone interested in seeing the final result, it can be found here: http://www.mediafire.com/download/cj9ppc2ckzn94m3/UQQ.rar

Please note that you will need the .NET framework installed to get it to work. If you are on a Windows machine, chances are you already got it. I’ve also included the C# source code if anyone wants to take a gander at it.

Well, looks like the backup just finished and now it’s on to verifying the image, so I’ll call this post complete and enjoy Universalquantifer’s quest.

Viewing all articles
Browse latest Browse all 480

Trending Articles