Hi!

I just started poking around with Python recently, and I decided to do a text-adventure game project to practice some of the core concepts. Trying to think of ways to go about it, I stumbled on this DaniWeb post:

http://www.daniweb.com/forums/thread55140.html&highlight=python+text+rpg+tutorial

Gave me a lot of inspiration, and I took my main layout from that. Wonderful practice! So thanks to everyone who contributed to that thread. Hope you don't mind I borrowed some pieces of the code skeleton from there.

In any case, I'm about done with the adventure, but there are some minor (So far non-fatal) problems I've run into. The game is mostly playable, unless it has some errors I don't know about, which is more than likely.

I've sorted out most of the problems I've run into myself, but one thing I can't figure out is:
The "quit" option works just fine in the first few rooms. Then it starts screwing up a bit, looping once before quitting. I can't figure out where it starts to go screwy, except that it always messes up after you've entered the Fungal Garden. Seems like that's not always the case, though.

The looping becomes increasingly worse with each new room you go into when you try to quit from there. For example, if you try to quit in the sept, "Press Enter to Quit" loops once, but you can still quit. If you try to quit in, say, the last room of the game, you get an infinite loop.

I've been over the whole thing again and again, but I just can't find what the problem is.

I hope you guys will be willing to play-test / help me debug!

Any other comments on the code, suggestions on things to add or take out, and general advice would be greatly appreciated. Thanks!

Code can be found here: http://www.mediafire.com/?xfn1ngn1gdp

(Attempted) features include:
1) Linked puzzles and brain-teasers
2) Two tricky insta-kills
3) Exciting sorta-combat!
4) A womanizing, alcoholic protagonist
5) 11 rooms

Thanks for your help and patience.

Kendra

Blarg. Just found an error myself.

After you re-enter the bedroom, the exit directions disappear. They should read:

"You see exits leading (E)ast, (W)est and (S)outh."

It looks very nice. I guess I only have one comment at the moment, and that is that printing multiple lines of text is easier if you use triple quotes instead of lots of different print statements. For example:

print "You press one hand to your eyes and wave him away, but Ferrontius doe
s"
    print "not move to go. 'I'm sorry, my liege, but it's my duty to inform"
    print "you that you've been confined to your apartments. Your father's order
s,"
    print "I'm afraid.'"
    print

->

print """\
You press one hand to your eyes and wave him away, but Ferrontius does
not move to go. 'I'm sorry, my liege, but it's my duty to inform
you that you've been confined to your apartments. Your father's orders,
I'm afraid.'
"""

Just a thought. You could also read the descriptions from files, I guess, but that's not really necessary.

Good work, it's much more than I'd have the patience to do.

Thanks! I have the not-so-sneaking suspicion that the code could have been about half as long if I knew a little more.

For example, the prompts for every room are similar, but not the same. Anyone have any idea how I could have made a "prompt" class?

For example
First room:

Leogas' bedroom
def bedroom():
    global leogasHp
    print "Health: ", leogasHp
    if leogasLeftBedroom == 0:
        bed_Desc1()
    else:
        bed_Desc2()

# If you haven't left the room yet, this description will trigger
def bed_Desc1():
    print "You are standing in your own bedroom. In the corner is a preposterously"
    print "large, opulent four-poster bed. Empty wine bottles litter the floor and"
    print "the top of the dresser. Discarded clothing lays strewn about the rich"
    print "quarters. A faint, directionless light permeates the room."
    print "You see exits leading (E)ast, (W)est and (S)outh."
    print "Type 'help' for a list of commands."
    print
    prompt_bed()

# If you've already left the room, and are coming back, this description will trigger
def bed_Desc2():
    print "You are standing in your own bedroom. While you were out, it seems the"
    print "servants have inconspicuosly tidied up. Your bed was painstakingly made, "
    print "and the floors and surfaces are free of clutter. Seems the maids aren't perfect,"
    print "however, as one last bottle lays half-hidden beneath the bed. A faint,"
    print "directionless light permeates the room."
    print "You see exits leading (E)ast, (W)est and (S)outh."
    print "Type 'help' for a list of commands."
    print
    prompt_bed()

#prompt for bedroom
def prompt_bed():
    global leogasLeftBedroom
    global inventory
    prompt_b = raw_input("Your orders, my liege? ").lower()
    try:
        if prompt_b == "e":
            leogasLeftBedroom = 1
            library()
        elif prompt_b == "w":
            leogasLeftBedroom = 1
            guardroom()
        elif prompt_b == "s":
            leogasLeftBedroom = 1
            fungalGarden()
        elif prompt_b == "help":
            help1()
            prompt_bed()
        elif prompt_b == "look":
            if leogasLeftBedroom == 0:
                bed_Desc1()
            else:
                bed_Desc2()
        elif prompt_b == "ex bed":
            if leogasLeftBedroom == 0:
                print "Your bed is a gigantic mess of slept-in silks and satins. Is that a"
                print "splash of vomit you see on the brocade pillows?"
            else:
                print "Your voluminous bed, with its carved mahogany posts, has been"
                print "meticulously made."
            print
            prompt_bed()

#You get a potion later. This ensures you can drink it while in the bedroom.

        elif prompt_b == "drink potion" or prompt_b == "drink healing potion":
            if "a healing potion" in inventory:
                if leogasHp < 35:
                    leogasHp = 35
                    print "You've been healed! Your health is now ", leogasHp
                    inventory.remove("a healing potion")
                else:
                    print "You really ought to save that for when it counts."
                print
                prompt_bed()
            else:
                print "You don't have a healing potion."
            print
            prompt_bed()
        elif prompt_b == "ex clothes":
            if leogasLeftBedroom == 0:
                print "Your fine clothes lay rumpled in scattered heaps across the room. They"
                print "smell like pipe smoke and are splotched with stains."
            else:
                print "Your clothes are hanging neatly in the closet."
            print
            prompt_bed()
        elif prompt_b == "ex bottles" or prompt_b == "ex bottle" or prompt_b == "ex wine":
            if leogasLeftBedroom == 0:
                print "Countless wine bottles of every shape and size lay around the room."
                print "Their emptiness is the only thing they have in common."
            else:
                print "The maids cleaned out all the bottles, except one, which you see poking"
                print "out from beneath the bed."
            print
            prompt_bed()
        elif prompt_b == "take bottles" or prompt_b == "take bottle" or prompt_b == "take wine":
            if "an empty wine bottle" in inventory:
                print "You're already carrying a wine bottle."
            else:
                print "You snatch up an empty bottle. Its pleasing heft comforts you."
                inventory += ["an empty wine bottle"]
                print "Your inventory now contains: "
                for item in inventory:
                    print item
            print
            prompt_bed()
        elif prompt_b == "take bed":
            print "It's much too heavy, and anyway, you're in a weakened state."
            print
            prompt_bed()
        elif prompt_b == "ex dresser":
            print "This stunning dresser features ironwork handles and a stunning walnut finish."
            print
            prompt_bed()
        elif prompt_b == "open dresser":
            print "It's full of dresses. Always wondered why they called it that."
            print
            prompt_bed()
        elif prompt_b == "take dresser":
            print "An adventurous spirit is appreciated, but c'mon."
            print
            prompt_bed()
        elif prompt_b == "take dresses":
            print "Oh, how the servants would talk."
            print
            prompt_bed()
        elif prompt_b == "take clothes":
            print "You ought to wait until the Clean Clothes Fairy comes and washes them, don't"
            print "you think?"
            print
            prompt_bed()
        elif prompt_b == "ex pillow" or prompt_b == "ex pillows":
            print "The pillows are crafted from the finest red velvet and exquisite brocade."
            print "They smell of last night's dinner."
            print
            prompt_bed()
        elif prompt_b == "take pillow" or prompt_b == "take pillows":
            print "Pillow fights are for sissies. Or for sultry bar wenches in lingerie."
            print "You are neither."
            print
            prompt_bed()
        elif prompt_b == "i":
            print "Your inventory contains: "
            for item in inventory:
                print item
            print
            prompt_bed()

#you get a spotted mushroom later in the game. If you eat it at any time, you die. This ensures you can eat it in the bedroom.

        elif prompt_b == "eat mushroom" or prompt_b == "eat mushrooms":
            if "a spotted mushroom" in inventory:
                print "Colors and lights start to swirl before your eyes. You feel a portal"
                print "to another world opening."
                die()
            else:
                print "You don't have a spotted mushroom."
                print
                prompt_bed()
        elif prompt_b == "q":
            quit()
        else:
            print "Pardon, my lord, I'm sure I don't know what you mean."
            print
            prompt_bed()
    except ValueError:
        print "Pardon, my lord, I'm sure I don't know what you mean."
        print
        prompt_bed()

Third room:

# If you haven't opened the secret door, this description will trigger
def lib_Desc1():
    print "You are standing in the library."
    print "An unusual, carved bookshelf packed full of musty tomes is set into the"
    print "northern wall. Fyrglyss lanterns, hung about at strategic locations, fill"
    print "the room with an unearthly glow."
    print "You see a single exit leading (W)est."
    print "Type 'help' for a list of commands."
    print
    prompt_lib()

# If you have opened the secret door, this description will trigger
def lib_Desc2():
    print "You are standing in the library."
    print "Before you stands a darkened doorway to outside."
    print "Fyrglyss lanterns, hung about at strategic locations, fill the room with"
    print "an unearthly glow."
    print "You see exits leading (W)est and (In)."
    print "Type 'help' for a list of commands."
    print
    prompt_lib()

# The library prompt
def prompt_lib():
    global inventory
    global gotLantern
    global bookOnShelf
    prompt_l = raw_input("Your orders, my liege? ").lower()
    try:
        if prompt_l == "w":
            bedroom()
        if prompt_l == "in":
            if bookOnShelf == 1:
                chamber()
            else:
                print "I'm sorry, my lord, I'm sure I don't know what you mean."
                print
                prompt_lib() 
        elif prompt_l == "help":
            help1()
            prompt_lib()
        elif prompt_l == "i":
            print "Your inventory contains: "
            for item in inventory:
                print item
            print
            prompt_lib()
        elif prompt_l == "look":
            if bookOnShelf == 0:
                lib_Desc1()
            else:
                lib_Desc2()
        elif prompt_l == "ex shelf" or prompt_l == "ex bookshelf" or prompt_l == "ex shelves" or prompt_l == "ex bookshelves":
            print "These old bookshelves contain all manner of volumes. You see your diary"
            print "and an atlas among the rest of the arcane texts. You notice that there's"
            print "an empty space where a book seems to be missing."
            print
            prompt_lib()
        elif prompt_l == "ex diary":
            print "'Dear Diary: Got drunk. Dear Diary: Got drunker.'"
            print
            prompt_lib()
        elif prompt_l == "take diary":
            print "Ugh, no thanks. Too depressing."
            print
            prompt_lib()
        elif prompt_l == "ex atlas":
            print "Maps of all the places you'll never see. Not if you stay locked up in"
            print "here, at any rate."
            print
            prompt_lib()
        elif prompt_l == "take atlas":
            print "Maybe on the way out, when it'll be of some use."
            print
            prompt_lib()
        elif prompt_l == "drink potion" or prompt_l == "drink healing potion":
            if "a healing potion" in inventory:
                if leogasHp < 35:
                    leogasHp = 35
                    print "You've been healed! Your health is now ", leogasHp
                    inventory.remove("a healing potion")
                else:
                    print "You really ought to save that for when it counts."
                print
                prompt_lib()
            else:
                print "You don't have a healing potion."
            print
            prompt_lib()
        elif prompt_l == "ex lantern" or prompt_l == "ex lanterns":
            print "These shimmering lanterns sparkle with an ethereal glow."
            print
            prompt_lib()
        elif prompt_l == "take lantern" or prompt_l == "take lanterns":
            if "a fyrglyss lantern" in inventory:
                print "You've already got a lantern."
            else:
                print "You take one of the lanterns down from its peg and hook it over your arm."
                gotLantern = 1
                inventory += ["a fyrglyss lantern"]
                print "Your inventory now contains:"
                for item in inventory:
                    print item
            print
            prompt_lib()
        elif prompt_l == "ex space" or prompt_l == "ex empty space":
            print "It's empty. There's nothing there. Totally blank. Full of zero. Packed with"
            print "nada."
            print
            prompt_lib()
        elif prompt_l == "take bookshelf" or prompt_l == "take shelf" or prompt_l == "take shelves" or prompt_l == "take bookshelves":
            print "This is no time to re-arrange the furniture."
            print
            prompt_lib()
        elif prompt_l == "put book on shelf" or prompt_l == "put book on bookshelf" or prompt_l == "put book on shelves" or prompt_l == "put strange book on shelf" or prompt_l == "put strange book on bookshelf" or prompt_l == "put strange book on shelves":
            if "a strange book" in inventory:
                print "You place the strange book in the empty space on the shelf."
                bookOnShelf = 1
                inventory.remove("a strange book")
                print "Suddenly you hear a great rumbling as the bookshelf slides away."
                print "Before you stands a darkened doorway and a flight of stairs leading down."
                print "You can feel fresh air wafting in through the opening."
                print "A way out!"
            else:
                print "You don't have a book in your possession."
            print
            prompt_lib()
        elif prompt_l == "q":
            quit()
#You can get a spotted mushroom later in the game. If you eat it at anytime, you die.
        elif prompt_l == "eat mushroom" or prompt_l == "eat mushrooms":
            if "a spotted mushroom" in inventory:
                print "Colors and lights start to swirl before your eyes. You feel a portal"
                print "to another world opening."
                die()
            else:
                print "You don't have a spotted mushroom."
                print
                prompt_lib()
        else:
            print "Pardon, my lord, I'm sure I don't know what you mean."
            print
            prompt_lib()
    except ValueError:
        print "Pardon, my lord, I'm sure I don't know what you mean."
        print
        prompt_lib()

Can anyone help shorten this up? Alternate coding solutions welcome. I'm curious how else this could be done, since I only really know one way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.