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!
Print Function:
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.
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
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.", []]
}
- story dictionary: This is where the story is stored. Each key represents a phase of the story, and the corresponding value is a list containing the story text and the choices available at that phase.
prevchoice = "start"
- prevchoice variable: This variable keeps track of the current phase of the story. It starts with “start” because that’s where our story begins. The prevchoice and nextchoice variables match with the keys in the story dictionary, so we can access different parts of the story.
print(story[prevchoice][0])
- print(story[prevchoice][0]): This line prints out the story text for the current phase (prevchoice). It accesses the story text by using the current phase (prevchoice) as the key to look up the corresponding value in the story dictionary. The [0] index accesses the first element of the value list, which is the story text.
if story[prevchoice][1]:
# make next choice
else:
# the end! goodbye!
- if story[prevchoice][1]: This line checks if there are choices available for the current phase of the story. It accesses the list of choices by using prevchoice as the key to look up the corresponding value in the story dictionary. The [1] index accesses the second element of the value list, which is the list of choices.
- else: if there are no choices available, the story is over!
if story[prevchoice][1]:
nextchoice = input("ur choice:")
nextchoice = nextchoice.lower()
- nextchoice = input(“ur choice:”): This line prompts the user to input their choice. It stores the input in the nextchoice variable.
while nextchoice not in story[prevchoice][1]:
print("invalid. try again.")
nextchoice = input(story[prevchoice][0])
- while nextchoice not in story[prevchoice][1]: This line ensures that the user’s input is a valid choice for the current phase of the story. It keeps prompting the user to input their choice until they enter a valid choice that is included in the list of choices for the current phase.
prevchoice = nextchoice
- prevchoice = nextchoice: If the user’s input is valid, this line updates the prevchoice variable to the user’s choice. This way, the story progresses to the next phase based on the user’s decision.
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
- while True loop: This is an infinite loop, meaning it will keep running until you explicitly tell it to stop. It ensures that the story continues until it reaches an ending.
- print(“the end”): If there are no choices available for the current phase of the story, this line prints “Goodbye” to indicate that the story has reached its conclusion.
- break: This keyword breaks out of the infinite loop, ending the execution of the program.
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!
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