Can you help me with something? This code (it also draws from the text_game
file) says it has a syntax error, but I can't seem to find what it is, I think
the code is having a fit but I'm not sure. I'm appreciative to all hep.
# Josh Wilkerson's Project 1 Part 3
# 2/25/2013
# Basic layout of text-based game
# Stayin' Alive
import text_game
class Game(text_game.GamePlay):
"""Stayin' Alive, an adventure."""
def __init__(self):
# game locations
bedroom = text_game.Location("Bedroom", "Looks a hot mess, and why
won't \
that alarm clock radio shut up? The radio is going on about some meteor \
shower that should be prettifying the sky soon. You almost smack it off the \
night stand trying to turn it off.")
bedroomview = text_game.Location("Bedroom", "You sit up, streth, yawn, \
and look around. You're going to be late anyways, so who cares? Watching your \
step as you get out of bed, you head to the bathroom to at least look \
decent.")
backinbedroom = textgame.Location("Bedroom again?", "Why are you back \
in this slop heap? Don't you have things to do?")
bedbegone = text_game.Location("Bedroom still", "You decide to hit \
the good ole snooze several times, end up flinging the alarm clock at the \
wall, and wake up four hours later to find a bulldozer preparing to level your \
house due to an as before unknown bypass. I think we all know where this is \
going...")
bathroom1 = text_game.Location("Bathroom", "Your about to be late for
work \
better hurry... but disretion is always advised. It is always good to be
clean.")
bathroom2 = "Bathroom", "If you really must, but hurry if you value \
your job...")
downstairs1 = text_game.Location("Downstairs", "You rush downstairs, \
You are pretty hungry, but is your job worth it?.")
downstairs2 = text_game.Location("Downstairs", "You walk down the \
stairs and look about your sparse eating area. Guess it's time to eat... \
can't cook though.")
# car is a room per se, but instead of leaving it, the directions
# move you along with the car.(many incarnations of car at each spot?)
car = text_game.Location("Car", "Your car has scattered derbis
everywhere.")
work = text_game.Location("Work", "You work here, but you don't really
\ like it. Crap desk job.")
office = text_game.Location("Office", "Ahhh, home away from home... \
if your parents abused you verbally at home. It could use a bit of neatening \
up, there are several scattered objects on the floor.")
# car will be used again... maybe a car 2?
# Home too.
huh = text_game.Location("?", "What the...?")
dead1 = text_game.Location("You Died", "You hop up, freaking out about
your lateness, step on some \
random piece of junk, and hit your head just right on your night stand. It's \
gonna be a fun day. \
\nTry again.")
dead2 = text_game.Location("You Died, well... kinda", "You don't even \
open your eyes just rub them and roll over, trying to go back to sleep. \
Unfortunately for you and the countless millions (of atoms) that depend on \
you, you land funny on an as yet unknown object and are now a quadraplegic. \
\nWhat a shame.")
dead3 = text_game.Location( "Funny story...", "He finds you as \
expected, butt to mud, but this time around he actually passed out from all \
of those drinks. \nOpps.")
# add exits
bedroom.add_exit(text_game.Location.SIT_UP_GET_UP, bedroomview)
bedroom.add_exit(text_game.Location.ROLL_OUT, dead2)
bedroom.add_exit(text_game.Location.HURRY_UP, dead1)
bedroom.add_exit(text_game.Location.STAY_PUT, bedbegone)
bedroomview.add_exit(text_game.Location.BACK_TO_BED, sleep)
bedroomview.add_exit(text_game.Location.CLEAN_UP, bathroom1)
bedroomview.add_exit(text_game.Location.NO_TIME, downstairs1)
bedbegone.add_exit(text_game.Location.FIND_FORD, dead3)
downstairs1.add_exit(text_game.Location.
dead1.add_exit(text_game.Location.REVIVE, bedroom)
dead2.add_exit(text_game.Location.REVIVE, bedroom)
dead3.add_exit(text_game.Location.REVIVE, bedroom)
huh.add_exit(text_game.Location.WHAT, bedroom)
#add a list of possible locations to the list
self.locations = []
self.locations.append(bedroom)
self.locations.append(bedroomview)
self.locations.append(backinbedroom)
self.locations.append(bedbegone)
self.locations.append(bathroom1)
self.locations.append(bathroom2)
self.locations.append(downstairs1)
self.locations.append(car)
self.locations.append(work)
self.locations.append(office)
self.locations.append(huh)
# put the player in the first room to start
self.player = text_game.Player(bedroom)
def prolog():
print \
"""
Just an orindary day in the life of you, or so it seems, you wake up to your
alarm clock going off, it'll all go downhill from here.
"""
def to_win():
print \
"""
The point of this day in your life is to live, easier said then done, believe
it or not...
Pay attention to everything, it just might save your life...
"""
def describe():
print \
"""
The house you live in is pretty old, and the wiring in several rooms, including
\
the living room, bathroom, and dining room, could use a pick me up. Not the \
neatest person ever, things are strewn about the house, with seemingly no \
order about the place...
"""
def main():
print "Welcome to Stayin' Alive!"
prolog()
to_win()
describe()
adv = Game()
adv.play("Hope you're ready...")
main()
raw_input("Press any key to exit.")
# Simple Adventure
# The beginnings of a text adventure game
# Modified by Paul Sbraccia from work initially developed by M.Dawson
class Entity(object):
""" Base from which all others game objects are derived """
def __init__(self, name, desc = None):
self.name = name
self.desc = desc
def __str__(self):
rep = self.name.upper()
if self.desc:
rep += " - " + self.desc
return rep
class Location(Entity):
""" A place where the player can be """
WHAT = "what"
HURRY_UP = "hurryup"
REVIVE = "revive"
SIT_UP_GET_UP = "situpgetup"
ROLL_OUT = "rollout"
STAY_PUT = "stayput"
FIND_FORD = "findford"
BACK_TO_BED = "backtobed"
CLEAN_UP = "cleanup"
NO_TIME = "notime"
DIRECTIONS = (WHAT, HURRY_UP, SIT_UP_GET_UP, REVIVE, ROLL_OUT, STAY_PUT, \
FIND_FORD, BACK_TO_BED, CLEAN_UP, NO_TIME)
def __init__(self, name, desc = None):
super(Location, self).__init__(name, desc)
self.exits = {}
def __str__(self):
rep = super(Location, self).__str__()
rep += "\nAvailable exits: " + str(self.exits.keys())
return rep
def add_exit(self, direction, location):
self.exits[direction] = location
def leads_to(self, direction):
return self.exits.get(direction)
class Player(object):
""" A main character that can be in a location """
def __init__(self, location):
self.location = location
def display_inventory(self):
print "You carry:\n", self.inventory
def look(self, item_name):
item = self.inventory.name_to_item(item_name) or \
self.location.contents.name_to_item(item_name)
if item:
print "You see:\n", item
else:
print "You don't see anything like that here."
def look_location(self):
print "You're in:\n", self.location
def move(self, direction):
new_location = self.location.leads_to(direction)
if new_location:
self.location = new_location
print "You enter:", new_location
else:
print "You can't go that way."
def get(self, item_name):
item = self.location.contents.name_to_item(item_name)
if item:
print "You get it."
self.inventory.add_item(item)
self.location.contents.remove_item(item)
else:
print "That's not something you can get."
def drop(self, item_name):
item = self.inventory.name_to_item(item_name)
if item:
print "You drop it."
self.location.contents.add_item(item)
self.inventory.remove_item(item)
else:
print "That's not something you can drop."
def sell(self):
print "Sell not implemented"
def scream(self):
print "scream not implemented"
def buy(self):
print "buy not implemented"
class GamePlay(object):
def play(self,opening_message = "Welcome to my Text Adventure Game"):
print opening_message
self.help()
self.player.look_location()
while True:
command = raw_input("\n>")
verb, noun = self.parse(command)
if verb == "exit":
break
elif verb == "look":
if noun:
self.player.look(noun)
else:
self.player.look_location()
elif verb == "inventory":
self.player.display_inventory()
elif verb == "get":
self.player.get(noun)
elif verb == "take":
self.player.get(noun)
elif verb == "drop":
self.player.drop(noun)
elif verb in Location.DIRECTIONS:
self.player.move(verb)
elif verb == "help":
self.help()
#New actions
elif verb == "sell":
self.player.sell()
elif verb == "buy":
self.player.buy()
elif verb == "scream":
self.player.scream()
else:
print "I don't understand", verb
def help(self):
print """
There are many unique commands that will be available, use your discretion to
decide which will get you through the day alive.
"""
def parse(self, command):
command = command.strip() # get rid of leading/trailing whitespace
command = command.lower() # make lowercase
words = command.split() # split into a list of words
if len(words) == 0:
return "",""
elif len(words) == 1:
return words[0], ""
else:
return words[0], words[1]
if __name__ == "__main__":
print "This is a Module with classes for text based games."
raw_input("\n\nPress the enter key to exit.")
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor