def run_away(self): """Abandon the battle. .. seealso:: `self.acquire_hut` """ print_bold("RUNNING AWAY...") self.enemy = None
def show_health(self, bold=False, end='\n'): """Show the remaining hit points of the player and the enemy""" # 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 show_health(self, bold=False, end='\n'): """Ispisi informaciju o zdravlju. Stavite opis metoda, parametre, seealso i todo. """ msg = "Zdravlje: {}: {}".format(self.name, self.health_meter) if bold: print_bold(msg, end=end) else: print(msg, end=end)
def run_away(self): """Abandon the combat and run away from the hut If the player is losing the combat, there is an option to leave the hut. A strategy to rejuvenate and restart the combat for a better chance of winning. ..seealso :: :py:meth:`self.acquire_hut` """ print_bold("RUNNING AWAY...") self.enemy = None
def acquire(self, new_occupant): """Ažuriraj okupanta kućice i promijeni status `is_acquired`. Ažuriraj varijablu instance za okupanta s parametrom `new_occupant` :arg new_occupant: self.occupant će se ažurirati s ovim parametrom .. todo:: U trenutačnoj implementaciji samo vitez može zatražiti kućicu. Plan je dodati mogućnost i za neprijatelja. """ self.occupant = new_occupant self.is_acquired = True print_bold("Kucica broj {} zauzeta".format(self.number))
def heal(self, heal_by=2, full_healing=True): """Heal the unit replenishing all the hit points""" if self.health_meter == self.max_hp: return if full_healing: self.health_meter = self.max_hp else: self.health_meter += heal_by if self.health_meter > self.max_hp: raise GameUnitError("Healh_meter > max_hp!", 101) print_bold("You are HEALED!", end=' ') self.show_health(bold=True)
def heal(self, heal_by=2, full_healing=True): """Heal the unit replenishing all the hit points""" if self.health_meter == self.max_hp: return if full_healing: self.health_meter = self.max_hp else: # TODO: Do you see a bug here? it can exceed max hit points! self.health_meter += heal_by if self.health_meter > self.max_hp: raise HealthMeterException("health_meter > max_hp!") print_bold("You are HEALED!", end=' ') self.show_health(bold=True)
def heal(self, heal_by=2, full_healing=True): """Heal the unit replenishing all the hit points""" if self.health_meter == self.max_hp: return if full_healing: self.health_meter = self.max_hp else: self.health_meter += heal_by # raise a custom exception if self.health_meter > self.max_hp: raise HealthMeterException("health_meter > max_hp!") print_bold("You are HEALED!", end=' ') self.show_health(bold=True)
def heal(self, heal_by=2, full_healing=True): """Heal the unit replenishing its hit points""" if self.health_meter == self.max_hp: return if full_healing: self.health_meter = self.max_hp else: self.health_meter += heal_by # ------------------------------------------------------------------ # raise a custom exception. Refer to chapter on exception handling # ------------------------------------------------------------------ if self.health_meter > self.max_hp: raise GameUnitError("health_meter > max_hp!", 101) print_bold("You are HEALED!", end=' ') self.show_health(bold=True)
def heal(self, heal_by=2, full_healing=True): """ metoda za ozravljanje lika """ if self.health_meter == self.max_hp: return if full_healing: self.health_meter = self.max_hp else: self.health_meter += heal_by # nasa iznimka if self.health_meter > self.max_hp: raise HealthMeterException() print_bold("Izliječen si!", end=' ') self.show_health(bold=True)
def show_health(self, bold=False, end='\n'): """Print info on the current health reading of this game unit The arguments to this method are mainly to customize the message display style. :param bold: Flag to indicate whether information should be printed in bold style or normal style. :param end: Specify how the message should end i.e wheter a new line character should be appended in the end or you want to add a space or a tab (for message continuation) """ msg = "Health: {0:s} {1:d}".format(self.name, self.health_meter) if bold: print_bold(msg, end=end) else: print(msg, end=end)
def acquire_hut(self, hut): """'Fight' the combat (command line) to acquire the hut :arg Hut hut: The hut that needs to be acquired. .. todo:: Refactor this method as an exercise Example: Can you use self.enemy instead of calling hut.occupant every time? """ print_bold("Entering hut %d..." % hut.number, end=' ') is_enemy = (isinstance(hut.occupant, AbstractGameUnit) and hut.occupant.unit_type == 'enemy') continue_attack = 'y' # Code block that tells what to do when you see, an enemy or a friend # or no one in the hut. # TODO: Refactor this. if is_enemy: print_bold("Enemy sighted!") self.show_health(bold=True, end=' ') hut.occupant.show_health(bold=True, end=' ') # Attack the enemy until the player says so. # TODO: The user must select either 'y' or 'n'. In # the current implementation there is a bug where you # do not need to enter 'y'... fix this as an exercise! while continue_attack: continue_attack = input("\n...continue attack? (y/n): ") if continue_attack == 'n': self.run_away() break # Player wants to attack, call the attack method. self.attack(hut.occupant) # This turn (iteration) of the combat is over, Check # if Player has won if hut.occupant.health_meter <= 0: print("") hut.acquire(self) break # Player's health_meter is empty.. # This indicates Player has lost the combat 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 acquire(self, new_occupant): """Update the occupant of this hut and set is_acquired flag. Update the occupant instance variable with the parameter new_occupant and set the is_acquired flag to True. :arg new_occupant: self.occupant will be updated with this parameter .. todo:: In the current implementation this is supposed to be called only by the `Knight` instance (everything from the player context. A generalization is to allow anyone to 'acquire' the hut! In that case, the client code should properly interpret meaning of `is_acquired` flag! Otherwise it will be a bug! As an exercise, write a unit test to catch this and/or make the calling code robust. """ self.occupant = new_occupant self.is_acquired = True print_bold("GOOD JOB! Hut %d acquired" % self.number)
def heal(self, heal_by=2, full_healing=True): """Metoda za ozravljanje lika. Stavite opis metode, parametre, seealso i todo. """ if self.health_meter == self.max_hp: return if full_healing: self.health_meter = self.max_hp else: self.health_meter += heal_by # nasa iznimka if self.health_meter > self.max_hp: #raise HealthMeterException() pass print_bold("Izliječen si!", end=' ') self.show_health(bold=True)
def play(self): self.player = Knight() self._occupy_huts() acquired_hut_counter = 0 self.show_game_mission() self.player.show_health(bold=True) while acquired_hut_counter < 5: idx = self._process_user_choice() self.player.acquire_hut(self.huts[idx - 1]) if self.player.health_meter <= 0: print_bold("YOU LOSE :( Better luck next time") if self.huts[idx - 1].is_acquired: acquired_hut_counter += 1 if acquired_hut_counter == 5: print_bold("Congratulations! YOU WIN!!!")
def play(self): """ Workhorse method to play the game """ # Create a Knight instance , create huts and preoccupy them # with a game character instance (or leave empty) self.setup_game_scenario() acquired_hut_counter = 0 while acquired_hut_counter < 5: idx = self._process_user_choice() self.player.acquire_hut(self.huts[idx - 1]) if self.player.health_meter <= 0: print_bold("Your Lose :( Better Luck Next Time!!!") break if self.huts[idx - 1].is_acquired: acquired_hut_counter += 1 if acquired_hut_counter == 5: print_bold("Congratulations!!! You Win!!!")
def play(self): """Workhorse method to play the game. Controls the high level logic to play the game. This is called from the main program to begin the game execution. """ self.setup_game_scenario() acquired_hut_counter = 0 while acquired_hut_counter < 5: idx = self._process_user_choice() self.player.acquire_hut(self.huts[idx - 1]) if self.player.health_meter <= 0: print_bold("YOU LOSE :( Better luck next time") break if self.huts[idx - 1].is_acquired: acquired_hut_counter += 1 if acquired_hut_counter == 5: print_bold("Congratulations! YOU WIN!!!")
def play(self): """ Workhorse method to play the game. Controls the high level logic to play the game. this is called from the main program to begin the game execution. In summary, this method has the high level logic that does the following by calling appropriate functionality: * Set up instance variables for the game * Accept the user input for hut number to enter * Attempt to acquire the hut (:py:meth:`Knight.acquire_hut`) * Determine if the player wins or loses. .. seealso:: :py:meth: `setup_game_scenario`, :py:meth:`Knight.acquire_hut` """ # Create a Knight instance, create huts and preoccupy them with a game # character instance (or leave empty) self.setup_game_scenario() # Initial setup is done, now the main play logic acquired_hut_counter = 0 while acquired_hut_counter < 5: idx = self._process_user_choice() self.player.acquire_hut(self.huts[idx - 1]) if self.player.health_meter <= 0: print_bold("YOU LOSE :( Better luck next time") break if self.huts[idx - 1].is_acquired: acquired_hut_counter += 1 if acquired_hut_counter == 5: print_bold("Congratulations! YOU WIN!!!")
def play(self): """Workhorse method to play the game. Controls the high level logic to play the game. This is called from the main program to begin the game execution. In summary, this method has the high level logic that does the following by calling appropriate functionality: * Set up instance variables for the game * Accept the user input for hut number to enter * Attempt to acquire the hut ( :py:meth:`Knight.acquire_hut` ) * Determine if the player wins or loses. .. seealso:: :py:meth:`Knight.acquire_hut` """ self.player = Knight() self._occupy_huts() acquired_hut_counter = 0 self.show_game_mission() self.player.show_health(bold=True) while acquired_hut_counter < 5: idx = self._process_user_choice() self.player.acquire_hut(self.huts[idx - 1]) if self.player.health_meter <= 0: print_bold("YOU LOSE :( Better luck next time") break if self.huts[idx - 1].is_acquired: acquired_hut_counter += 1 if acquired_hut_counter == 5: print_bold("Congratulations! YOU WIN!!!")
def acquire_hut(self, hut): """Fight the combat (command line) to acquire the hut .. todo:: acquire_hut method can be refactored. Example: Can you use self.enemy instead of calling hut.occupant every time? """ 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: try: continue_attack = input(".......continue attack? (y/n): ") assert continue_attack in ('y', 'n') except AssertionError: print("Invild Input:", continue_attack) print("You should input letter 'y' or 'n'!") continue 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 acquire_hut(self, hut): """'Fight' the combat (command line) to acquire the hut :arg Hut hut: The hut that needs to be acquired. .. todo:: Refactor this method as an exercise Example: Can you use self.enemy instead of calling hut.occupant every time? """ print_bold("Entering hut %d..." % hut.number, end=' ') is_enemy = (isinstance(hut.occupant, AbstractGameUnit) and hut.occupant.unit_type == 'enemy') continue_attack = 'y' # Code block that tells what to do when you see, an enemy or a friend # or no one in the hut. # TODO: Refactor this. 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("\n...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 acquire_hut(self, hut): """Fight the combat (command line) to acquire the hut .. todo:: acquire_hut method can be refactored. Example: Can you use self.enemy instead of calling hut.occupant every time? """ 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 not in ["n", "y"]: print("Error. Respond with n or y") continue 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 acquire_hut(self, hut): """Borba za kucicu """ print_bold("Ulazim u kucicu broj %d..." % hut.number, end=' ') is_neprijatelj = (isinstance(hut.occupant, GameUnit) and hut.occupant.unit_type == 'neprijatelj') continue_attack = 'd' if is_neprijatelj: print_bold("Neprijatelj na vidiku!") self.show_health(bold=True, end=' ') hut.occupant.show_health(bold=True, end=' ') while continue_attack: continue_attack = input(".......nastavi s napadom? (d/n): ") if continue_attack == 'n': self.run_away() break # napadaj dok je 'd' upisan elif continue_attack == 'd': self.attack(hut.occupant) # ukoliko zdravlje neprijatelja padne na 0 ili manje if hut.occupant.health_meter <= 0: print("") # zauzmi kucicu hut.acquire(self) break if self.health_meter <= 0: print("") break else: raise Napaderror() else: if hut.get_occupant_type() == 'slobodna': print_bold("Kućica je slobodna") else: print_bold("Prijatelj na vidiku!") hut.acquire(self) self.heal()
def acquire_hut(self, hut): """Fight the combat (command line) to acquire the hut .. todo:: acquire_hut method can be refactored. Example: Can you use self.enemy instead of calling hut.occupant every time? """ 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 = raw_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 acquire_hut(self, hut): """Fight the combat (command line) to acquire the hut :arg Hut hut: The hut that needs to be acquired. """ print_bold("Entering hut {0:d}...".format(hut.number), end=' ') is_enemy = (isinstance(hut.occupant, AbstractGameUnit) and hut.occupant.unit_type == 'enemy') continue_attack = 'y' if is_enemy: self.enemy = hut.occupant self.show_health(bold=True, end=' ') self.enemy.show_health(bold=True, end=' ') while continue_attack: try: continue_attack = input(".......continue attack? (y/n): ") assert continue_attack in ('y', 'n') except AssertionError: print( "Please, select either 'y' for attack or 'n' to run away" ) continue if continue_attack == 'n': self.run_away() break self.attack(self.enemy) if self.enemy.health_meter <= 0: print("") hut.acquire(self) break if self.health_meter <= 0: print("") break else: if hut.get_occupant_type() == 'unnocupied': print_bold("Hut is unoccupied") else: print_bold("Friend sighted!") hut.acquire(self) self.heal()
def can_not_jump(): """ 无法跳跃""" print_bold(" --> CanNotJump.jump: I can't jump:-( ")
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 run_away(self): """Metoda za napustanje borbe """ print_bold("BJEZANJE...") self.neprijatelj = None
def horse_jump(): """ 骑士的跳跃能力""" print_bold(" --> HorseJump.jump: Jumping my horse. ")
def power_jump(): """ 超级跳跃能手""" print_bold(" --> PowerJump.jump: I can jump 100 feet from the ground !! ")
def acquire(self, new_occupant): """Update the occupant of this hut""" self.occupant = new_occupant self.is_acquired = True print_bold("GOOD JOB! Hut %d acquired" % self.number)
def acquire(self, new_occupant): """Update the occupant of this hut""" self.occupant = new_occupant self.is_acquired = True # refactor print_bold("Good Job!!! Hut %d acquired" % self.number)
def acquire(self, new_occupant): """Azuriranje okupanta u kucici""" self.occupant = new_occupant self.is_acquired = True print_bold("Kucica broj {} zauzeta".format(self.number))
def show_game_mission(self): """Ispis misije igre""" print_bold("Misija:") print(" 1. Bori se s neprijateljem.") print(" 2. Stavi sve kucice pod svoju kontrolu") print("---------------------------------------------------------\n")