Skip to content

Python CYOA Game Tutorial by Nathalie Meremikwu

Published: at 08:15 AM

fork in the path in a dark forest

Have you ever wanted to create your own adventure story where you get to make the choices? Well, today, we’re going to learn how to do just that using Python!

What Do We Need to Know?

What Python concepts do we need to create our game? Let’s take a look!

The print function displays text on the screen so we can see what’s happening in our program.

Input Function:

The input function lets us type in information while the program is running, so the program can use our responses to make decisions or perform actions.

Variables:

Variables are like labeled boxes where we can store different pieces of information that we want to use later in our program. If you want to learn more about variables in Python, try watching this tutorial.

Lists:

Lists are collections of items (like numbers or words) stored in a specific order, and we can easily add, remove, or access items from them. If you want to learn more about lists in Python, try watching this tutorial.

hands on keyboard typing code

Dictionaries:

Dictionaries are collections of key-value pairs, where each key has a specific value associated with it, like a real dictionary where a word has a definition. If you want to learn more about dictionaries in Python, try watching this tutorial.

If Statements:

If statements let us make decisions in our code by running certain parts of the program only if specific conditions are true. If you want to learn more about if statements in Python, try watching this tutorial.

While Loops:

While loops keep running a block of code as long as a certain condition is true, allowing us to repeat actions until we want them to stop. If you want to learn more about while loops in Python, try watching this tutorial.

Program Steps

CYOA game decision tree

Okay, that’s all the Python we will need to put together our program. Let’s start coding! In our adventure, we’ll start with a simple path in the woods. You’ll have to choose whether to go left or right.

story = {
  "start" : ["There is a fork in the path in the woods. Left or Right?", ["left", "right"]],
  "left" : ["You encounter a giant monster. Fight or Charm?", ["fight", "charm"]],
  "right" : ["You see a river and a bridge with a troll. Across the river is a way out of the woods! Swim or Troll?", ["swim", "troll"]],
  "fight" : ["Fight the monster? With what?? Your tiny fists? You died. The end.", []],
  "charm" : ["You and the monster have a lot in common! You become best friends. The end.", []],
  "swim" : ["You aren't a very strong swimmer. You drowned. The end.", []],
  "troll" : ["The troll is super laid back and let's you pass. You've made it out of the woods! The end.", []]
}
prevchoice = "start"
print(story[prevchoice][0])
if story[prevchoice][1]:
  # make next choice
else:
  # the end! goodbye!
if story[prevchoice][1]:
  nextchoice = input("ur choice:")
  nextchoice = nextchoice.lower()
while nextchoice not in story[prevchoice][1]:
  print("invalid. try again.")
  nextchoice = input(story[prevchoice][0])
prevchoice = nextchoice
prevchoice = "start"

while True:
  print(story[prevchoice][0])
  if story[prevchoice][1]:
    nextchoice = input("ur choice:")
    nextchoice = nextchoice.lower()
    while nextchoice not in story[prevchoice][1]:
      print("invalid. try again.")
      nextchoice = input(story[prevchoice][0])
    prevchoice = nextchoice
  else:
    print("Goodbye!")
    break

Conclusion

Now, it’s time to play! Run your Python code, and you’ll start at the beginning of the story. Read the text and make your choice by typing it in. Follow the story and see where your choices lead you!

girl walking in the woods

Congratulations! You’ve just created your very own choose your own adventure game in Python! You can always add more choices and make the story even more exciting. Have fun exploring different paths and creating new adventures!

Thanks for reading! | Nathalie Meremikwu