Creating Interactive Fiction - from Twine to Python
Modified on: Thu, 15 Sep 2022 7:01 AMFollow these instructions to install Python 3, then write your interactive fiction in Python
Setting up Python
In Chrome, go to the website: python.org/downloads
Download and install the latest version of Python 3. As of today, that's 3.6.2 but it might be newer for you, depending on how far in the future you are reading this. Greetings from the past!
Installing Python
Click the Continue button on three installation screens screens (Introduction, Read Me, and License).
Agree to the terms of the software licensing.
Type in your administrator password.
When the installation is successful, click on the Close button
Click on Move to Trash when asked.
Open IDLE
IDLE is where we'll write and run our Python program. Go to your Applications folder and open IDLE in the Python 3 folder.
The Shell
When you open Python, you'll be greeted by the Shell. This is the place where the output of our program will be seen.
For now, check that the first line says Python 3.X.X. We want to be sure we are using Python 3, not 2.
Open a new file
We write our program in a new file, not the shell. Open a new file for our use
Creating our python program
Ready to get programming? Let's get started.
One of the most important parts of any programming language is comments. This allows us to write notes to ourselves or others who will read our program. These won't be run by Python - they are just for the humans who look at the code! Comments are really helpful in explaining your code, and helping you work through your thinking.
A comment is made by typing a #, followed by your comment.
Notice how the comment is red? IDLE colors the code to keep it easy to follow. We'll add comments as many places as possible.
Starting your program
At the top of your program, it's a good idea to start with a few lines of comments.
Start your program with a comment for the name of the program. Follow that with your name, then the date, then any notes you might want to add.
Twine
Remember our story from Twine? Let's get that open. We'll start by making the starting passage from our Twine story in Python. Double click on the opening passage.
Viewing the passage
Here we can see the text and the choices from this passage. Keep this open while we hop over to Twine
Turning Passages into Functions
Let's turn this passage into a function.
A function is a chunk of code that does one specific task. In this case, we'll make a function that shows the text, waits a second, shows the options and then goes off to the next passage depending on what the user chooses.
A function is made, or defined, by using the define command.
Enter def followed by the name of the function you are making, then an open and close parentheses, then a colon. Like this: def intro():
We could call our starting passage whatever we like, so you might have def start(): or def beginning(): or def whateveryouwanttocallit(): depending on your style.
After we define the function, let's go down to the next line and indent by pressing the tab key once. Here we'll make text appear on the screen with the print command. The print command works as follows print ("TEXT YOU WANT TO SHOW"). In this case we want to show the text from the passage.
Notice how we made the print command go over 3 lines? That makes it a bit easier to read. To do that, just end the line with a ", then return to the next line and start with another ".
Don't forget to end the print with the closing ). That's a classic programming error, or bug. Remember, if you open something you must close it!
Just wait a second
The next thing we want to do is pause 1 second before displaying the choices. To do this, we'll use the line time.sleep(1).
See how we've added a comment in the same line as this command? That's called an in-line comment and it's a super good idea. Use comments as many places as possible to make it easy to understand your code.
time.sleep() is a special function that requires what's called a module. In order to use a module, we have to import it which we'll do in the next step...
Import time
Above the function, import the time module by typing import time.
Behind the scenes this brings in a bunch of complicated code so Python knows what a second is. We won't worry too much about that, but give a quiet thank you to whomever created the time module and just saved you a ton of work.
The important thing (get it) is that if we use a function from a module, we must import it at the beginning of our program.
Give me options
Now that we've shown the text and waited a second, let's show the options.
To do this, use a print command.
Notice how this print command uses a different option for spanning multiple lines? If you surround your print text, which is called a string, in tripple quotes ("""), you can span multiple lines. It's another way to span lines like we did before. Choose whichever feels right to you. Listen to your heart.
Don't forget to use inline comments - they are super helpful!
Input please
Now we need to get input from the user.
To do this, we'll create a variable, which is a bucket in which to save something. We'll call it choice, and that's where the user's choice will be saved.
We'll set that equal to a function called input(). Input gives the user a chance to type something in.
So here, we are asking the user to type something, and we are saving it as choice.
Choices
Now'll we'll look at what the user said. If it's A, we'll go to the function that matches that choice (which we'll make later).
To do this we'll use the if statement. An if statement looks to see if something is true, and if it's true it'll do something.
Here we setup the statement like so: if choice in answer_a: Remember, choice is where we are saving the answer from the user, and answer_A is a list of acceptable answers that'll match as A. We'll set that list up soon.
After the if line, we indent (use tab) and then enter the name of a function we'll make for that choice. In this case, if they select A they will be stopping the colonists, so we named the function option_stop_colonists(). You can name the functions as you see fit.
Adding other choices
Now that we've checked if the user chooses A, we'll setup the next if statement for B.
Here we'll use an elif - this is Else IF, which means if it wasn't A, let's check if it's something else.
The line will be elif choice in answer_B: answer_B is a list of acceptable choices that'll match for B.
Then we name a function we'll make later that will be the passage for that option. In this case B matches with waiting, so we named the function option_wait(). You can name the function as you see fit.
What if they didn't choose A or B
Finally we need to check if they didn't choose A or B. To do this we'll use an else: which says anything other than what we looked for above will be matched as true. So if it's anything else, we'll want to print some instructions on the options. we will save those instructions as required, so for now we'll add else: and then indented on the next line, print (required).
Finally we'll want to give them the passage again, so we'll run this function again - intro()
Possible Answers
At the top of our program, after the import, we'll want to make a few lists of what answers match as A, B, C, etc.
Here we create a variable named answer_A, answer_B, etc, and set them equal to what the user could type to indicate that choice. A list starts with a bracket [, has each item in "quotations", separated by a comma ,.
Required Text
Next we'll create a variable that has the instructions if they choose something we don't expect.
To do this we'll add required = (\nUse only A, B, or C\n")
Notice how the start and end of the string have \n in them? That starts a new line and can be helpful in adding some space to your text.
Creating another Passage/Function
Now we'll create another function for stopping the colonists. From before we decided to call this option_stop_colonists().
Open the passage in Twine.
Use the same pattern to create the function
Use the same pattern as before to create this function.
Remember, use comments to help make the code easier to understand.
We:
- Name the function
- Print the passage text
- Wait 1 second
- Print the options
- Ask the user for their choice
- Check if it's A
- Check if it's B
- Tell them to try again if they entered anything else.
Creating another passage/function
Let's do this again for each passage.
Here's the function for option_wait()
Here's what option_wait() would look like
Running the program
After we create all of our functions, we will run the starting function to start it all. Here that's just intro().
Running the Program
To run the program, in IDLE click Run then Run Module.
Save if you haven't yet
Save your code if you haven't yet by clicking OK and entering a name and choosing a location
Your program runs in the shell
Test your program by running it and choosing every option. Does it behave as you'd expect?
Full example code
Here's the full example code for your reference.