Text Based Adventure? (Python)

Discussion in 'Mac Programming' started by Nsutton, Apr 1, 2010.

  1. macrumors member


    Dec 29, 2009
    6 Feet Under
    How would you go about making a text based adventure in python?

    Nothing too fancy. I was thinking I would have a door to escape but you have to gather a certian amount of keys. So an inventory type thing would be need..

    Any step by step tutorial that can help me?
  2. macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    Don't take this the wrong way, but the number one skill that is essential to programming is problem solving. I think you would probably get more out of the exercise if you actually try and work this out yourself.

    Obviously if you have a specific question for a certain part then lots of people would be glad to help, but working out the flow of data for instance and how to interact with the user is vital stuff and you'll learn more doing it yourself than following someone else's written tutorial on the subject.
  3. thread starter macrumors member


    Dec 29, 2009
    6 Feet Under
    I understand. It's probably better to learn on my own.

    But i just need an starting point because i have no idea what to even start with/
  4. macrumors 68030


    Sep 13, 2001
    Portland, OR
    Here's an example thought process:

    What is an inventory? Hm, it's a list of items.
    OK, how do I make a list in Python? <googles python list> The first hit is http://docs.python.org/tutorial/datastructures.html
    Scrolling down to the examples it looks like I can make a list with this syntax:
    nameOfList = [item1, item2, item3];
    and add items to a list like so:
    So, if the player's inventory starts out empty, I'll need
    inventory = [];
  5. macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    Okay, think about the flow of the program. What happens when the user first executes the program? How does the user then proceed through the program? Do you use a menu based approach? Or some other method?

    How are you going to store information about the character that the user is playing as? What information should you store?

    You should probably look into the object orientated features that Python offers. One of the major advantages that object orientated programming offers is the ability to think about discreet things rather than data structures. So you have a character rather than a linked list or a struct that represents a character. You can also perform actions on that object that change its properties. This helps immensely when thinking about to implement a program.

    Just don't get carried away and try and implement everything all at once as that will just get you down in the end. Stick to one feature at a time.


    Semi-colons are evil :).
  6. macrumors 603


    Sep 19, 2003
    Canada, eh?
    When talking about making higher-level applications like games and business applications, what's more important than the programming language is some concept of how you're going to organize and arrange your data, and more specifically how you're going to model the "world" in which your program will work.

    For example, if you're writing PowerPoint, your "world" consists of slides, and the objects that appear on each slide. If you're writing iTunes, your world is a list of media files. If you're writing an e-commerce system, your world consists of lists of products and customer accounts.

    In the case of an adventure game, we really mean "world" in the literal sense!

    So let's start by looking at some of the basic requirements of a text adventure game like the old Zork games or MUD/MOO type games that used to occupy so much of my time. Well, at its heart, the world consists of rooms. You start in one room, and you can move in the cardinal compass directions, north, east, south, west, up, down. Each move, if one is possible, leads you to another room.

    Your game, once complete, will consist of hundreds or thousands of possible rooms, each interconnecting with other rooms. Each room will have its own name and description.

    Before you even think about what programming language you're going to use (be it Python, Java, C++, whatever) you need to think about how your data is going to be stored.

    You might recognize that "items of data connected to other items of data" sounds like a tree structure or a multiply-linked list. Each room (and its description) could be a node in the tree or list, and the links between nodes represents the available pathways between the rooms.

    So now you have a starting point. You can decide to model your world like this, with each node (a room) having the following information:

    - The room ID number (as you give this some thought, you realize that every room is going to have to have one)
    - The room title ("A Dark Cave")
    - The room description ("You step into a dark cave. Boy, it's sure dark!")
    - The ID number of the room to the north (if any)
    - The ID number of the room to the south (if any)
    - The ID number of the room to the east (if any)
    - The ID number of the room to the west (if any)

    So now you start to design the data structures. Next you will want to ask, what do I DO with those data structures? This will be the game "engine", which is essentially:

    Print the description of the current room.
    Get instructions from the user.
    Process those instructions.
    If the instructions are movement, go to the room the user requested (if possible).
    If the instructions can't be followed, print an error message.
    Repeat these steps until the game is over!

    You will eventually add more, such as fighting characters, processing items in inventory, and so on. But this is the starting point.

Share This Page