Created
June 15, 2011 13:27
-
-
Save keyo/1027083 to your computer and use it in GitHub Desktop.
Text RPG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
import re | |
class Location(): | |
name = None | |
msg_arrive = 'Hi {player}, Welcome to {location_name}' | |
msg_depart = 'You leave {location_name}' | |
msg_look = 'You see {location_name}' | |
items = defaultdict(int) | |
characters = [] | |
def __init__(self, **kwargs): | |
self.__dict__.update(kwargs) | |
def __getitem__(self, item): | |
if item == 'north': | |
return self.north | |
elif item == 'south': | |
return self.south | |
elif item == 'east': | |
return self.east | |
elif item == 'west': | |
return self.west | |
else: | |
raise KeyError | |
def __str__(self): | |
return self.name | |
class Item(): | |
item_name = 'random item' | |
movable = True | |
msg_pick_up = 'You pick up the {item_name}' | |
description = 'You see giraffe with a long neck.' | |
class Person(): | |
name = None | |
inventory = {} | |
def __init__(self, name): | |
self.name = name.strip() | |
class Player(Person): | |
location = None | |
class Character(Person): | |
greeting = 'come_in' | |
msg_tree ={ | |
'tea': | |
{'Hello, how about a cup of tea?': | |
{'yes':'tea_type','no':'come_in','green tea':'...'} | |
}, | |
'tea_type': | |
{'What kind of tea?': | |
{'earl grey':'...', 'english breakfast':'...'} | |
}, | |
'come_in': | |
{"Hi there, I am {name}. would you like to come in?": | |
{'yes':'tea', 'no':'goodbye'} | |
}, | |
'goodbye': | |
{'Fairwell, may you luck be with you on your travels.': | |
False} #end the talk | |
} | |
class Game(): | |
locations = { | |
'cliff_top':Location(name='Cliff', north='ocean', msg_depart='You fall off the cliff', msg_arrive='Welcome {player}. You are standing at the edge of a cliff.'), | |
'ocean':Location(name = 'Ocean', south='beach', msg_arrive='There is ocean at the bottom of the cliff. You\'re alive but a shark is circling.', msg_depart = 'The shark got your big toe, but you are safe now!'), | |
'beach':Location(name = 'Beach', north='ocean', msg_arrive='{player}, you are now on the beach, bro, beached as! Can\t chew bro!') | |
} | |
items = {} | |
player = None | |
def __init__(self, player_name): | |
self.player = Player(player_name) | |
self.player.location = 'cliff_top' | |
print self.locations[self.player.location].msg_arrive.format(player=self.player.name) | |
def go(self, direction): | |
if self.player.location is None: | |
return | |
#depart from old location | |
loc = self.locations[self.player.location] | |
print loc.msg_depart.format(player=self.player.name, location_name=loc.name) | |
loc = self.locations[self.player.location][direction] | |
if loc is not None: | |
self.player.location = loc | |
else: | |
print("Can't find {0}").format(location) | |
#arrive at new location | |
loc = self.locations[self.player.location] | |
print loc.msg_arrive.format(player=self.player.name, location_name=loc.name) | |
def look(self, direction): | |
if self.player.location is None: | |
return | |
#look at new locaton | |
#try: | |
new_loc = self.locations[self.locations[self.player.location][direction]] | |
#except KeyError: | |
# print("There is nothing here.") | |
print(new_loc.msg_look.format(player = self.player.name, location_name = new_loc.name)) | |
def look_here(self): | |
if self.player.location is None: | |
return | |
print(self.locations[self.player.location]) | |
g = Game(raw_input('What is your name?')) | |
while(True): | |
s = raw_input('>') | |
parts = s.split() | |
#special cases | |
if parts[0] is 'exit': | |
print("Bye.") | |
sys.exit() | |
if re.search("where\s+am\s+i",s): | |
print g.look_here() | |
continue | |
if len(parts) > 1: | |
try: | |
f = getattr(g, parts[0]) | |
except: | |
print("That command does not exist.") | |
try: | |
f(*parts[1:]) | |
except: | |
print("You cannot {0} like that.".format(parts[0])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment