def check_special_function(self, game): if self.action_dict.get("verb", None) == None\ or self.action_dict["verb"] is not "call": say("You must use 'call' with " + self.action_dict["dobj"] + ".") else: say(verb_list[self.action_dict["verb"]].execute( game, self.action_dict))
def get_yn_answer(self, prompt): # displays the prompt # gets a yes or no from the player # returns True for yes, False for no # NOTE I included the prompt in case we want to re-display the prompt after an invalid input # NOTE2 it would probably make sense to have the other y/n questions in the game options use the same method valid_input = False input_str = "" while not valid_input: say(prompt) input_str = input("> ") if input_str.lower() == "y" or input_str.lower() == "yes": ret_val = True valid_input = True elif input_str.lower() == "n" or input_str.lower() == "no": ret_val = False valid_input = True else: say("Invalid input!") print() return ret_val
def go(self, game, actionargs): direction = actionargs.get("dobj") if self.exits.get(direction): self.exits[direction].go(game, actionargs) else: say(self.msg_cannot_go_direction)
def get_thing_by_name(self, thing_name, must_be_in_inventory): # first, look for thing with given name in player inventory # default_thing = Utilities.find_by_name() thing_in_inventory = self.find_by_name(thing_name, self.player.inventory) if(thing_in_inventory != None): #found it return thing_in_inventory else: if(must_be_in_inventory): default_thing = self.find_by_name(thing_name, self.thing_list) if default_thing is not None: say("You don't have {}.".format(default_thing.list_name)) else: say("I don't understand that. Please try another command.") return None else: # look in room's accessible contents: thing_in_room = self.find_by_name(thing_name, self.player.current_room.get_all_accessible_contents()) if(thing_in_room != None): # found it return thing_in_room else: default_thing = self.find_by_name(thing_name, self.thing_list) if default_thing is not None: say("You don't see {}.".format(default_thing.list_name)) else: say("You don't see that.") if self.player.current_room.id == "roomI"\ and self.player.current_room.is_lit == False: say("But then again you can't really see much of anything...") return None
def inventory(self): item_str = "" for item in self.player.inventory: item_str = item_str + "{:15}".format(item.name) say("Inventory:") say(item_str)
def get_description(self, time=-1): if time != -1: if time not in self.special_time: if self.shifty_man in self.contents: self.remove_thing(self.shifty_man) super().get_description() else: super().get_description() self.add_thing(self.shifty_man) say("There is a shifty man standing in front of the door.") else: super().get_description()
def sleep(self, game, actionargs): hours = int(-1) while (hours < 1) or (hours > 12): say("How many hours do you want to sleep for (between 1 and 12)?") hours = input("> ") try: hours = int(hours) if (hours < 1) or (hours > 12): say("You must enter an integer between 1 and 12.") except ValueError: hours = int(-1) say("You must enter an integer between 1 and 12.") count = 0 while count < hours: game.advance_time() count += 1 hours = str(hours) say("You slept for " + hours + " hours.") self.look(game, actionargs)
def check_basic_verbs(self, game): if self.user_input == "look": self.action_dict["verb"] = "look" say(verb_list[self.action_dict["verb"]].execute( game, self.action_dict)) elif self.user_input == "help": game.help() elif self.user_input == "inventory": game.inventory() elif self.user_input == "loadgame": game.load_menu(False) elif self.user_input == "savegame": game.save_game() elif self.user_input == "quit": game.quit() else: return 1
def get_description(self, time=-1): # print("BusStation.get_description()") # print("special_time = " + str(self.special_time)) # print("time = " + str(time)) # print("bus= " + self.bus.id) if time != -1: if time not in self.special_time: self.set_normal() super().get_description() else: self.set_special() super().get_description() print() say("You caught the bus! It is waiting at the platform...") else: super().get_description()
def print_unrecognized(self): message = "WARNING - I did not recognized the following words: " idx = 0 while idx < len(self.unrecognized_words): message += self.unrecognized_words[idx] if len(self.unrecognized_words) == 2: if idx < (len(self.unrecognized_words) - 1): message += " and " elif idx < (len(self.unrecognized_words) - 2): message += ", " elif idx == (len(self.unrecognized_words) - 2): message += ", and " idx += 1 say(message) print()
def intro(): #Graphic display only supported on Linux/Mac if OS == "Linux" or OS == "Darwin": os.system('clear') print() file = open("model", "r") for line in file: print(line, end="") file.close() print() any = input("Press enter to continue...") say("Main Menu") else: print() say("Tower Escape - Main Menu") print()
def get_word_answer(self, prompt, answer): # displays the prompt # gets input from player # compares input to answer, and returns True or False if it matches # matches should ignore case? or extra whitespace? # NOTE I included the prompt in case we want to re-display the prompt after an invalid input say(prompt) input_str = input("> ") input_str_lc = input_str.lower() answer_lc = answer.lower() input_words = input_str_lc.split() answer_words = answer_lc.split() print() if input_words == answer_words: return True else: return False
def get_description(self, time=None): say(self.name) if not self.has_been_visited: say(self.long_description) self.has_been_visited = True else: if not self.is_lit: description_string = self.short_description for exit in self.exits.values(): if exit.has_dynamic_description: description_string += " " + exit.get_dynamic_description( ) say(description_string) else: description_string = self.short_description listed_things = [] for exit in self.exits.values(): if exit.is_accessible: if exit.has_dynamic_description: description_string += " " + exit.get_dynamic_description( ) if exit.is_listed: listed_things.append(exit) for thing in self.contents: if thing.is_accessible: if thing.has_dynamic_description: description_string += " " + thing.get_dynamic_description( ) if thing.is_listed: listed_things.append(thing) num_listed_things = len(listed_things) if num_listed_things > 0: list_string = " You see" list_string += Utilities.list_to_words( [o.get_list_name() for o in listed_things]) list_string += "." description_string += list_string say(description_string)
def parse_input(self, game, user_input): print() self.init_input(user_input) self.update_dictionaries(game) if self.check_basic_verbs(game) == 1: if self.set_input(game) == 1: print() return else: if self.doubles == True: say("I don't understand that. Please try a different command." ) print() return if len(self.unrecognized_words) != 0: self.print_unrecognized() if self.debug == True: self.print_parsed() elif self.special_function: self.check_special_function(game) elif self.action_dict.get("verb", None) != None: say(verb_list[self.action_dict["verb"]].execute( game, self.action_dict)) else: say("I don't understand that. Please try a different command." ) print()
def led(self, game, actionargs): if self.is_lit is False: self.is_lit = True for item in self.contents: if item.name == "cobwebs": item.description = "Sticky cobwebs cover the walls, ceiling, "\ "and floors, only getting denser further into the room." break message = self.msg_lit if self.contains_moth: message += self.moth_msg say(message) self.short_description = self.alternate_description # add floppy # change properties of stuff in room (floppy, cobwebs, spider?) else: say(self.msg_already_lit)
def save_game(self): data = dict() new_save = dict() # Get the game data that is going to be saved data["player"] = json.loads(self.player.get_status()) data["room_list"] = dict() for id in self.room_list: data["room_list"][id] = json.loads(self.room_list[id].get_status()) data["thing_list"] = dict() for id in self.thing_list: data["thing_list"][id] = json.loads(self.thing_list[id].get_status()) # if this is a game that hasn't been previously saved if self.current_save is None: # assign a new save ID self.current_save = self.game_data["save_ctr"] # increment the save ID counter self.game_data["save_ctr"] += 1 # write or overwrite the game data into the saves list self.game_data["saves"][str(self.current_save)] = {} self.game_data["saves"][str(self.current_save)]["data"] = data.copy() self.game_data["saves"][str(self.current_save)]["timestamp"] = str(datetime.datetime.now()).replace(":", "-") self.game_data["saves"][str(self.current_save)]["game_time"] = self.game_time self.game_data["saves"][str(self.current_save)]["hints"] = self.hints # write the game data to the save file f = open (SAVES, "w") f.truncate(0) json.dump(self.game_data, f) f.close() say("Game saved!")
def hint(self, game, acitonargs): max_hints = len(self.hints) if not self.hints: say("There are no hints for this room") else: if self.used_hints < max_hints: self.used_hints += 1 game.hints += 1 for num in range(self.used_hints): say("Hint #{}: ".format(num + 1) + self.hints[num]) if self.used_hints == max_hints: say("There are no additional hints.")
def get_description(self, time=None): # print("Room.get_description()") # print("time = " + str(time)) say(self.name) if self.has_been_visited: description_string = self.short_description listed_things = [] already_described = [ ] # Stores exits that have already been described for exit in self.exits.values(): # Only do this if exit hasn't already been described if exit.is_accessible and exit not in already_described: if exit.has_dynamic_description: description_string += " " + exit.get_dynamic_description( ) already_described.append( exit) # Append exit to described exits if exit.is_listed: listed_things.append(exit) for thing in self.contents: if thing.is_accessible and thing not in already_described: if thing.has_dynamic_description: description_string += " " + thing.get_dynamic_description( ) already_described.append(thing) if thing.is_listed: listed_things.append(thing) num_listed_things = len(listed_things) if num_listed_things > 0: list_string = " You see" list_string += Utilities.list_to_words( [o.get_list_name() for o in listed_things]) list_string += "." description_string += list_string say(description_string) else: say(self.long_description) self.has_been_visited = True
def dance(self, game, actionargs): if game.thing_list["DancingDaemon"] in self.get_all_contents(): game.thing_list["DancingDaemon"].dance(game, actionargs) else: message = "You dance like no one's watching. Except you feel like someone is watching..." say(message)
def load_menu(self, initial): i = 1 load_names = dict() for save in self.game_data["saves"]: name = self.game_data["saves"][save]["data"]["player"]["name"] time = self.game_data["saves"][save]["timestamp"] display = name + ", " + time display_obj = dict() display_obj["display"] = display display_obj["id"] = save load_names[str(i)] = display_obj.copy() i += 1 if not initial and len(load_names) == 0: print("No saved games found!") return create_idx = str(len(load_names)) quit_idx = str(len(load_names)+1) if initial: print("Select a saved game to load, or create a new game.") else: # If this is not the first game being loaded, ask user if they are sure if not self.get_yn_answer("Any unsaved progress will be lost. Are you sure you want to load a different game? (y/n) "): return False print("Select a saved game to load.") for i in load_names: print(i + ". " + load_names[i]["display"]) if initial: create_idx = str(len(load_names)+1) quit_idx = str(len(load_names)+2) max = len(load_names)+2 print(create_idx + ". Create New Game") print(quit_idx + ". Quit") else: back_idx = str(len(load_names)+1) max = len(load_names)+1 print(back_idx + ". Go back") selection = self.validate_input(1, max) if initial: if int(selection) is (max-1): debug("creating new game") self.create_new_game() return True if int(selection) is max: return False debug("loading saved game: " + str(load_names[selection]["display"])) self.load_saved_game(load_names[selection]["id"]) print() say("Game loaded!") print() #any = input("Press any key to continue...") return True
def sit(self, game, actionargs): say(self.msg_sit)
def led(self, game, actionargs): say("The lights are already on.")
def pro(self, game, actionargs): if game.player.pro: say(self.msg_already_pro) else: game.player.pro = True say(self.msg_pro)
def dance(self, game, actionargs): say(self.msg_dance)
def add_exit(self, exit, direction): if direction not in self.exits.keys(): self.exits[direction] = exit else: say("ERROR: THAT DIRECTION ALREADY HAS AN EXIT")
def help(self): say("Commands:") say("{:>10}{:^5}{}".format("inventory", ":", "display a list of all of your items")) say("{:>10}{:^5}{}".format("loadgame", ":", "load a previously saved game")) say("{:>10}{:^5}{}".format("savegame", ":","save your current progress")) say("{:>10}{:^5}{}".format("help", ":","display this help menu")) say("{:>10}{:^5}{}".format("quit", ":","close the game (with or without saving)")) print() verb_str = "" for verb in self.verb_list: verb_str = verb_str + "{:10}".format(verb) say("Actions:") say(verb_str)
def end(self): self.end_game = True if self.game_over: say("You used " + str(self.hints) + " hints to complete the game.")