def light(self, action, noun): if utils.in_inventory(Lantern, self) and not self.has_light: self.print('Your lantern bursts in green flame that illuminates' ' the room.') self.has_light = True if self.location.dark: # history=False so the same location isn't saved back into # self.locationStack self.go('go', Location=self.location, history=False) return True elif utils.in_inventory(Lantern, self): self.print('Your light is already lit.') else: self.print('You need a light source!') return False
def look(self, action, noun): """Display information about the current location or an item.""" # Disable light if a lantern is not in the inventory ### Should be defined in the Lantern class, not here. This will require changing how items work if self.has_light and not utils.in_inventory(Lantern, self): self.has_light = False if noun == '' or noun == 'around': self.location.give_info(True, self.has_light) else: item = utils.get_item_from_name(noun, self.inventory) if item: if item.name == 'sword': # Make sword glow if an enemy is in an adjacent room ### Cute. Again should be defined in the sword class. This is leftover from before I decided ### to make this a library instead of a single game. glowing = False for i in self.location.creatures: if isinstance(i, Baddie): item.examine(True) if not glowing: for i in self.location.exits: for creature in self.location.exits[ exit].creatures: if isinstance(creature, Baddie): item.examine(True) return if not glowing: item.examine(False) else: item.examine() else: self.print(f'You do not have {noun}') return False return True
def look(self, action, noun): """Display information about the current location or an item.""" # Disable light if a lantern is not in the inventory if self.has_light and not utils.in_inventory(Lantern, self): self.has_light = False if noun == '' or noun == 'around': self.location.give_info(True, self.has_light) else: item = utils.get_item_from_name(noun, self.inventory) if item: if item.name == 'sword': # Make sword glow if an enemy is in an adjacent room glowing = False for i in self.location.creatures: if isinstance(i, Baddie): item.examine(True) if not glowing: for i in self.location.exits: for creature in self.location.exits[ exit].creatures: if isinstance(creature, Baddie): item.examine(True) return if not glowing: item.examine(False) else: item.examine() else: self.print(f'You do not have {noun}') return False return True
def say(self, action, noun): if noun == 'xyzzy': if utils.in_inventory(Mirror, self): if self.location.name == 'Start': self._change_score(1) self.print('You vanished and reappeared in your house.\n') self.go(action, noun) else: self.print('There was a flash of light...and your score was' ' mysteriously lowered by one.') self.score -= 1 else: self.print(f'You said "{noun}" but nothing happened.') return False return True
def say(self, action, noun): if noun == 'xyzzy': ### Again, this needs to go into Mirror. And Mirror should be a part of the demo, not ### a part of the actual library. if utils.in_inventory(Mirror, self): if self.location.name == 'Start': self._change_score(1) self.print('You vanished and reappeared in your house.\n') self.go(action, noun) else: self.print('There was a flash of light...and your score was' ' mysteriously lowered by one.') self.score -= 1 else: self.print(f'You said "{noun}" but nothing happened.') return False return True
def go(self, action, noun='', Location=None, history=True): if self.has_light and not utils.in_inventory(Lantern, self): self.has_light = False if Location is not None: destination = Location is_loc = True else: is_direction = False is_loc = False try: destination = self.location.exits[noun] except KeyError: pass ### List of directions should not be necessary. Another problem Map should fix for direction in [ 'north', 'south', 'east', 'west', 'up', 'down', 'northwest', 'northeast', 'southwest', 'southeast' ]: if direction == noun: is_direction = True break elif direction in self.location.exits: if self.location.exits[direction].name.lower() == noun: destination = self.location.exits[direction] break if not is_direction and not is_loc and action != 'say': self.print('You must specify a valid direction.') return elif action == 'say': # Get right Location from list called locations ### I think this is a weird addition to make the Mirror thing work. ### Again this should all be defined in the Mirror class; it is ridiculous ### for it to be here for i in self.locations: if i.name == 'Home': destination = i break elif noun in self.location.exits: # Get right Location from list called locations loc = None # loc = self.locations[self.locations.index( # self.location.exits[noun])] for i in self.locations: if self.location.exits[noun] == i: loc = i if loc: for i in self.locations: if i == loc: destination = i break elif is_loc: pass else: self.print('There is no exit in that direction.') return False if destination is not None: ### MAP! if self.location.history and history: self.location_stack.append(self.location) self.location = destination if not self.visited_places[self.location]: self.location.give_info(True, self.has_light) self.visited_places[self.location] = True else: self.location.give_info(False, self.has_light) if (not self.location.dark) or self.has_light: self._fight_check() else: self.print('Something went wrong.') return False return True
def go(self, action, noun='', Location=None, history=True): if self.has_light and not utils.in_inventory(Lantern, self): self.has_light = False if Location is not None: destination = Location is_loc = True else: is_direction = False is_loc = False try: destination = self.location.exits[noun] except KeyError: pass for direction in [ 'north', 'south', 'east', 'west', 'up', 'down', 'northwest', 'northeast', 'southwest', 'southeast' ]: if direction == noun: is_direction = True break elif direction in self.location.exits: if self.location.exits[direction].name.lower() == noun: destination = self.location.exits[direction] break if not is_direction and not is_loc and action != 'say': self.print('You must specify a valid direction.') return elif action == 'say': # Get right Location from list called locations for i in self.locations: if i.name == 'Home': destination = i break elif noun in self.location.exits: # Get right Location from list called locations loc = None # loc = self.locations[self.locations.index( # self.location.exits[noun])] for i in self.locations: if self.location.exits[noun] == i: loc = i if loc: for i in self.locations: if i == loc: destination = i break elif is_loc: pass else: self.print('There is no exit in that direction.') return False if destination is not None: if self.location.history and history: self.location_stack.append(self.location) self.location = destination if not self.visited_places[self.location]: self.location.give_info(True, self.has_light) self.visited_places[self.location] = True else: self.location.give_info(False, self.has_light) if not mute and (not self.location.dark) or self.has_light: self._fight_check() else: self.print('Something went wrong.') return False return True