Python Text Adventure Game: Simple Interaction
In this post I will show you a very primitive interaction script and combine it with the text displaying script.
First of all you have to import os:
You need this for the “clearscreen” function. As the name says, it “clears” the screen so you can focus on new text.
This is the script:
def clearscreen(): if os.name == "posix": # Unix/Linux/MacOS/BSD/etc os.system('clear') elif os.name in ("nt", "dos", "ce"): # DOS/Windows os.system('CLS')
The first thing you do here is defining a function with def. This is so you don’t have to write the whole script everytime. As there are different commands for clear depending on the operating system, you check what os you are dealing with and then use the related command.
You should also define a function for the writing script. It should look like this:
import time import sys from random import randrange def writetext(text): for c in text: sys.stdout.write(c) sys.stdout.flush() seconds = "0.0" + str(randrange(5, 9, 1)) seconds = float(seconds) time.sleep(seconds)
If you want to learn more about functions, have a look at this page: http://docs.python.org/tutorial/controlflow.html#defining-functions
Now it’s time for some more functions. 🙂
In this example, our character is in a locked room and searches for the key. Let’s start with the function for the menu:
def menu(list, question): for entry in list: print 1 + list.index(entry), print ") " + entry return input(question) - 1
The list will contain all places we want to search for the key to open the door. This function shows our menu with all the places from our list.
Just one more function now. 😉
def inspect(choice,location): if choice == location: print "" print "You found a key!" print "" return 1 else: print "" # Clear the screen so the menu won't be duplicated. clearscreen() print "Nothing of interest here." print "" return 0
This was the inspect function. It defines what happens if we select a place from the menu. If our choice is the right place for the key, it will display “You found a key!”. If it was the wrong place, the screen will be cleared and the menu written again.
Now define the items and set some variables:
items = ["pot plant","small case","vase","shoe"] keylocation = 2 keyfound = 0 loop = 1
Items are the places to look for the key.
Keylocation is where the key is, in our case it’s the vase. (It starts at 0)
Keyfound is whether you have found the key already or not and loop is the variable to loop the whole game until we found the key.
Now you can execute the game:
print "" # Write the introduction text writetext(text) print "" #Get your menu working, and the program running until you find the key while loop == 1: keyfound = inspect(menu(items,"What do you want to inspect? "),keylocation) if keyfound == 1: writetext("You put the key in the lock of the door, and turn it. It opens!" loop = 0
Here is the whole Code: pytag.zip
I got a lot of stuff in this post from this site: http://www.sthurlow.com/python/lesson07/
It has nice tutorials and may also help you. 🙂