Carcanken
Carcanken

Reputation: 91

Class Methods in Text Adventure with Branching Storyline

I'm working on an assignment that involves creating a text adventure with a branching story line.

It works like this:

Each choice taken leads to a new and unique prompt on the next level with 3 more choices.

I have a basic class set up like this:

class Level:
    def __init__(self,level_num, level_prompt):
        self.level_num = level_num
        self.level_prompt = level_prompt
        self.choices = []

    def add_choices(self, choices, next_branch):
        self.choices.append(choices)
        self.next_branch = next_branch

    def print_situation(self):
        print("Level " + str(self.level_num))
        print(self.level_prompt)
        print("[A] " + self.choices[0])
        print("[B] " + self.choices[1])
        print("[C] " + self.choices[2])

And I am initializing the levels starting off like this:

level1 = Level(1, 'PROMPT HERE')
level1.add_choices('CHOICE A', '2_A')
level1.add_choices('CHOICE_B', '2_B')
level1.add_choices('CHOICE_C', '2_C')

level2_A = Level(2, 'PROMPT HERE')
level2_A.add_choices('CHOICE A', '3_A1')
level2_A.add_choices('CHOICE_B', '3_A2')
level2_A.add_choices('CHOICE_C', '3_A3')

level2_B = Level(2, 'PROMPT HERE')
level2_B.add_choices('CHOICE A', '3_B1')
level2_B.add_choices('CHOICE_B', '3_B2')
level2_B.add_choices('CHOICE_C', '3_B3')

level2_C = Level(2, 'PROMPT HERE')
level2_C.add_choices('CHOICE A', '3_C1')
level2_C.add_choices('CHOICE_B', '3_C2')
level2_C.add_choices('CHOICE_C', '3_C3')

I'm having trouble figuring out how to associate each choice made with the next "level". I have the "next_branch" argument in the "add_choices" method but I'm having trouble figuring out how to follow up with that.

How I want it to work is, during level 1, you choose A, so then the program will print the prompt and choices associated with "level2_A". Keep in mind this will have to support a ton of separate choices and prompts in the later levels, so I'm trying to find an efficient way to manage the branching storyline.

Let me know if anything isn't clear..

Thanks a lot!

Upvotes: 0

Views: 106

Answers (1)

Jon Winsley
Jon Winsley

Reputation: 106

Instead of passing a string "2_A" to the method, you could define the level2_A instance first and then pass the object directly to the method:

level1 = Level(1, "PROMPT HERE")
level2_A = Level(2, "PROMPT HERE")

level1.add_choices("CHOICE A", level2_A)

In this scenario, you would define all of your levels first, and then define how they are related to one another.

With exponential growth like this, your game will quickly get very massive. You might find it easier to store the definitions in a data file, and construct the level objects automatically by loading data from the file. This would separate your content from your game logic and make things easier to work with. Something to think about!

Upvotes: 1

Related Questions