Skip to content

Instantly share code, notes, and snippets.

@bhtucker
Created April 13, 2016 17:03
Show Gist options
  • Save bhtucker/cd17c4b2b25d940e0f2933d0a1e791c8 to your computer and use it in GitHub Desktop.
Save bhtucker/cd17c4b2b25d940e0f2933d0a1e791c8 to your computer and use it in GitHub Desktop.
adventure game sketch
import random
GEORGI_TEXT = """
GEORGI IS HERE!!!!
Maybe you should [C]all for help.
"""
class Scene(object):
@classmethod
def enter(cls):
cls.display(cls.entry_text)
if getattr(cls, 'has_georgi', False):
cls.display(GEORGI_TEXT)
cls.action_map.update(GEORGI_ACTION)
return cls.react()
@classmethod
def display(cls, content):
print(content)
@classmethod
def react(cls):
action = raw_input("> ")
destination = cls.action_map.get(action)
if not destination:
cls.display("That didn't work")
return cls
return destination
class DiningRoom(Scene):
entry_text = """
You are in the dining room.
It looks like the Kitchen is to your [N]orth
"""
state_name = 'dining_room'
class Kitchen(Scene):
entry_text = """
You are in the Kitchen
It looks like the dining room is to your [S]outh
"""
state_name = 'kitchen'
class Escape(Scene):
entry_text = """
You are escaping! Great!
"""
SCENE_ACTIONS = {
DiningRoom: {
'N': Kitchen
},
Kitchen: {
'S': DiningRoom
}
}
GEORGI_ACTION = {
'C': Escape
}
class Engine(object):
def __init__(self, starting_scene):
self.starting_scene = starting_scene
self.initialize_georgi()
@classmethod
def handle_scene(cls, scene):
return scene.enter()
def start(self):
return Engine.handle_scene(self.starting_scene)
def initialize_georgi(self):
scene_list = SCENE_ACTIONS.keys()
scene_list.remove(self.starting_scene)
lucky_scene = random.choice(scene_list)
lucky_scene.has_georgi = True
def register_action_maps():
for scene, actions in SCENE_ACTIONS.iteritems():
scene.action_map = actions
register_action_maps()
if __name__ == '__main__':
game = Engine(DiningRoom)
next_scene = game.start()
while True:
next_scene = game.handle_scene(next_scene)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment