hello, i'm new to this website and well only have a few months of python exp. i decided to make a small, simple rather easy if HUMOR game. When is started, i did not know a whole lot other than how to make lists and scenes and make a way to get "a key" to "open doors" and hiding them under "lamps, beds, etc." I know a bit more from studying other peoples code. so here it is(Note it says version 2.0 because i this one has some slight "modifications" made to it, if you want, i could post version 1.0) :

# Title: All out of Cheese burgers, Version 2.0
# Genre: Humor/Text Adventure
# Date: November 16, 2009

import textwrap
import sys

# Intro text:
scenes = {
    "The Harry Hamburger": {
                     "description": "You are standing in a long line at the register. To the NORTH of you is the order window, " \
                                    "to the EAST of you is the kitchen, to the WEST of you is the MASCOT, and to the " \
                                    "the SOUTH of you is the walk-in freezer.",

                     "paths": [
                         { "go_to": "order window", "phrase": "Go north to the order window" },
                         { "go_to": "kitchen",      "phrase": "Go east to the kitchen" },
                         { "go_to": "MASCOT",       "phrase": "Go south to the MASCOT" },
                         { "go_to": "walkin freezer", "phrase": "Go west to the walk-in freezer" }

                    ]
    },
    "walkin freezer": {
        "description": "You are standing at the door of the walk in freezer, which is locked. " \
        "You'll need to find a way to open it. Perhaps a key is somewhere in the restraunt.",

                    "paths": [
                        { "go_to": "kitchen", "phrase": "Go east to the kitchen" },
                        { "go_to": "MASCOT", "phrase": "Go north to the mascot" }


                    ]
                },
                "order window": {
                    "description":  "You finally reach the order window at last. The fast food clerk " \
                                    "tells you that they are all out of cheese burgers. None of the other " \
                                    "items on the menu interest you. You leave the building in disappointment.",
                    "paths": [ ]
                },
                "kitchen": {
                    "description": "Sneaking in to the kitchen, you can't believe your eyes. Melted cheese on top of " \
                                   "of sesame seed buns fill your nostrils and you almost could began to drool. You want to " \
                                   "avoid detection as your goal is the walk-in freezer so you refrain from stealing anything.",
                    "paths": [
                        { "go_to": "MASCOT",      "phrase": "Go west to the MASCOT" }
                    ]

                },
                "MASCOT": {
                    "description": "The Harry Hamburger mascot stands off to the side of the order window and is handing " \
                                   "out free coupons to passer bys and on his side is a key. You quickly grab his key before anyone notices " \
                                   "and you than go to the locked door and open the walk-in freezer. CONGRATS, YOU WIN!",
                    "paths": [ ]
                }
}





scene = scenes["The Harry Hamburger"]

while 1 == 1:
    next_step = None
    description = scene["description"]
    paths = scene["paths"]

    print textwrap.fill(description)

    for i in range(0, len(paths)):
        path = paths[i]
        menu_item = i + 1
        print "\t", menu_item, path["phrase"]

    print "\t(0 Quit)"

    prompt = "Make a selection (0 - %i): " % len(paths)

    while next_step == None:
        try:
            choice = raw_input(prompt)
            menu_selection = int(choice)

            if menu_selection == 0:
                next_step = "quit"
            else:
                index = menu_selection - 1
                next_step = paths[ index ]
        except (IndexError, ValueError):
            print choice, "Unknown Command!"

    if next_step == "quit":
        print "I hope you had fun, Bye!"
        sys.exit()
    else:
        scene = scenes[next_step['go_to'] ]
        print "You decided to:", next_step["phrase"]

Recommended Answers

All 9 Replies

Well you could start by using code tags when posting source code.

sry, i did not know i had to do that i will tag them now.

It just saves the formatting and makes it more easy to read with highlighting. I will go threw your program and find some tips soon.

# Title: All out of Cheese burgers, Version 2.0
# Genre: Humor/Text Adventure
# Date: November 16, 2009

import textwrap
import sys

# Intro text:
scenes = {
    "The Harry Hamburger": {
                     "description": "You are standing in a long line at the register. To the NORTH of you is the order window, " \
                                    "to the EAST of you is the kitchen, to the WEST of you is the MASCOT, and to the " \
                                    "the SOUTH of you is the walk-in freezer.",

                     "paths": [
                         { "go_to": "order window", "phrase": "Go north to the order window" },
                         { "go_to": "kitchen",      "phrase": "Go east to the kitchen" },
                         { "go_to": "MASCOT",       "phrase": "Go south to the MASCOT" },
                         { "go_to": "walkin freezer", "phrase": "Go west to the walk-in freezer" }

                    ]
    },
    "walkin freezer": {
        "description": "You are standing at the door of the walk in freezer, which is locked. " \
        "You'll need to find a way to open it. Perhaps a key is somewhere in the restraunt.",

                    "paths": [
                        { "go_to": "kitchen", "phrase": "Go east to the kitchen" },
                        { "go_to": "MASCOT", "phrase": "Go south to the mascot" }


                    ]
                },
                "order window": {
                    "description":  "You finally reach the order window at last. The fast food clerk " \
                                    "tells you that they are all out of cheese burgers. None of the other " \
                                    "items on the menu interest you. You leave the building in disappointment.",
                    "paths": [ ]
                },
                "kitchen": {
                    "description": "Sneaking in to the kitchen, you can't believe your eyes. Melted cheese on top of " \
                                   "of sesame seed buns fill your nostrils and you almost could began to drool. You want to " \
                                   "avoid detection as your goal is the walk-in freezer so you refrain from stealing anything.",
                    "paths": [
                        { "go_to": "MASCOT",      "phrase": "Go north to the MASCOT" }

                    ]


                },
                "MASCOT": {
                    "description": "The Harry Hamburger mascot stands off to the side of the order window and is handing " \
                                   "out free coupons to passer bys and on his side is a key. You quickly grab his key before anyone notices " \
                                   "and you than go to the locked door and open the walk-in freezer. CONGRATS, YOU WIN!",
                    "paths": [ ]
                }
}





