Ginger — Text Adventure
When I just started learning programming I had no idea what programming is about. What I knew back then was just the fact that we can write some lines of code to have something happen, which seems pretty powerful. Not to mention, it seems challenging and involves math which I am very interested in. I was fascinated by those facts. However, down the road, I picked up that if-else is a key concept in programming:
if (condition) then (something happen) else (something else happen)
Building Text Adventure game is one of the great ways to practice this concept. In this game, the main thing we need to do is to use lots of if-else to navigate a user through different scenario based on their choices. It is also very rich in story telling. Alright so how do we start?
First, we initiate a variable, here I called it ‘choice’. This variable is to store user’s choice. You can set up the story however you want. Let your imagination roar. The input() built-in function in Python is for users to be able to type in their input. A typical storyline starts with a statement and ends with at least two choices.
For each selection, the user will be prompted with a different response. For example, if I selected 1 for the first choice, which is Peanut butter bread, it will ask me How do I get bread next.
You will notice that I used a while loop here. Loop is used to run things over and over. Here is syntax of while loop:
The condition put in will be evaluated True or False. In the above case, I had a variable ‘invalid’ to update if a user is giving a valid answer. If they do give a valid answer, the variable will be update to False and the loop is stopped, otherwise it will keep running till the user gives a valid one. Remember that given a user inputs 1, 2, 3 as number, input() function turns the value into string no matter what so when you compare the choice in if condition, make sure you compare it with a string. In case you do not know, string is a series of characters.
Hold on, but if we keep adding more logic like this, it does not seem sustainable and easy to maintain, it feels more like a jungle! There must be a more systematic way than the one currently:
The current problem is that there is too much content and many if else mixed together. Whenever there are things repeating themselves, the concept of loop arrives to me. With this in mind, we need to structure content in a list, one line needs to be linked to the other by a code number. For example, peanut butter will lead to story line with code “1”, which is “There is some peanut butter on the shelf…”
A list is basically a list of items. The list above has nested lists in it. Each item in the list has an index just like a house has an address. The index starts with 0 so to access the first index, we do list_name, in this case, stories will give us the first story. Then to get into the first story line, we need to do stories because it was nested 3 times.
First we need a function to get story, this function will take in a story index as a parameter and return the story we need. To do this, we need to loop through stories list and check which one has a code that is exact like that parameter and return it if found. I will explain the variable showStory later.
So how are we going to extract the content to display? We need a variable to update which story is being rendered then we call the function above to get story and story it in another variable ‘story’ before printing the story line. Remember the story line is nested 2 times within the story so we have to get two indicies to have access.
After the story line, we extract the choices and using loop to print each of them one by one.
Next, to collect user’s input, we use input function and store answer into a variable.
Moving on, we need to validate the variable to make sure it is an integer and it is in range from 0 to the length of choices list to make sure it is available. If not, we need to indicate to user that it is invalid and most importantly we need to get their choice again, this is where a variable inValidAnswer and while loop come in. This variable is a boolean, initiated as True and will be updated to False if they give valid answer, based on this the while loop will be decided to keep running or stop.
Last but not least, since we want the stories to keep rolling out, we need a while loop and a condition to stop or maintain it, this is where the variable showStory comes in. This variable is initiated as True and when one story is being shown, it is updated to False to stop the loop until a user makes a valid choice, a new story will be rendered.
And wala, the game is completed, check full code here, the content still needs to be filled more.