[ create a new paste ] login | about

Link: http://codepad.org/hsqgN4p6    [ raw code | fork ]

Python, pasted on Apr 6:
from sys import exit

class TheMap(object): #I am TheMap! I am a thing that exists. 
    def __init__(self, location):  #Start TheMap! It needs a "location" to function. It also needs to
                                    #know itself, hence "self" is initiated. 
        print "Calling __init__ for TheMap." #confirming that it's running for my own sake. 
        self.start = location #start in TheMap context is now "location". This is necessary for the getattr
                                #wayyyyy down below in TheEngine, because TheEngine has to run off strings
                                #passed to it from TheMap. 

    def death(self):  
        print "you died."
        exit(1)  

    def central_corridor(self):  
        print "Gothons have killed your crew and one's about to blast you!"
        print "Will you (s)hoot or (t)aunt?"

        action = raw_input("> ")
        action = action.lower()

        if action == "s":
            print "You shoot! You miss! You die!"
            return 'death'  

        elif action == "t":
            print "improbably, that works!"
            return 'outer_space'

        else:
            print "Try that again, champ!"
            return 'central_corridor'

    def outer_space(self):
        print "now you are in outer space! Do you want to go home, or drift?"

        spaced = raw_input("h or d? ")
        spaced = spaced.lower()

        while spaced != "h" and spaced != "d":
            print "h or d, bozo."
            return "outer_space"

        if spaced == "h":
            print "Well, now you've gone home. Which is great, if you like home."
            exit(0)

        else:
            print "You drift forever, a happy vagabond."
            exit(0)

class TheEngine(object):
    def __init__(self, currentspot): #currentspot parameter is initially defined as TheMap('central_corridor')
                                #i.e. 'central_corridor' in the context of TheMap.
        print "Calling __init__ for TheEngine." #for my own sanity.
        self.currentspot = currentspot #this sets the above currentspot variable passed to TheEngine 
                                        #as currentspot in this class. But why? It is always referred to as
                                        #self.currentspot. 

    def play(self): #when this starts, currentspot is defined as TheMap('central_corridor').
        nextroom = self.currentspot.start #so nextroom is initially TheMap('central_corrior').start in the 
                                        #CONTEXT of self (in this class).
                                        #but why is start here? Since currentspot is set to be
                                        #TheMap('central_corridor'), it also looks for start in TheMap to set
                                        #self.start? If I remove the .start it doesn't work. WTF?

        while True: 
            print "---ONWARD!---"
            room = getattr(self.currentspot,nextroom) #applies nextroom to the local currentspot parameter.
            nextroom = room() #turns that string into a function
                                #so initially, this line wants a return from TheMap('central_corridor').
                                #when it gets it, it passes it to room = getattr(self.currentspot,nextroom)
                                #and then it parses that new entity as a function. 


#first define the parameter I want to pass into TheEngine: 
location = TheMap('central_corridor') #redundant now but keeping for archival reasons
#so now location points at the 'central corridor' string in the context of The Map.
#in other words 'central_corridor'.TheMap
#and now I need to put that location value into The Engine so it will parse it as start. 

x = TheEngine(TheMap('central_corridor')) #so this passes 'central_corridor'.TheMap INTO TheEngine as its parameter. 

#and finally, get it to play...

x.play() #applies function play() to TheEngine(location), sending 'central_corridor' in the context
# of TheMap into TheEngine as its variable. 


Create a new paste based on this one


Comments: