How might I create a branching text game?

I want to try making a nonlinear text RPG. I understand the basics of making a game engine, and I can review Zed’s in the Python book if needed. But how can I implement a branching plot, one which may or may not – I haven’t decided – include re-converging branches? If statements are the basic branching construction, but obviously a bunch of nested ifs would be a mess.

At the most basic level you have to map out the whole plot, all the possibilities and endings.

And I suspect you’d have to implement some sort of “state” manager that keeps track of the decisions your player makes. That way the “scenes” of your game react to those changes.

For example, if you return to an early room after you destroyed a dam, the room would now be flooded.

Also Keeping branches as simple as possible. Meaning at any given point where the game can split off, it can only really split of one or two ways.

I hope that helps.

Put a variable at the top of your script:

has_key = False

In the function where they can get the key do this:

def secret_room():
    global has_key
    # do some stuff and they get the key
    has_key = True

Now in the other part of the game where having a key decides a change in the game you just do:

if has_key:
   # different path
   # regular path
1 Like