[Tutor] A Text Adventure Problem
bob gailer
bgailer at gmail.com
Sun Mar 24 23:03:05 CET 2013
On 3/24/2013 3:41 PM, John Bochicchio wrote:
> I've been trying to write a simple test text adventure
[snip]
Good first try! As you learn OOP and use of classes you will discover
ways to improve and simplify coding.
Here's an example. It is far from perfect, but definitely worth a study.
It will make text adventures easier to create. Note that this separates
logic from data. Most room definitions have only data and the Room class
has the common logic.
from sys import exit
from random import randint
class Room: # superclass of rooms
actions = dict()
def __init__(self):
self.prompt = ', '.join(self.actions.keys())
def enter(self):
print self.enterMsg
if self.prompt:
while True:
action = raw_input(self.prompt + ">").lower()
next = self.actions.get(action,None)
if next is None:
print "Not a valid command"
else:
print next[0]
return next[1]
else:
exit(1)
class Shack_front(Room):
enterMsg = "The year is 2062, and the zombies have risen. You're not
a doctor, and you have no training. You're just a guy trying to survive.\n"
enterMsg += "......" * 8 + "\n"
enterMsg += "You stand in front of a broken down shack, you here
gurgles and screams from inside. There is a window to the side of the
house "
enterMsg += "that you can see through, or a door you can burst into.\n"
enterMsg += "......" * 8 + "\n"
actions = dict(
window = ("You silently walk over to the dirty window. You look
inside to see a man held captive by 2 marauders. "
"The marauders are armed with bats, and don't seem to know what
they're doing. You have 1 9mm glock with 12 bullets.", 'window'),
door = ("You burst through the door with your shoulder. You see
only one marauder. You shoot him in the chest and notice a man tied up "
"in a chair. You walk over to him but feel a sharp pain in the back
of your head. You never saw the second marauder coming.", 'death'))
class Shack_window(Room):
enterMsg = "Do you burst through the door or try to fire through the
window at the marauders?"
actions = dict(
window = ("You fire off three shots through the window. Two in the
chest of the first marauder, one in the neck of the other. "
"They fall to the ground, dead. You walk inside to see if
the man is okay.", 'window'),
door = ("You burst through the door and fire two shots into the
first marauder. You remember seeing two, so you turn around and "
"put one bullet through the head of the one behind you. You
walk over to the man to see if he's okay.", 'death'))
class Death(Room):
quips = ["You've become a zombie and eat you pals. Douche.",
"You've died and left your comrades to fend for themselves.
Douche",
"Your whole group dies because of you. Douche.",
"You've got terrible planning skills, and ruined the teams
chances of survival. Douche."]
enterMsg = quips[randint(0, len(quips)-1)]
actions = dict()
rooms = dict(shack_front = Shack_front(), death = Death(), window =
Shack_window())
next = 'shack_front'
while True:
next = rooms[next].enter()
--
Bob Gailer
919-636-4239
Chapel Hill NC
More information about the Tutor
mailing list