πŸ‘©β€πŸ’» Putting Your Story into Code | myBetabox

πŸ‘©β€πŸ’» Putting Your Story into Code

Keyboard Guide

Putting Your Story into Code

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")

The Code Explained

story_root is where our story officially begins for the player. Think of it like the first page!

story_root = TreeNode()

When we type TreeNode() we are starting the scene and we will pass our first story_piece into it. In our example, we begin our scene with a phone booth in an otherwise empty room. Let’s describe that as a string.Β This will be your first story setting!

🌟 A string is a sequence of characters (numbers, letters, symbols and spaces) and start and end with quotation marks. Think about it like a sentence.

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")

Notice how we used a new line characterΒ to make the text look nicer in the game? This prevents the text from looking clumped together once the game runs.

🌟 Create a new line in Python by typing \n to give a line break.

Now is the time to type out your first scene and its story choices as the 0 and 1 options!