Making with Code

Story Lab #

In this lab, you are introduced to the structure for writing a branching story. You will use this in your unit project.


[0] Setup #

๐ŸŒ Github Repo: github.com/Making-with-Code/lab_story

๐Ÿ’ป Enter the Poetry shell and install the requirements:
poetry shell
poetry install

This repo includes the following files:

  • game.py
  • story_setup.py
  • view.py
  • node.py
  • story.py

[2] How do you write a story? #

Stories are made up of connected connected Node() objects.


[Node] #

๐Ÿ‘€ Let’s start by looking at the Node constructor:

class Node():
  def __init__(self, id, option_title, description):
      self.id = id
      self.children = []   
      self.option_title = option_title                    
      self.description = description  
  • id: a unique id (str, float, or integer)
  • children: a list of Node() objects
  • option_title: the text for the display in the menu (str)
  • description: the additional text for when a user selects an option (str)

It has the following methods:

  • __str__: defines how a Node is printed
  • add_child(child_node): this adds child_node to its children
    • this is how the story pieces are connected to each other.
  • find(id) - searches through the nodes to return the node with the id

[Story] #

Now, let’s look at the Story() constructor. This is the primary class you will be interacting with when writing your story.

class Story():
  def __init__(self, title, start_id, start_option_title, start_description):
      self.title = title
      self.current_node = Node(start_id, start_option_title, start_description)
      self.first_node = self.current_node
  • title: the title of your story (`str)
  • current_node: the current Node of your story, at the start of the story it is the same as first_node
  • first_node: the first Node of your story

A Story() has 4 main methods.

def add_new_child(self, parent_id, child_id, child_option_title, child_description):
  parent_node = self.current_node.find(parent_id)

  new_child_node = Node(
      id = child_id,
      option_title = child_option_title,
      description= child_description
  )

  parent_node.add_child(new_child_node)

def get_current_children(self):
  return self.current_node.children

def set_current_node(self,chosen_node):
  self.current_node = chosen_node

def is_finished(self):
  return len(self.current_node.children) == 0
  • add_new_child(): adds a new child node to a parent node
  • get_current_children():returns a list of the children of the current node
  • set_current_node(chosen_node): sets the current_node to the chosen_node
  • is_finished(): returns True or False based on if the current node has children

[3] Building your first story #

๐Ÿ‘€ Let’s start taking a look at story_setup.py As you can see, the current story has only has 4 unique Node() objects and it call .add_new_child() to build the story.

๐Ÿ’ป Try to play through the short story in by running: python game.py

$ python game.py

==================================================
Title: Lunch.

It's lunch time.
Where will you go?
==================================================

[what will you do?]
โฏ Walk to cyberport.
  Elevator down to A Block Cafeteria.

๐Ÿง Hmmmmm…. it works, but it the story does not continue with your choice.


[Finish game.py] #

๐Ÿ’ป Finish game.py so it properly plays through the story. It should:

  1. loop through the given chosen options
  2. display the description of each chosen node
  3. end when there are no more options for the chosen node

๐Ÿง Consider…:

  • What methods exist in the View() and Story() that you could use?
  • how do you loop until a condition is met?

๐Ÿ‘พ Be sure to play test the game to ensure it works as expected: python game.py


[Continue the Story] #

Now that you’ve gotten a working game.py, let’s build out the story.

Come up with your own options to continue the story! We’ll share out at the end of class.

๐Ÿ’ป “Continue the story, add at least 3 unique Node() objects. Some ideas…

  • add additional lunch options in ISF A Block Cafeteria
  • add lunch options in Fusion
  • add other locations to purchase lunch in Cyberport

๐Ÿ‘พ Be sure to play test the game to ensure your story additions work as expected: python game.py


[5] Deliverables #

โšกโœจ

Once you’ve successfully completed the game be sure to fill out this Google form.

๐Ÿ’ป Push your work to Github:

  • git status
  • git add -A
  • git status
  • git commit -m “describe your drawing and your process here”

    be sure to customize this message, do not copy and paste this line

  • git push


[6] Extension: #

For your project, you will need to build out one additional feature to this Story framework. Here are a few suggested features:

  • looping stories with the ability to set an Node as a current Node’s child
    • e.g. player can go back to a previous area
  • a Player class with unique properties
    • e.g. hunger, money, health
  • variable messaging in the story
    • e.g. "you have visited this store 5 times"
  • a special Node child-class that gives items or status effects
    • e.g. a locked door where you need to first collect the key in a certain room.

๐Ÿ’ป Use this time to experiment with of these features! If you have your own ideas, feel free to experiment with that!