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
# 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
# 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?")
if cmd == "n":
if locations[player1.loc].north == True:
player1.loc = player1.loc - 2
else:
print "you can't go that way"
elif cmd == "s":
if locations[player1.loc].south == True:
player1.loc = player1.loc + 2
else:
print "you can't go that way"
elif cmd == "e":
if locations[player1.loc].east == True:
player1.loc = player1.loc + 1
else:
print "you can't go that way"
elif cmd == "w":
if locations[player1.loc].west == True:
player1.loc = player1.loc - 1
else:
print "you can't go that way"
elif cmd == "get bag":
if locations[player1.loc].bag == 1:
print "You pick up the bag."
locations[player1.loc].bag == 0
player1.bag += 1
else:
print "I see no bag here..."
elif cmd == "get gold":
if locations[player1.loc].gold == 1 and player1.bag >= 1:
print "You pick up 1 gold."
locations[player1.loc].gold = 0
player1.gold += 1
print "You now have " + str(player1.gold) + " gold."
elif player1.bag < 1:
print "You have nothing to carry the gold with."
else:
print "I see no gold here..."
elif cmd == "q":
player1.stop = True
else:
print "I'm not sure I understand what you mean..."
# 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."
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:
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 "You've WON! You are truly a marvel! CONGRATULATIONS!"
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
