Very Basic Python Text Adventure

I'm very new to python, and I'm trying to create a basic text adventure in python. Basically, the way it works is that there is a statement, and then 2 or three choices, each of which leads to something different. My question is this: How can I link 2 statements together so that they lead to the same outcome, when they are in different layers? What I want to do is something like this(This is just a simplified version of what I have):

>>> choice1 = input("Will you drink the potion?")
>>> if choice1 == "A":
>>>     choice2 = input("You drink it, and when you do,  you start to see strange visions.  But, to your right, you see another bottle marked 'antidote'\nDrink it?")
>>>         if choice2 == "B":
>>>             print("You die")
>>>         elif choice2 == "A":
>>> elif choice1 == "B":
>>>     #etc, etc.

Here, I want choice2, if given the input A, to lead back to the same place choice1, if given the input B, would lead (choice3).

Upvotes: 2

Views: 2051

Answers (4)

Leo Cornelius
Leo Cornelius

Reputation: 5

You could use functions

if choice2 == 'A':
    function()
If choice1 == 'B':
    function()

Upvotes: 0

steveha
steveha

Reputation: 76695

As @dave mankoff suggested, you should use a "state machine". Here is more detail about that.

For a basic state machine, you need a "state variable", a variable that keeps track of the current state. Then, as the user makes choices, the state variable gets updated to reflect the new state.

You can call the state variable anything, but I suggest calling it state. That's traditional and pretty straightforward.

You can use any set of unique values to keep track of the state, as long as you can map the unique values onto the states you want. In most languages you would use an "enum", an enumerated constant, but Python has historically not had enums. (They are being added as of Python 3.4, which at this time is in alpha release.)

A simple way is to use strings for the states, and have the strings describe the states. So for your game, most of the states will be places the player can go: "library", "kitchen", "potion_storage_room" or whatever. But you might also have states that don't strictly map to single places, such as: "wandering_around" (in a daze after drinking the wrong potion?).

You can also use integers, something like this:

LIBRARY, KITCHEN, POTION_STORAGE_ROOM, WANDERING_AROUND = range(4)

Now LIBRARY is set to integer 0, KITCHEN is set to integer 1, and so on. I think strings might be easier, but if you search for examples of state machines, you will probably see the above trick being used for integer states for the state machine.

You might want to study the book/website Learn Python the Hard Way. Exercise 43 shows a simple text game. It uses a state machine with strings to represent the state. There is example code for a simple game, and you could run the code and play the game to see how it works. (It doesn't look like a very fun game, but I think the author of that book wants to encourage you to do a better one!)

http://learnpythonthehardway.org/

http://learnpythonthehardway.org/book/ex43.html

Upvotes: 2

dave mankoff
dave mankoff

Reputation: 17769

What you're looking for is what is known as a "state machine" (a finite-state machine, more specifically). You have a player who is in state A. They then are given a number of choices that can move them to state B or C. From B they are given more choices that can lead them to other states - over to state C for instance.

There are many many many ways to implement a state machine. For example, you might simply use a dictionary where the key's are the states, and the values are a list of choices and resulting states:

{'A': {'prompt': 'Will you drink the potion?',
       'choices': ['B', 'C']}
 'B': {'prompt': 'You drink it and when you do, ...',
       'choices': ['C']}
 'C': {'prompt': 'You die'}}

This can obviously be made much more complex, and using dictionaries like this won't scale well to a more complicated set of choices, but it should give you an idea. You can build a whole game world like this and then simply write a function to interpret it:

world = {...}
def play(state):
  print world[state]['prompt']
  if 'choices' in world[state]:
    # print the users choices
    return input('So which do you choose?')
  return None

state = 'A'
while state is not None:
  state = play(state)

That example is incredibly primitive - you'll want error checking and all sorts of other bells and whistles - but it should give you the gist of it.

As an aside, it may help to draw out your state machine before you start coding it. I would at least draw a small piece of it. You can see examples of simple state machines by doing a quick image search: https://www.google.com/search?q=state+machine&source=lnms&tbm=isch

EDIT: Thanks to @steveha for pointing out an obviously dumb error in my code!

Upvotes: 9

JoeC
JoeC

Reputation: 1850

You could do something like

def start():
    choice1 = input("Will you drink the potion?")
    if choice1 == "A":
        choice2 = input("You drink it, and when you do,  you start to see strange visions.  But, to your right, you see another bottle marked 'antidote'\nDrink it?")
        if choice2 == "B":
            print("You die")
        elif choice2 == "A"
            start()
start()

Upvotes: 0

Related Questions