On 8/27/2010 9:42 PM, kevin hayes 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. I've been piecing together what is in the book with some examples I found online. My goal with this question is not to necessarily make the code I've written work, but to gain a fundamental understanding of the concepts. Can someone show me how to organize a text-based game with these things kept in mind: 1)Each room will be a function 2)Direction(N,S,E,W) 3)"Look at" 4)Pick Up 5)Quit 6)Accumulated Gold Coins 7) Find all the Gold 8)Win/Lose

aug dawg recommended classes, with which I agree, but that may be premature for you.

1) define the functions once, outside the while loop.

2) there is no need for global gold. global only makes sense inside a def.

3) do not reset gold to 0 at the start of each loop cycle. Do that before the loop.

4) write a help function to handle the help text rather than repeating the text multiple times.

5) use initial lower case for function and variable names. This is a convention that makes
     it easier to read each other's code as well as your own code.

6) check the spelling of wooley mamoth

7) while keepGoing == True: can be simplified to while keepGoing:

8) write a function to get a command. Have it do the uppercasing and checking for / processing of common commands such as HELP,

9) have the functions return text rather than printing it. Use triple quoted text. e.g.:

def Room1():
description = """You find yourself in a large room. Above you is a massive crystal
chandelier. In front of you is round stone fountain, spewing water."""

Then in the loop:
  print Room1().

This separates data from logic and paves the way to convert things to classes & dictionaries.


I'm looking for the simplest version of this as possible, because I'm having trouble with the concepts. I'm only showing my code to give you an idea where I'm at. However, I would really like someone to lay out a little code structure for me.

I'm using Python 2.6.5 on a Mac Version 10.4.11 and don't consider myself computer literate, so bear with my ignorance please. Also, don't feel like you have to correct my code...please just create your own example of code structure to get me on the right track.

Here is the start of my game:

"""FirstGame.py
First try at creating a text-based adventure game. Trying to learn the basics
of creating functions and using if and loops.
kevin: 8/27/10"""

keepGoing = True
while keepGoing == True:

    global gold
    gold = 0

    def RoomOne():
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."
    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()





_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


--
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to