Critique of Coding for Text Adventure

jdwright

Senior member
May 18, 2000
208
0
0
I haven't had much formal instruction in object oriented programming and thought I'd post this to see if I was even in the ballpark of implementing it correctly. Please keep in mind that this is just a skeleton of a complete bit of code. Any constructive suggestions are very welcome and thanks in advance for your time =).

# Text Adventure by jwright

import sys

# set up the player with any attributes that might come up in the game: gold, sword, bag, etc.
class player(object):
"object to define the current player"
loc = 3
gold = 0
bag = 0
stop = False

# set up the room object with any objects that might be present, a description and name, and the possible exits.
class room(object):
"object to define each room on the map"
north = True
south = True
west = True
east = True
des = "description"
name = "name"
gold = 1
bag = 0

# quick function to describe the current location
def describe():
"runs to show a description of the location"
print locations[player1.loc].des
print

# function to evaluate the commands that the user enters
def command():
"runs to get and evaluate commands from user"
cmd = raw_input("What would you like to do next?")
print
if cmd == "n":
if locations[player1.loc].north == True:
player1.loc = player1.loc - 2
else:
print "you can't go that way"
print
elif cmd == "s":
if locations[player1.loc].south == True:
player1.loc = player1.loc + 2
else:
print "you can't go that way"
print
elif cmd == "e":
if locations[player1.loc].east == True:
player1.loc = player1.loc + 1
else:
print "you can't go that way"
print
elif cmd == "w":
if locations[player1.loc].west == True:
player1.loc = player1.loc - 1
else:
print "you can't go that way"
print
elif cmd == "get bag":
if locations[player1.loc].bag == 1:
print "You pick up the bag."
print
locations[player1.loc].bag == 0
player1.bag += 1
else:
print "I see no bag here..."
print
elif cmd == "get gold":
if locations[player1.loc].gold == 1 and player1.bag >= 1:
print "You pick up 1 gold."
print
locations[player1.loc].gold = 0
player1.gold += 1
print "You now have " + str(player1.gold) + " gold."
print
elif player1.bag < 1:
print "You have nothing to carry the gold with."
print
else:
print "I see no gold here..."
print
elif cmd == "q":
player1.stop = True
else:
print "I'm not sure I understand what you mean..."
print

# function to prepare all the rooms with descriptions, contents and exits
def prep():
"runs to setup all the rooms"
print "This is text adventure. You need 4 gold pieces to win. The commands available to you are n,s,e,and w for directions; get <object> for get an object and q to quit."
print
for x in 0,1,2,3:
locations[x]=room()
locations[0].north = False
locations[0].west = False
locations[0].des = "You're in a plain room. There is an exit to the south and east. There is a piece of gold on the floor. There is a bag on the floor."
locations[0].bag = 1
locations[1].east = False
locations[1].north = False
locations[1].des = "You're in a plain room. There is an exit to the south and west. There is a piece of gold on the floor."
locations[2].west = False
locations[2].south = False
locations[2].des = "You're in a plain room. There is an exit to the south and west. There is a piece of gold on the floor."
locations[3].east = False
locations[3].south = False
locations[3].des = "You're in a plain room. There is an exit to the south and west. There is a piece of gold on the floor."

# function to see if the player wants to quit completely or restart the game
def checkquit():
if player1.stop == True:
print
again = raw_input ("Would you like to start again? (y/n):")
if again == "n":
sys.exit()
else:
pass

# function to see if the player has won
def checkwin():
if player1.gold == 4:
print
print "You've WON! You are truly a marvel! CONGRATULATIONS!"
print
again = raw_input ("Would you like to start again? (y/n):")
if again == "n":
sys.exit()
else:
pass

# Start Main Loop
while 1:
locations = [0,1,2,3]
player1 = player()
prep()
while player1.stop != True:
describe()
command()
checkwin()
checkquit()

Again, thanks for any suggestions that you might offer. I know it's very basic right now, but I'm hoping that it will be a place to build from.

J
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
I'd probably use non-zero "room IDs" and for the array of rooms have them be this way ( 0 = no exit)

class room(object):
"object to define each room on the map"
room_id = 10
north = 12
south = 14
west = 0
east = 0
des = "description"
name = "name"
gold = 1
bag = 0

This lets you attach non-adjacent rooms (teleport, mazes) and add new rooms in any order.

Back in the BASIC days we used DATA tables to embed the room data into the program, the modern way would be with an external XML file.

That lets you keep the engine separate from the data, and use a different editor (XML or word processor) to create content.
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
Good start.
You'll probably want to look into an inventory list instead of dedicated fields for each item type. You could use tuples to associate size/count with type.
The addition of triggers (functions which can alter world state) is useful - for example, leaving a room through an exit might collapse the previous room, etc.
Also, investigate generating the description field from the room contents. For example (psuedo code):
def roomDescription( room ):
. rv = "You are in a plain room. "
. exits = ""
. if ( room.West != False ) exits += "West"
. ....
. contents = ""
. if ( room.bag ) contents +=
. ...
. return rv + exits + contents
 

jdwright

Senior member
May 18, 2000
208
0
0
I really like both suggestions:

Dave: Can you send me in the direction of a tutorial for including an XML file that I can access as a resource? Or possibly a library that would work within python? Any help would be appreciated greatly! As I was building I was thinking to myself how nice it would be to just access a database of this info.

mundane: I really like that idea - I had been wondering how I might get around the description problem when an item that was there initially was removed. This should do the trick. I'll be implementing it =).

Thanks a ton for the info guys!

J
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
I mostly use Visual C++ with win32 & MFC at work, so you'll have to ask the All Knowing Google about python XML support.