[Tutor] help, complete beginner please!

Alan Gauld alan.gauld at btinternet.com
Sat Aug 28 10:57:46 CEST 2010


"kevin hayes" <kevinosky at gmail.com> wrote

> Hi all! I'm trying to write a basic text adventure game to start 
> learning
> python (just for fun). I've gone through the first four chapters of 
> a "learn
> python game programming book" and I'm having trouble with the 
> exercise on
> writing a text adventure.

Sorry I don't know the book but...

>         I'm looking for the simplest version of this as possible, 
> because
> I'm having trouble with the concepts.

What is your programming background? Have you programmed in
any other language before or is Python your fist?

> an idea where I'm at. However, I would really like someone to lay 
> out a
> little code structure for me.

I think you are maybe trying to run too fast.

Its good practice in programming to build things up slowly, pece by 
piece.
In your case forget about the loop initially. Just write the 
individual functions
and test each one individually. Once you get the functions working on
their own you can start to build the structure to call them. [This 
approach
is known as bottom-up programming in case you are interested]

This approach is particularl;y powerful in a language like Python 
because
if you put the functions in a file you can import that file as a 
module
(have you covered modules yet?)

Then at the intreractive prompt(>>>) you can interactively test your
functions to check they work.

>
> keepGoing = True
> while keepGoing == True:
>
>    global gold

You don;t need this global is only used inside a function definition
so that the function can modify variables outside the function.
You are not inside a function at this point.

>    gold = 0
>
>    def RoomOne():

Here you are defining a function inside the while loop.
So you will redefine the function every time through the loop.
This is wasteful and not needed. Take the definition
outside the loop.

>        print "You find yourself in a large room. Above you is a 
> massive
> crystal"
>        print "chandelier. In front of you is round stone fountain, 
> spewing
> water"
>        print "from it's center. On the walls hang green satin 
> drapery.  The
> ceiling"
>        print "is comprised of three ornate arches. There is an arch 
> in
> front of you"
>        print "and to your right and left. A staircase leads up from 
> under
> the arch"
>        print "in front of you. Under the arches to your left and 
> right are
> large wooden"
>        print "doors, each with an iron handle. Enter 'Help' for a 
> full list
> of commands."

Use a triple quoted string to avoid multiple print statements.

>    RoomOne()
>    Command = raw_input("Please enter a command. ")
>    Command = Command.upper()
>    if Command == "N":
>        RoomFour()
>    elif Command == "S":
>        print "You ditched the creepy castle and headed for the 
> road."
>        keepGoing == False
>    elif Command == "E":
>        RoomTwo()
>    elif Command == "HELP":
>        print "List of Commands: 'Help',then enter for this list."
>        print                   "'N', then enter = Door to the 
> North."
>        print                   "'S', then enter = Door to the 
> South."
>        print                   "'E', then enter = Door to the East."
>        print                   "'W', then enter = Door to the West."
>        print                   "'Look at', then 'objects name', then 
> enter
> = Looks at an object."
>        print                   "'Pick Up', then 'objects name', then 
> enter
> = Picks up an object."
>        print                   "'Q', then enter = Quit Game."
>
>    elif Command == "W":
>        RoomSix()
>    elif Command == "LOOK AT FOUNTAIN":
>        print "There appears to be 4 gold coins in it."
>    elif Command == "PICK UP 4 GOLD COINS":
>        gold = gold + 4
>        print "Current Gold = ", gold
>    elif Command == "Q":
>        keepGoing = False
>
>    else:
>        print "That doesn't work."
>
>    def RoomTwo():
>        print "Current Gold = ", gold
>        print "In the middle of the room is a large Gargoyle with 
> fiery red
> eyes."
>        print "He's holding a cup. In the southeast corner of the 
> room you
> see a broom"
>        print "with dust and cob-webs on it. Next to the broom is a 
> dead
> rat."
>        print "To the north is another door. In front of the door is 
> an
> overturned basket."
>
>    promptTwo = raw_input("What are you going to do? ")
>    promptTwo = promptTwo.upper()
>
>    if promptTwo == "N":
>        RoomThree()
>    elif promptTwo == "S":
>        print "There is no door there."
>    elif promptTwo == "W":
>        RoomOne()
>    elif promptTwo == "E":
>        print "There is only a wall there."
>    elif promptTwo == "Q":
>        keepGoing = False
>    elif promptTwo == "PICK UP BASKET":
>        print "You pick up the basket, revealing 2 gold pieces."
>        RoomTwo()
>    elif promptTwo == "PICK UP 2 GOLD COINS":
>        gold = gold + 2
>        print "Current Gold = ", gold
>        RoomTwo()
>    elif promptTwo == "LOOK AT GARGOYLE":
>        print "Looking at the Gargoyle you notice he's really mean, 
> and
> really still."
>        RoomTwo()
>    elif promptTwo == "LOOK AT BROOM":
>        print "Looking at the broom, you notice it's unnecessarity 
> dirty."
>    elif promptTwo == "PICK UP BROOM":
>        print " You pick up the broom. But you don't notice anything
> special, so you"
>        print "set it back down where it was."
>    elif promptTwo == "LOOK AT CUP":
>        print "You look at the cup, and find nothing of interest."
>    elif promptTwo == "LOOK AT DEAD RAT":
>        print "You look at the dead rat and find it utterly 
> disgusting."
>    elif promptTwo == "HELP":
>        print "List of Commands: 'Help',then enter for this list."
>        print                   "'N', then enter = Door to the 
> North."
>        print                   "'S', then enter = Door to the 
> South."
>        print                   "'E', then enter = Door to the East."
>        print                   "'W', then enter = Door to the West."
>        print                   "'Look at', then 'objects name', then 
> enter
> = Looks at an object."
>        print                   "'Pick Up', then 'objects name', then 
> enter
> = Picks up an object."
>        print                   "'Q', then enter = Quit Game."
>    else:
>        print "That didn't work."
>
>
>    def RoomThree():
>        print "Current Gold = ", gold
>        print "To the north is a giant replica of a wooley mamoth. 
> His
> tusks are made"
>        print "of gold. To the east is a painting of Mother Theresa. 
> Under
> the painting"
>        print "is a baby's crib. The only doors you see are to the 
> south and
> west."
>
>    promptThree = raw_input("What would you like to do? ")
>    promptThree = promptThree.upper()
>
You could improve the generality of the code by building a data model
of your castle using a multi dimensional list.

Rooms = [[Room3, Room4,NotExist],
               [Room0,Room1,Room2]]

You can access your functions like:

Rooms[X][Y]()

Now you can define your movement commands as arithmetic operations
on the indices of the Rooms map, eg:

N increment the Y index
S decrement the Y index
E increment the X index
W decrement the X index

If you pass the direction value as a parameter the room functions can
check if a valid entrance exists on that side. So in Room3 you might 
have:

def Room3(dir):
    if dir == 'N':
        raise NoDoorError, "Cannot enter Room3 from the South"
     # rest of code here

Just some ideas.

HTH,
-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/







More information about the Tutor mailing list