scene = scenes["The Harry Hamburger"]

while 1 == 1:
    next_step = None
    description = scene["description"]
    paths = scene["paths"]

    print textwrap.fill(description)

    for i in range(0, len(paths)):
        path = paths[i]
        menu_item = i + 1
        print "\t", menu_item, path["phrase"]

    print "\t(0 Quit)"

    prompt = "Make a selection (0 - %i): " % len(paths)

    while next_step == None:
        try:
            choice = raw_input(prompt)
            menu_selection = int(choice)

            if menu_selection == 0:
                next_step = "quit"
            else:
                index = menu_selection - 1
                next_step = paths[ index ]
        except (IndexError, ValueError):
            print choice, "Unknown Command!"

    if next_step == "quit":
        print "I hope you had fun, Bye!"
        sys.exit()
    else:
        scene = scenes[next_step['go_to'] ]
        print "You decided to:", next_step["phrase"] [CODE][/CODE]

It just saves the formatting and makes it more easy to read with highlighting. I will go threw your program and find some tips soon.

did i format it right? do i highlight code and click the (code) symbol, or use the (code) symbol from the first part of the code to the end such as

code began

code end

[C.O.D.E=Python]
def Myfunc():
print "Stuff"
[/C.O.D.E]

With out dots just copy and paste your code in between the code tags.

def Myfunc():
print "Stuff"
# Title: All out of Cheese burgers, Version 2.0
# Genre: Humor/Text Adventure
# Date: November 16, 2009

import textwrap
import sys

# Intro text:
scenes = {
    "The Harry Hamburger": {
                     "description": "You are standing in a long line at the register. To the NORTH of you is the order window, " \
                                    "to the EAST of you is the kitchen, to the WEST of you is the MASCOT, and to the " \
                                    "the SOUTH of you is the walk-in freezer.",
                                    
                     "paths": [
                         { "go_to": "order window", "phrase": "Go north to the order window" },
                         { "go_to": "kitchen",      "phrase": "Go east to the kitchen" },
                         { "go_to": "MASCOT",       "phrase": "Go south to the MASCOT" },
                         { "go_to": "walkin freezer", "phrase": "Go west to the walk-in freezer" }

                    ]
    },
    "walkin freezer": {
        "description": "You are standing at the door of the walk in freezer, which is locked. " \
        "You'll need to find a way to open it. Perhaps a key is somewhere in the restraunt.",

                    "paths": [
                        { "go_to": "kitchen", "phrase": "Go east to the kitchen" },
                        { "go_to": "MASCOT", "phrase": "Go south to the mascot" }
                       
                         
                    ]
                },
                "order window": {
                    "description":  "You finally reach the order window at last. The fast food clerk " \
                                    "tells you that they are all out of cheese burgers. None of the other " \
                                    "items on the menu interest you. You leave the building in disappointment.",
                    "paths": [ ]
                },
                "kitchen": {
                    "description": "Sneaking in to the kitchen, you can't believe your eyes. Melted cheese on top of " \
                                   "of sesame seed buns fill your nostrils and you almost could began to drool. You want to " \
                                   "avoid detection as your goal is the walk-in freezer so you refrain from stealing anything.",
                    "paths": [
                        { "go_to": "MASCOT",      "phrase": "Go north to the MASCOT" }
                        
                    ]
                                       
                
                },
                "MASCOT": {
                    "description": "The Harry Hamburger mascot stands off to the side of the order window and is handing " \
                                   "out free coupons to passer bys and on his side is a key. You quickly grab his key before anyone notices " \
                                   "and you than go to the locked door and open the walk-in freezer. CONGRATS, YOU WIN!",
                    "paths": [ ]
                }
}



            
             
scene = scenes["The Harry Hamburger"]

while 1 == 1:
    next_step = None
    description = scene["description"]
    paths = scene["paths"]

    print textwrap.fill(description)

    for i in range(0, len(paths)):
        path = paths[i]
        menu_item = i + 1
        print "\t", menu_item, path["phrase"]

    print "\t(0 Quit)"

    prompt = "Make a selection (0 - %i): " % len(paths)

    while next_step == None:
        try:
            choice = raw_input(prompt)
            menu_selection = int(choice)

            if menu_selection == 0:
                next_step = "quit"
            else:
                index = menu_selection - 1
                next_step = paths[ index ]
        except (IndexError, ValueError):
            print choice, "Unknown Command!"

    if next_step == "quit":
        print "I hope you had fun, Bye!"
        sys.exit()
    else:
        scene = scenes[next_step['go_to'] ]
        print "You decided to:", next_step["phrase"]

Here the code is again, this time formatted. thanks for telling me how to do that.

You didn't have to make it so complicated with the dictionary. A bunch of if statements. Would've done fine.

thanks. i like looking at others code like a tututorial to get a better idea of how to make the code "good". i found out you can define "scenes" and use if, else, and elif functions to make the directions the player can go, to print the intro, to print the scenes, etc. you dont have to use a dictionary, but i guess i just like having a # list so the player just types a # in to go to the next scene. if you played my game u will see that. is there away to make a list of directions you can go WITHOUT using a complicated dict func??? much appreciated.

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.