text adventure question

Ara Kooser ghashsnaga at gmail.com
Sat Dec 2 12:22:28 EST 2006


   I am working on a text adventure game for python to get back into
python programming. My version 0.1 used only functions so I could get
familiar with how those work. I want to move beyond that. I am not
sure what would be a good Python way of handling this.  I was
wondering if classes would help? What things should be included in the
main program?

A couple of my goals are:
1) Remove the rooms (or areas) from the main program and have them
called in as needed.
2) Remove NPC's and monsters from the main program and have them
called in as needed.
3) A way of keeping track of time passing in the game
4) Having the main character info stored somewhere else.

Below is pasted a section of my code. Each room is a function that is
later called. I included only one room to keep this short. Thanks you
for suggestions and your time.

Code:

#A sample text adventure game
#Version 0.1
#By Ara Kooser

import random
import sys
import string



#Using global variables for the character
#Want to use a function instead

stre = 9
con = 8
inte = 11
agl = 14
app = 10
mag = 6
sz = 9

hp = 17

reputation = 0

stealth = False

quest1 = False
quest2 = False

cruse = False
poison = False
diseased = False

ducats = 50
lira = 25
florin = 80


equipment = {'Sword': 1, 'Dagger': 1, 'Walking staff': 1, 'Leather Armor':1}
backpack = {'Flint and steel': 1, 'Rations': 7, 'dressing kit': 6,
'waterskin': 2}
belt_pouch = {}

#####################################################################################

day = False

###   Global variables for items   ###

#grass blades in meadow_1
getGrass_m1 = 0
#mushroom in edge_forest1
getMushroom_ef1 = 0
#orc in forest2
aliveOrc = 0



#####################################################################################

# help function that will give you a list of commands
def help():
    print "look, examine (object), n, w, e, s, take (item)"
    print "climb, stealth, fishing, herbalism, forage, haggle"
    print "field dressing"
    print "wield (object), attack, flee, close, withdraw, maintain"
    print "backpack, belt pouch, cs"
    print "Example: examine book, take ducats, attack orc"

def character_sheet():
    print """\
============================================================================
Name:                       Profession:
Social Class:               Race:

============================================================================
Strength
Constitution
Intelligence
Agility
Appearance
Magic
Size
============================================================================
Ducats:     Lira:       Florin:

Skills: Forage, Haggle, Stealth, Fishing, Herbalism, Climb, Sword, Staff,
Dagger, Field Dressing

Equipment: Backpack, Belt Pouch, Run of the Mill Sword, Dagger, Flint&Steel
1 week food, 2 waterskins, walking stick, dressing kit
============================================================================
"""



def start():
    print '''
        SAMPLE TEXT ADVENTURE V0.1
   You are the last person to leave the small village of Hommlet. The wooden
gate closes behind you and twilight reaches across the land. A dense mist
creeps up out of the ground, only to be kept at bay by the watchmens torches.
Somewhere deep in the woods lies the foul orcs you have tracked for several
days.

    '''

    print

def outside1():
    global hp
    global reputation
    print " Current Hit Points = ",hp
    print " Current Reputation = ",reputation
    print '''   You are outside the town gates. The dirt road heads
(N)orth to another
town several days away. The forest lies (E)ast and (W)est through the
meadows. The
rumors you heard in town describe the orcs as being to the west. The town's gate
is to the (S)outh but it is locked for the night.
Type 'help' for a full list of commands.'''
    print
    prompt_out1()

def out1_desc():
    print '''    The fog is growing denser as the sun sets on the meadows.
    The exits are (N)orth, (E)ast and (W)est.'''
    print
    prompt_out1()

def prompt_out1():
    global day
    prompt_o1 = raw_input("Type a command: ").lower()
    try:

        if prompt_o1 == "help":
            help()
            print
            prompt_out1()

        elif prompt_o1 == "cs":
            character_sheet()
            print
            prompt_out1()

        elif prompt_o1 == "status":
            print " Current Hit Points = ",hp
            print " Current Reputation = ",reputation
            prompt_out1()

        elif prompt_o1 == "backpack":
            print backpack
            prompt_out1()

        elif prompt_o1 == "belt pouch":
            print belt_pouch
            prompt_out1()

        elif prompt_o1 == "equipment":
            print equipment
            prompt_out1()

########################################################################################
        elif prompt_o1 == "examine fog":
            print "The fog seems natural enough for this time of year."
            print
            prompt_out1()

        elif prompt_o1 == "examine road":
            print '''    The road is a well travelled dirt road
winding many leagues'''
            print
            prompt_out1()

        elif prompt_o1 == "look":
            out1_desc()

#######################################################################################
        elif prompt_o1 == "w":
            meadow1()

        elif prompt_o1 == "e":
            meadow2()


        elif prompt_o1 == "s":
            #if day = False
                print "The town's gate is closed for the night"
                print
                prompt_out1()
            #elif
            #    town_1

        elif prompt_o1 == "n":
            n_road1()

######################################################################################
        elif prompt_o1 == "haggle":
            print "There is no one to haggle with here."
            promt_out1()

        elif prompt_o1 == "stealth":
            print "You try and be stealthy"
            prompt_out1()

        else:
            print "Please choose another command. That command is invalid"
            print
            prompt_out1()

    except ValueError:
        print "Please choose another command. That command is invalid"
        print
        prompt_out1()

#there are 5 more rooms that follow using functions

start()
outside1()


-- 
Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis
an sub cardine glacialis ursae.



More information about the Python-list mailing list