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:

<span style="color: #ff9900;">import</span> 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:

<span style="color: #ff9900;">def</span> clearscreen():<br /> <span style="color: #ff9900;">if</span> os.name == <span style="color: #1fa22f;">"posix"</span>:<br /> <span style="color: #ff0000;"># Unix/Linux/MacOS/BSD/etc</span><br /> os.system(<span style="color: #1fa22f;">'clear'</span>)<br /> <span style="color: #ff9900;">elif</span> os.name <span style="color: #ff9900;">in</span> (<span style="color: #1fa22f;">"nt", "dos", "ce"</span>):<br /> <span style="color: #ff0000;"># DOS/Windows</span><br /> os.system(<span style="color: #1fa22f;">'CLS'</span>)

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

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

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. 😉

<span style="color: #ff9900;">def</span> inspect(choice,location):<br /> <span style="color: #ff9900;"> if</span> choice == location:<br /> <span style="color: #a37acc;">print </span><span style="color: #1fa22f;">""</span><br /> <span style="color: #a37acc;">print</span> <span style="color: #1fa22f;">"You found a key!"</span><br /> <span style="color: #a37acc;">print</span> <span style="color: #1fa22f;">""</span><br /> <span style="color: #ff9900;">return </span>1<br /> <span style="color: #ff9900;"> else<span style="color: #000000;">:</span></span><br /> <span style="color: #a37acc;">print</span> <span style="color: #1fa22f;">""</span><br /> <span style="color: #ff0000;"># Clear the screen so the menu won't be duplicated.</span><br /> clearscreen()<br /> <span style="color: #a37acc;">print</span> <span style="color: #1fa22f;">"Nothing of interest here."</span><br /> <span style="color: #a37acc;"> print</span> <span style="color: #1fa22f;">""</span><br /> <span style="color: #ff9900;">return</span> 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 = [<span style="color: #1fa22f;">"pot plant","small case","vase","shoe"</span>]

keylocation = 2<br /> keyfound = 0<br /> 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 "“

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. 🙂