Source

TextRPG / branching_story.py

Full commit
"""A branching story: moving through the woods.

decisions: right, left, straight, back

ideas: 
    - with a pursuer

"""
from lib.textrpg import story, ask

story("""Fading red light 
filters through the leaves 
and trunks of the trees, 
as you leave the campsite. 

The first chill of the night 
touches your skin, 
and your summercoat
thin as it is, 
protects you just feebly. 

But the way which was clear 
the day before today
now seems forgotten, 
after this day of dancing
with the sister of the sun
a child of the faery kin. 
""")

wood = [
['you car', 	'withered leaves', 	0,			0, 	0], 
[0, 		'the rabbit hill', 	'dark underbrush', 	0, 	0], 
[0, 		'the angry boar', 	'the campsite', 	0, 	0], 
[0, 		0, 			0, 			0, 	0], 
[0, 		0, 			0, 			0, 	0]
]

def get_text(name):
    """Get the text for each locations name"""
    return name # TODO: Get the text from a dict. 

class Path(object):
    def __init__(self):
        self.position = [3, 3]
        self.target = [1, 1]
    def pos_to_name(self, pos=[3, 3]):
        """return the location name for a position."""
        return wood[pos[0]][pos[1]]
    def move(self):
        x, y = self.position
        # specify possible movement. 
        move = {
        'north': (x, y + 1), # up
        'south': (x, y - 1), # down
        'west': (x - 1, y),  # left
        'east': (x + 1, y)  # right
        }
        question = 'Where do you want to go? '
        answers = []
        # get all possible answers by checking if the ways are possible. 
        for i, j in move.items():
            print i, j
            if wood[j[0]][j[1]]:
                answers.append(i)
        question = " ".join([question] + answers + [":"])
        description = "\n".join(['to the ' + i + ' you see ' + 
self.pos_to_name(move[i]) for i in answers])
        story(description)
        target = ""
        while not target:   
            ans = ask(question)
            for i in answers: 
                if ans in [i.lower(), i.lower[0]]: 
                    target = move[i]
        self.position = target
        story("""You go to """ + self.pos_to_name(self.position))
        story(get_text(self.pos_to_name(self.position)))

path = Path()
while not path.position == path.target:
    path.move()