def engage(self): """Begin combat. Combat consists of looping through the turn order until either the attackers or defenders have all left combat. """ console.output(f"You are fighting {self.defenders}") for character in self.attackers: character.enemies = self.defenders for character in self.defenders: character.enemies = self.attackers if self.surprise: for placement in self.combat_order: if placement.character in self.attackers: placement.character.attack() while True: for placement in self.combat_order: if len(self.attackers) == 0 or len(self.defenders) == 0: return if not placement.character.alive: self.combat_order.remove(placement.character) self.attackers.discard(placement.character) self.defenders.discard(placement.character) continue placement.character.attack() time.sleep(1)
def open(self): """Open the door, allowing the use of the Enter command.""" if self.is_open: console.output("The doors are already open.") return False self.is_open = True console.output("The heavy doors grind against the ground " "as you push them open.") return False
def read(script): if len(script) is 0: return console.end_line() for line in script: if type(line) is str: console.output(line, False) elif callable(line): line() console.end_line()
def loot(self): if self.confirm: console.output("You find a few old, copper coins among the " "well-preserved bodies. You monster.") self.player.proficiency -= 1 else: console.output("Are you sure? Stealing from the dead is said " "to bring bad luck.") self.confirm = True return False
def choose_target(self): """Choose a target to attack. This overrides the Actor class' choose_target method. The player will choose a target from the list of enemies. """ console.output("Choose a target:") console.output(self.enemies) while True: action = console.user_action(self.enemies) target = action.noun if target in self.enemies: return self.enemies[target]
def invalid_command(self, command): """Output to the user that they have failed to enter proper input, and tell them what input is acceptable. """ if command.noun is None: console.output("Valid objects are:") for noun in self.objects: console.output(noun + " ", new_line=False) else: console.output(f"Valid actions for the {command.noun} object are:") for action in self.objects[command.noun].actions: console.output(action + " ", new_line=False) console.end_line()
def check_health(player): if player.hit_points < player.max_hit_points / 2: console.output(badly_wounded, new_line=False) elif player.hit_points < player.max_hit_points: console.output(wounded, new_line=False) elif player.hit_points == player.max_hit_points: console.output(unharmed, new_line=False)
def attack(self): """Take a turn in combat and attack an enemy. The actor will choose a target and a weapon to attack with. The actor will swing at the target and, if their attack roll matches the target's armor, the attack will hit. The damage of the attack is determined by the chosen weapon. """ valid_enemies = self.enemies is not None and len(self.enemies) > 0 assert valid_enemies, "There are no enemies!" target = self.choose_target() weapon = self.choose_weapon() console.output(f"{self} attacks {target} with a {weapon}.") if self.swing() >= target.block(): damage = self.damage(weapon) target.take_damage(damage) console.output(f"{target} takes {damage} damage. {target} is now " f"at {target.hit_points} health.") if target.hit_points <= 0: console.output(f"{target} has died.") else: console.output(f"{self} misses.")
def enter(self): """Enter the door, triggering the next scene.""" if not self.is_open: console.output("The doors are shut.") return False return True
def enter(self): """The door is sealed shut and will not open.""" console.output("The doors are shut.") return False
def open(self): """The doors will refuse to open.""" console.output("The doors seem to be sealed shut. " "Hopefully you'll be able to open them when you leave.") return False
def look(self): """Display the object's description.""" assert (self.description is not None), f"{self} has no description." console.output(self.description) return False
def player_status(player): if player.alive: console.output(victory) else: console.output(death)