def showInventory (): """ lists everything the player is carrying, and everything they are wearing. """ global location # show a list of everything you have print "You have: " for thing in things.getByLocation("INVENTORY"): print thing.title, # We want to let the player know if the flashlight is on if "on" in thing.tags: print "(on)" else: print print # show anything you're wearing print "You are wearing: " for thing in things.getByLocation("WORN"): print thing.title print
def showInventory(): """ lists everything the player is carrying, and everything they are wearing. """ global location # show a list of everything you have print "You have: " for thing in things.getByLocation("INVENTORY"): print thing.title, # We want to let the player know if the flashlight is on if "on" in thing.tags: print "(on)" else: print print # show anything you're wearing print "You are wearing: " for thing in things.getByLocation("WORN"): print thing.title print
def look (): """ makes the player look at the current room """ global location here = places.getById(location) # Where are we? Show this place's title and description: print " - " + here.title + " -" print here.description print # display any Things in the area, unless they're hidden. thingsHere = [thing for thing in things.getByLocation(location) if "hidden" not in thing.tags] # <-- list comprehension! one of many ways python is awesome if len(thingsHere) > 0: for thing in thingsHere: print " " + thing.short_description print
def look(): """ makes the player look at the current room """ global location here = places.getById(location) # Where are we? Show this place's title and description: print " - " + here.title + " -" print here.description print # display any Things in the area, unless they're hidden. thingsHere = [ thing for thing in things.getByLocation(location) if "hidden" not in thing.tags ] # <-- list comprehension! one of many ways python is awesome if len(thingsHere) > 0: for thing in thingsHere: print " " + thing.short_description print
def gameLoop(): """ the main game loop, where we get the whole feedback, input, and command parsing. """ done = False while not done: # first, make sure the game isn't over. if player.location == "TRYING TO LEAVE": # the player is trying to leave the building. Is the flashlight in their hand, and is it turned on? if things.getById("flashlight").location == "INVENTORY" and "on" in things.getById("flashlight").tags: showGoodEnding() done = True continue else: # woops. You haven't won yet. showBadEnding() done = True continue # There's also the option you died last round. elif player.location == "DEAD": done = True continue # Every once inawhile, spookiness happens. ghosts.playSpookyFlavorText() # Does anyone know you're here? If so, they get to act. for thing in things.getByLocation(player.location): if "alive" in thing.tags: thing.think() # ...did someone just kill you? if player.location == "DEAD": done = True continue # Tell us where we are: player.look() # get input from the user userInput = raw_input("What do you do? ") # then strip the leading whitespace, convert everything to lowerspace, and split input into individual words we can parse: words = userInput.strip().lower().split() # did they actually say anything? If not, we don't need to go further. if len(words) < 1: continue # So, does the player want to ... # quit the game? if words[0] in ["quit", "exit", "end", "q"]: # Looks like we're done here. done = True # read instructions again? if words[0] in ["instructions", "help", "?"] or (words[0] in ["show"] and words[1] in ["instructions", "help"]): showInstructions() # check inventory if words[0] in ["i", "inv", "inventory"] or (words[0] == "show" and words[1] == "inventory"): player.showInventory() # move? if words[0] in ["n", "north"] or (words[0] in ["go", "move"] and words[1] == "north"): player.go(NORTH) if words[0] in ["s", "south"] or (words[0] in ["go", "move"] and words[1] == "south"): player.go(SOUTH) if words[0] in ["w", "west"] or (words[0] in ["go", "move"] and words[1] == "east"): player.go(WEST) if words[0] in ["e", "east"] or (words[0] in ["go", "move"] and words[1] == "west"): player.go(EAST) if words[0] in ["go", "move"] and len(words) != 2: print ("Go where? Please specify a direction.") continue # all of these take a single argument. checkForTwoWordCommand(words, ["take", "get"], player.take) checkForTwoWordCommand(words, ["drop"], player.drop) checkForTwoWordCommand(words, ["throw"], player.throw) checkForTwoWordCommand(words, ["examine", "x"], player.examine) checkForTwoWordCommand(words, ["wear"], player.wear) checkForTwoWordCommand(words, ["unwear"], player.unwear) # flashlight verbs! if words[0] == "turn": # turn something on? if words[1] == "on": if len(words) == 3: player.turnOn(words[2]) else: print "Turn on what? Please specify a single noun." # maybe turn it off? if words[1] == "off": if len(words) == 3: player.turnOff(words[2]) else: print "Turn on what? Please specify a single noun."
def gameLoop(): """ the main game loop, where we get the whole feedback, input, and command parsing. """ done = False while not done: # first, make sure the game isn't over. if player.location == 'TRYING TO LEAVE': # the player is trying to leave the building. Is the flashlight in their hand, and is it turned on? if things.getById( 'flashlight' ).location == 'INVENTORY' and "on" in things.getById( 'flashlight').tags: showGoodEnding() done = True continue else: # woops. You haven't won yet. showBadEnding() done = True continue # There's also the option you died last round. elif player.location == 'DEAD': done = True continue # Every once inawhile, spookiness happens. ghosts.playSpookyFlavorText() # Does anyone know you're here? If so, they get to act. for thing in things.getByLocation(player.location): if "alive" in thing.tags: thing.think() # ...did someone just kill you? if player.location == 'DEAD': done = True continue # Tell us where we are: player.look() # get input from the user userInput = raw_input("What do you do? ") # then strip the leading whitespace, convert everything to lowerspace, and split input into individual words we can parse: words = userInput.strip().lower().split() # did they actually say anything? If not, we don't need to go further. if len(words) < 1: continue # So, does the player want to ... # quit the game? if words[0] in ["quit", "exit", "end", "q"]: # Looks like we're done here. done = True # read instructions again? if words[0] in [ 'instructions', 'help', '?' ] or (words[0] in ['show'] and words[1] in ['instructions', 'help']): showInstructions() # check inventory if words[0] in ['i', 'inv', 'inventory' ] or (words[0] == 'show' and words[1] == 'inventory'): player.showInventory() # move? if words[0] in ['n', 'north'] or (words[0] in ['go', 'move'] and words[1] == 'north'): player.go(NORTH) if words[0] in ['s', 'south'] or (words[0] in ['go', 'move'] and words[1] == 'south'): player.go(SOUTH) if words[0] in ['w', 'west'] or (words[0] in ['go', 'move'] and words[1] == 'east'): player.go(WEST) if words[0] in ['e', 'east'] or (words[0] in ['go', 'move'] and words[1] == 'west'): player.go(EAST) if words[0] in ['go', 'move'] and len(words) != 2: print("Go where? Please specify a direction.") continue # all of these take a single argument. checkForTwoWordCommand(words, ["take", "get"], player.take) checkForTwoWordCommand(words, ["drop"], player.drop) checkForTwoWordCommand(words, ["throw"], player.throw) checkForTwoWordCommand(words, ["examine", "x"], player.examine) checkForTwoWordCommand(words, ["wear"], player.wear) checkForTwoWordCommand(words, ["unwear"], player.unwear) # flashlight verbs! if words[0] == 'turn': # turn something on? if words[1] == 'on': if len(words) == 3: player.turnOn(words[2]) else: print "Turn on what? Please specify a single noun." # maybe turn it off? if words[1] == 'off': if len(words) == 3: player.turnOff(words[2]) else: print "Turn on what? Please specify a single noun."