📖 Finishing the Rest of the Story | myBetabox

📖 Finishing the Rest of the Story

Keyboard Guide

Finishing the Rest of the Story

Stuck? Copy & Paste the Code into Repl.it

class TreeNode:
  def __init__(self, story_piece):
    self.story_piece = story_piece
    self.choices = []

  def add_choices(self, choice_a, choice_b):
    self.choices.append(choice_a)
    self.choices.append(choice_b)

  def tell_story(self):
    story_node = self
    print(story_node.story_piece)
    while len(story_node.choices) !=0:
      answer = int(input("Enter 0 or 1 to continue the story: "))
      if answer == 0 or answer == 1:
        choice = story_node.choices[answer]
        story_node = choice
        print(story_node.story_piece)
      else:
        print("Invalid answer! Try again!")

story_root = TreeNode("You awake in an empty room. There is a phone booth in the middle. \nDo you: \n 0 ) Investigate the phone booth. \n 1 ) Leave it alone.\n")
current_node = story_root
choice_a = TreeNode("You enter the phone booth and look around. You see a button that says 1980 and a button that says 3000. \nDo you: \n 0 ) Press 1980. \n 1 ) Press 3000.\n")
choice_b = TreeNode("You decide to leave the phone booth alone. You live out the rest of your life in boredom.\n")
current_node.add_choices(choice_a, choice_b)

current_node = choice_a
choice_a = TreeNode("You travel back to 1980 and get stuck in an arcade.\n")
choice_b = TreeNode("You travel forward to 3000 and see that not much has changed but people live underwater. \nDo you: \n 0 ) Swim into the city.\n 1 ) Stay in the phone booth.\n")
current_node.add_choices(choice_a, choice_b)

current_node = choice_b
choice_a = TreeNode("You swim into the city and meet some merpeople.\n Do you: \n 0 ) Try to talk to them. \n 1 ) Swim away in fear.\n")
choice_b = TreeNode("You stay in the phone booth and get sent back home.\n")
current_node.add_choices(choice_a, choice_b)

current_node = choice_a
choice_a = TreeNode("The merpeople welcome you to their city.")
choice_b = TreeNode("You swim out of the city and become lost.")
current_node.add_choices(choice_a, choice_b)

play_again = "y"
while play_again == "y":
  story_root.tell_story()
  play_again = input("Would you like to play again [y]/[n]?: ").lower()

The Code Explained

Time to finish your story! Give your game more scenarios and options to create an engaging adventure. Go back to line 28 (or under your last set of choices and above play_again = “y”) and update the current_node.

Based on our last example, choice_a was the option that continued the story (it wasn’t the dead end) so we will set the current_node to choice_a. If yours was choice_b you can write that instead.

current_node = choice_a

Type out your second scenario (including your story options) by following the same template. Feel free to copy and paste the previous chunk of code and replace it with new strings.

choice_a = TreeNode("You travel back to 1980 and get stuck in an arcade.\n")
choice_b = TreeNode("You travel forward to 3000 and see that not much has changed but people live underwater. \nDo you: \n 0 ) Swim into the city.\n 1 ) Stay in the phone booth.\n")
current_node.add_choices(choice_a, choice_b)

This time choice_b will continue the story and choice_a will be the dead end. Again, you can make either one the dead end but remember which choice it is for the next chunk of code.

current_node = choice_b
choice_a = TreeNode("You swim into the city and meet some merpeople.\n Do you: \n 0 ) Try to talk to them. \n 1 ) Swim away in fear.\n")
choice_b = TreeNode("You stay in the phone booth and get sent back home.\n")
current_node.add_choices(choice_a, choice_b)

Now you can keep going and add as many chapters as you want! If you’d like to create an official winning end to the game, type a string for the winning choice that says something like, “You’ve reached the end in one piece, congrats!” This will let the player know the game is over. It could look something like this:

current_node = choice_a
choice_a = TreeNode("\n The merpeople are allergic to dairy. YOU LOSE!")
choice_b = TreeNode("\nGrateful for your kindness and honesty, the king of the merpeople rewards you with the SAPPHIRE of the sea. YOU WIN!")
current_node.add_choices(choice_a, choice_b)

Keep in mind that this example assumes choice_a from the previous node wasn’t the dead end. Yours could be different!

When you’re finished, be sure to share it with friends and family and see if they can complete your adventure!