def run_tests(): """Test Store class.""" # Create Inventory item1 = Item("Blue Wool", "2 Stacks", 1) item2 = Item("Red Wool", "2 Stacks", 1) inv = Inventory() inv.add_item(item1) inv.add_item(item2) # Create Store store = Store("All Australian Wool", 300, 400, "All your wool Needs!", inv) print(store) # Test update_description store.update_description("Lots of wool Colours") print(store) # Test update Location store.update_location(509, 5002) print(store) # Test list Inventory for item in store.inventory.items: print(item)
def load_entities(self): grid = self.create_grid(self.map_file) entities = [] for row in range(grid.shape[0]): for col in range(grid.shape[1]): entity = None if grid[row][col] == 1: entity = Player(col, row, Inventory(2, 10)) elif grid[row][col] == 2: inventory = Inventory(3, 5) inventory.add_item(DamageItem()) entity = Enemy(col, row, inventory) elif grid[row][col] == 3: entity = Wall(col, row) elif grid[row][col] == 4: entity = Treasure(col, row, [DamageItem()]) elif grid[row][col] == 5: entity = EndLevel(col, row) if entity: entities.append(entity) return entities
def run_tests(): """Test Inventory class.""" # Test empty Inventory (defaults) print("Test empty Inventory:") inv = Inventory() assert not inv.items # an empty list is considered False # Test adding an Item with values print("\nTest adding Item:") inv.add_item(Item("Red Wool", "2 Stacks", 2)) inv.add_item(Item("Green Wool", "2 Stacks", 2)) # Test list_inventory for item in inv.list_items(): print(item)
class InventoryFacade: def __init__(self, max_capacity=0): self.inventory_object = Inventory(max_capacity) def add_to_inventory(self, item=Item): if self.inventory_object.check_if_full(item.get_weight()): self.inventory_object.add_item(item) print('-> ' + str(item.__class__.__name__) + ' added to inventory') else: print('-> Inventory full') def display_inventory(self): inventory = self.inventory_object.get_inventory() print('Capacity => ' + str(inventory['occupied_capacity']) + '/' + str(inventory['max_capacity'])) print(':: Items ::') for item in inventory['items']: print(item.__class__.__name__)
def load_stores(self, path_to_file): """Read the file containing Stores and add to list.""" # Read file in_file = open(path_to_file, 'r') for line in in_file: line_str = line.strip().split(',') # Create Inventory shop_inventory = Inventory() # Separate Items items = line_str[INDEX_OF_ITEMS].strip().split('-') if items != ['']: for item in items: details = item.strip().split('|') # Create Item Objects item_name = details[INDEX_OF_ITEM_NAME] item_quantity = details[INDEX_OF_ITEM_QUANTITY] item_cost = int(details[INDEX_OF_ITEM_COST]) item_object = Item(item_name, item_quantity, item_cost) # Add to Inventory shop_inventory.add_item(item_object) # Create Shop shop_name = line_str[INDEX_OF_NAME] shop_location_x = line_str[INDEX_OF_LOCATION_X] shop_location_z = line_str[INDEX_OF_LOCATION_Z] shop_description = line_str[INDEX_OF_DESCRIPTION] shop = Store(shop_name, shop_location_x, shop_location_z, shop_description, shop_inventory) self.stores.append(shop) in_file.close()
class Hero: def __init__(self): self.name = 'Richard' self.max_hp = 50 self.hp = self.max_hp self.lvl = 1 self.exp = 0 self._expForLvlUp = 30 self.damage = 10 self.defence = 0 self.atk = '' self.defend = '' self.inventory = Inventory() self.equipment = Equipment() self.gold = 0 self.rewards = Rewards() def attack(self, enemy): enemy.hp -= self.damage def _check_lvl_up(self): if self.exp >= self._expForLvlUp: self.exp -= self._expForLvlUp self.lvl += 1 self._expForLvlUp = self._expForLvlUp * 2 self.max_hp += 10 self.hp = self.max_hp self.damage += 5 print('Уровень повышен! Теперь ваш уровень:', self.lvl, 'урон:', self.damage, 'здоровье:', self.max_hp) def restore_hero(self): # TODO: можно использовать для будущего лазарета self.hp = self.max_hp def equip_item(self, item): if item.type == 'weapon': self.equipment.weapon = item self.inventory.delete_item(item) print('Вы надели предмет:', item) elif item.type == 'armor': if item.part == 'head': self.equipment.head = item self.inventory.delete_item(item) print('Вы надели предмет:', item) elif item.part == 'body': self.equipment.body = item self.inventory.delete_item(item) print('Вы надели предмет:', item) elif item.part == 'arms': self.equipment.arms = item self.inventory.delete_item(item) print('Вы надели предмет:', item) elif item.part == 'legs': self.equipment.legs = item self.inventory.delete_item(item) print('Вы надели предмет:', item) def check_slot_equipment(self, current_item): if current_item.type == 'armor': if current_item.part == 'head': if self.equipment.head != '': return self.equipment.head elif self.equipment.head == '': return 0 elif current_item.part == 'body': if self.equipment.body != '': return self.equipment.body elif self.equipment.body == '': return 0 elif current_item.part == 'arms': if self.equipment.arms != '': return self.equipment.arms elif self.equipment.arms == '': return 0 elif current_item.part == 'legs': if self.equipment.legs != '': return self.equipment.legs elif self.equipment.legs == '': return 0 elif current_item.type == 'weapon': if self.equipment.weapon != '': return self.equipment.weapon elif self.equipment.weapon == '': return 0 def equip_and_replace_equip(self, current_item): equipment_item = self.check_slot_equipment(current_item) if equipment_item == 0: self.equip_item(current_item) elif equipment_item != 0: self.remove_item(equipment_item) self.equip_item(current_item) def remove_item(self, item): if item.type == 'armor': if item == self.equipment.head: self.equipment.head = '' self.inventory.add_item(item) print('Вы сняли предмет:', item) elif item == self.equipment.body: self.equipment.body = '' self.inventory.add_item(item) print('Вы сняли предмет:', item) elif item == self.equipment.arms: self.equipment.arms = '' self.inventory.add_item(item) print('Вы сняли предмет:', item) elif item == self.equipment.legs: self.equipment.legs = '' self.inventory.add_item(item) print('Вы сняли предмет:', item) elif item == self.equipment.weapon: self.equipment.weapon = '' self.inventory.add_item(item) print('Вы сняли предмет:', item) def calculate_stats(self, stat): if stat == 'damage': damage = self.damage + self.equipment.weapon return damage elif stat == 'armor': head = 0 body = 0 arms = 0 legs = 0 if self.equipment.head: head = self.equipment.head.defence if self.equipment.body: body = self.equipment.body.defence if self.equipment.arms: arms = self.equipment.arms.defence if self.equipment.legs: legs = self.equipment.legs.defence return self.defence + head + body + arms + legs def calculate_damage_by_armor(self, body_part, damage): armor_stats = self.calculate_stats('armor') if body_part == 'head' and self.equipment.head: damage -= damage / 100 * armor_stats return damage elif body_part == 'body' and self.equipment.body: damage -= damage / 100 * armor_stats return damage elif body_part == 'arms' and self.equipment.arms: damage -= damage / 100 * armor_stats return damage elif body_part == 'legs' and self.equipment.legs: damage -= damage / 100 * armor_stats return damage return damage
from Product import Product from Inventory import Inventory from Console import Console inventory = Inventory() inventory.add_item(Product(1, 1.23, 200)) inventory.add_item(Product(2, 2.2, 50)) inventory.add_item(Product(3, 0.5, 1000)) inventory.add_item(Product(4, 5.99, 75)) console = Console(inventory) console.run()
class Game: """Class used to represent the Game Attributes ---------- rooms_list: list list of Room objects representing the structure of the Mansion hero: Hero the Game character/iterator of the rooms_list inventory: Inventory the Game Inventory that provides carrying/dropping abilities tasks: Task the interactions within the Game that can/must be completed Methods ------- start() displays menu allows user to start the Game initializes_rooms() loads file data and initializes the Room objects move() manages Hero movement within the Game take() removes an Item from a Room and adds it to Inventory use() performs and action, with an Item drop() removes an Item from Inventory and adds it to a Room look_at_something() gets the description of an Item or a Feature save_game() saves the game data to load files for continuation get_command() retrieves user input for actions to be carried out play_game() main Game driver function """ rooms_list = list() hero = None inventory = None tasks = Task() parser = languageParser.LanguageParser() def start(self): """Displays the menu in a loop and allows user to start the Game :return: VOID """ while 1: item_list = [] selection = menu.display() if selection == 'newgame': intro.display() self.play_game('dataStore/newGame/load_file.json', 'dataStore/newGame/RoomState/', item_list) elif selection == 'loadgame': # Check if saved game exists to load load_file = Path('dataStore/savedGame/load_file.json') if load_file.is_file(): self.play_game(load_file, 'dataStore/savedGame/RoomState/', item_list) elif selection == 'credits': credits.display() elif selection == 'exit': break def initialize_rooms(self, data, file_path): """Creates Room objects based on the data passed to the function :param dict data: a list of the Room files :param str file_path: the path to the newGame or savedGame directory :return: VOID """ # for each item in data append it to the file path to get a full path # example dataStore/newGame/RoomState/Parlor.json # use the information in the extracted JSON to initialize Room objects for x in data: room_file = open(file_path + x, 'r', encoding='utf-8') room_data = json.loads(room_file.read()) room_file.close() new_room = Room(room_data['name'], room_data['longDes'], room_data['shortDes'], room_data['visited'], room_data['roomId'], room_data['directions'], room_data['startingItems'], room_data['droppedItems'], room_data['features']) # Room objects are placed into the rooms list() at specific # locations according the the room_id self.rooms_list.insert(new_room.room_id, new_room) def move(self, direction): """Manages movement of the Hero within the Game :param str direction: user input direction to move :return: VOID """ # set the current room to where the hero is located current_room = self.rooms_list[self.hero.location] current_room.set_visited() # check to see if the direction to move possible within that room if direction in current_room.directions: # change the hero's location to the new room self.hero.location = current_room.directions[direction] # Check if a task is necessary on move into next room and get the status. Currently nothing done with the status. self.tasks.perform_task_on_move(self.inventory, self.rooms_list, self.hero.location) # Hero time increment operation self.hero.time = self.hero.set_time() self.rooms_list[self.hero.location].get_description() def take(self, str_input): """Removes an Item from a Room and places it in the Inventory :param str str_input: user input of Item to be taken :return: VOID """ # set the current room to where the hero is located current_room = self.rooms_list[self.hero.location] # this will return True and the Item object if the # object is in the Room or False and None if it is not # it will also remove that object from the Room if self.inventory.space_available(): status, taken_item = current_room.take_item(str_input) else: self.print_output( "You cannot fit anymore items in your inventory.") return # if the Item was there put it in the Inventory if status == True: self.inventory.add_item(taken_item) # Check to determine if acquisition is part of a task # attempt to perform the task and get the status. Currently nothing done with the status. status = self.tasks.perform_task(taken_item, None, self.rooms_list) else: self.print_output("That is not an item you can take.") def use(self, str_item, str_feature): """Attempts to perform an action with an Item and/or a Feature :param str str_item: user input of Item wished to be used :param str str_feature: user input of Feature to be used :return: VOID """ current_room = self.rooms_list[self.hero.location] # check to see if the feature is in the room and get it feat_status, feat = current_room.get_feature(str_feature) # False Feature status - feature is not in the Room if not feat_status: print((' ' * 20) + 'There is no {} in the room.'.format(str_feature)) else: # Key counter variable to check if user has both keys in posession key_counter = 0 for x in self.inventory.items: if x.name == 'key': key_counter += 1 # If user has both keys, get the appropriate one for the room (kitchen or servant's quarters) if key_counter == 2: if current_room.room_id == 7: str_item = 'A small ornate key.' item_status, item = self.inventory.key_in_inventory( str_item) elif current_room.room_id == 15: str_item = 'A simple key.' item_status, item = self.inventory.key_in_inventory( str_item) else: item_status = False # If the key_counter is 2 but item_status is False it means that user was not in the kitchen or servant's quarters # when attempting to use the key. Get the first key item and move on if key_counter == 2: if item_status == False: # check that the item is in the inventory and get it item_status, item = self.inventory.in_inventory(str_item) # Else user doesn't have 2 keys - attempt to get the item elif key_counter != 2: # check that the item is in the inventory and get it item_status, item = self.inventory.in_inventory(str_item) # if the item is in the inventory and the feature is in the room if feat_status and item_status: # attempt to perform the task and get the status status = self.tasks.perform_task(item, feat, self.rooms_list) # True, means this is a valid Item/Feature combination if status: # Hero time increment operation self.hero.time = self.hero.set_time() # Remove the item from the inventory self.inventory.remove_item(item) else: # Else this is not a valid combination self.print_output(' ...you cannot do that now.') # False Item status - item is not in the Inventory elif not item_status: print((' ' * 20) + 'There is no {} in the inventory.'.format(str_item)) def drop(self, item_name): """Removes an Item from the Inventory and leaves it in a Room :param str item_name: user input of Item to be dropped :return: VOID """ # set the current room to where the hero is located current_room = self.rooms_list[self.hero.location] # this will return True and the Item if the object is # in the Inventory. If not it will return False and None # it will also remove the Item from the Inventory status, dropped_item = self.inventory.drop_item(item_name) # if the Item was in the Inventory, add it to the dropped_items if status: current_room.leave_item(dropped_item) self.print_output('You have dropped the {}'.format(item_name)) else: self.print_output('That item is not in your inventory.') def look_at_something(self, thing): """Gets the description of an Item or Feature in a Room or Inventory :param str thing: user input of Item/Feature to be looked at :return: VOID """ # set the current room to where the hero is located current_room = self.rooms_list[self.hero.location] # check to see if the 'thing' is in the Room - this will look in # the starting_items, dropped_items, and features thing_in_room, thing_room_des = current_room.look_in_room(thing) # check to see if the 'thing' is in the Inventory thing_in_inven, thing_inven_des = self.inventory.look_in_inventory( thing) # the thing is in the Room so print the description if thing_in_room: print() # Print the feature description via the wrap processor to preserve colors processed = wrapper.wrap_processor(thing_room_des) for i in processed: print(i) # Check to see if a task is associated with look operation self.tasks.perform_task_on_look(thing_room_des, self.rooms_list, self.hero.time) # Hero time increment operation self.hero.time = self.hero.set_time() # not in the Room, but in the Inventory, print description elif thing_in_inven: 'INVENTORY ITEM: ' self.print_output(thing_inven_des) # Check to see if a task is associated with look operation self.tasks.perform_task_on_look(thing_inven_des, self.rooms_list, self.hero.time) # Hero time increment operation self.hero.time = self.hero.set_time() # not in the Room or the Inventory else: self.print_output( 'You do not see a {} in this room.'.format(thing)) def save_game(self): """Saves the state of the Game to save files :return: VOID """ # Get the names of the Rooms from the Seed file copy to load_data room_names = open('dataStore/savedGame/Seed.json', 'r', encoding='utf-8') load_data = json.loads(room_names.read()) room_names.close() # add the Hero state and Inventory state to the load_data load_file = open('dataStore/savedGame/load_file.json', 'w', encoding='utf-8') load_data['inventory'] = self.inventory.save_inventory() load_data['hero'] = self.hero.save_hero() # write the load_data to the save file output_data = json.dumps(load_data, indent=2) load_file.write(output_data) load_file.close() # Go through the rooms_list and save each of the rooms to a separate save file for room in self.rooms_list: room_file = open('dataStore/savedGame/RoomState/{}.json'.format( room.name), 'w', encoding='utf-8') room_data = json.dumps(room.save_room(), indent=2) room_file.write(room_data) def get_command(self): """Get user input for interactions within the Game :return: VOID """ current_room = self.rooms_list[self.hero.location] # Check to determine if change needed to long_description for next game loop self.tasks.perform_task_on_description(self.rooms_list, self.hero.location) command = self.parser.parse_args(self.rooms_list, self.hero) if command[0] == 'move': self.move(command[1]) elif command[0] == 'take': self.take(command[1]) elif command[0] == 'inventory': self.inventory.show_inventory() elif command[0] == 'drop': self.drop(command[1]) elif command[0] == 'look': if len(command) == 1: print() processed = wrapper.wrap_processor(current_room.long_des) for i in processed: print(i) # Hero time increment operation self.hero.time = self.hero.set_time() else: self.look_at_something(command[1]) elif command[0] == 'use': self.use(command[1], command[2]) elif command[0] == 'map': inventoryMapScreen.display(self.inventory, current_room.name, self.hero.location, self.rooms_list) current_room.get_description() elif command[0] == 'save': self.save_game() elif command[0] == 'play' and command[1] == 'pool': if current_room.name == 'Game Room': self.play_pool() else: print(' ' * 20 + "You can't do that here.") # Check day status on each iteration of the game loop self.check_day() def play_game(self, input_file, file_path, item_list): """Initializes the Game variables and starts the game-play :param str input_file: main load file :param str file_path: path to the appropriate Rooms directory :param list item_list: list of starting Items :return: Void """ game_file = open(input_file, 'r', encoding='utf-8') file_data = json.loads(game_file.read()) room_data = file_data['rooms'] hero_data = file_data['hero'] inventory_data = file_data['inventory'] self.initialize_rooms(room_data, file_path) self.hero = Hero(hero_data['name'], hero_data['location'], hero_data['time'], hero_data['day']) self.inventory = Inventory(inventory_data) room_iterator = 0 current_room = self.rooms_list[0] while room_iterator < 22: for i in item_list: status, taken_item = current_room.take_item(i) if status: self.inventory.add_item(taken_item) room_iterator += 1 current_room = self.rooms_list[room_iterator] # Get the description of the starting Room and print it starting_room = self.rooms_list[self.hero.location] starting_room.get_description() while 1: self.get_command() def print_output(self, string): print() wrappedText = textwrap.wrap(string, width=83) for i in wrappedText: print((' ' * 20) + i) def check_day(self): # Check to see if new day. If not, a Null is returned. Else an integer corresponding to the day day = self.hero.check_day() if day: self.tasks.perform_task_on_day(day) def play_pool(self): rand_number = random.randint(0, 100) % 2 if rand_number == 0: print( ' ' * 20 + "You size up the Poltergeist. You know you can take down this clown.\n" ) print( ' ' * 20 + "In a flurry of quick strikes, he promptly and decidedly beats you.\n" ) print(' ' * 20 + "I don't lose. Better luck next try.") else: print( ' ' * 20 + "You're not sure if you can beat the Poltergeist at his own game.\n" ) print( ' ' * 20 + "Taking careful aim, you sink all of your balls without giving him a turn." ) print(' ' * 20 + "You sink the 8-ball! You've won!\n") print( ' ' * 20 + "The Polgergist is very unhappy. He breaks his cue against the pool table." ) print( ' ' * 20 + "Perhaps you should consider leaving this room and let him cool off for a bit." )