def _process_user_choice(self): verifying_choice = True index = 0 print("Current Occupant: %s" % self.get_occupants()) while verifying_choice: msg = "\033[1m" + "Choose a hut number to enter (1-5): " + "\033[0m" user_choice = input("\n" + msg) try: index = int(user_choice) except ValueError as e: print_bold("Invalid input args: %s\n" % e.args) continue try: if self.huts[index - 1].is_acquired: print( "You have already acquired this hut. Try again." "<INFO: You can NOT get healed in already acquired hut.>" ) else: verifying_choice = False except IndexError: print('Invalid input: ', index) print_bold('Number should be in the range of 1-5. Try again.') continue return index
def show_health(self, bold=False, end='\n'): # TODO: what if there is no enemy? msg = "Health: %s: %d" % (self.name, self.health_meter) if bold: print_bold(msg, end=end) else: print(msg, end=end)
def acquire_hut(self, hut): print_bold("Entering hut %d..." % hut.number, end=' ') is_enemy = (isinstance(hut.occupant, AbstractGameUnit) and hut.occupant.unit_type == 'enemy') continue_attack = 'y' if is_enemy: print_bold("Enemy sighted!") self.show_health(bold=True, end=' ') hut.occupant.show_health(bold=True, end=' ') while continue_attack: continue_attack = input(".......continue attack? (y/n): ") if continue_attack == 'n': self.run_away() break self.attack(hut.occupant) if hut.occupant.health_meter <= 0: print("") hut.acquire(self) break if self.health_meter <= 0: print("") break else: if hut.get_occupant_type() == 'unoccupied': print_bold("Hut is unoccupied") else: print_bold("Friend sighted!") hut.acquire(self) self.heal()
def heal(self, heal_by=2, full_healing=True): if self.health_meter == self.max_hp: return if full_healing: self.health_meter = self.max_hp else: self.health_meter = max(self.max_hp, self.health_meter + heal_by) if self.health_meter > self.max_hp: raise GameUnitException( "Health meter greater than maximum value %d" % self.max_hp, 100) print_bold("You are HEALED!", end=' ') self.show_health(bold=True)
def play(self): self.setup_game_scenario() acquired_hut_counter = 0 while acquired_hut_counter < 5: index = self._process_user_choice() self.player.acquire_hut(self.huts[index - 1]) if self.player.health_meter <= 0: print_bold("YOU LOSE :( Better luck next time") break if self.huts[index - 1].is_acquired: acquired_hut_counter += 1 if acquired_hut_counter == 5: print_bold("Congratulations! YOU WIN!!!")
def run_away(self): print_bold("RUNNING AWAY...") self.enemy = None
def show_game_mission(self): """Print the game mission in the console""" print_bold("Mission:") print(" 1. Fight with the enemy.") print(" 2. Bring all the huts in the village under your control") print("---------------------------------------------------------\n")
def acquire(self, new_occupant): self.occupant = new_occupant self.is_acquired = True print_bold("GOOD JOB! Hut %d acquired" % self.number)