Advertisement
horozov86

hero_testing_exercise

Jul 25th, 2023
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. class Hero:
  2.     username: str
  3.     health: float
  4.     damage: float
  5.     level: int
  6.  
  7.     def __init__(self, username: str, level: int, health: float, damage: float):
  8.         self.username = username
  9.         self.level = level
  10.         self.health = health
  11.         self.damage = damage
  12.  
  13.     def battle(self, enemy_hero):
  14.         if enemy_hero.username == self.username:
  15.             raise Exception("You cannot fight yourself")
  16.  
  17.         if self.health <= 0:
  18.             raise ValueError("Your health is lower than or equal to 0. You need to rest")
  19.  
  20.         if enemy_hero.health <= 0:
  21.             raise ValueError(f"You cannot fight {enemy_hero.username}. He needs to rest")
  22.  
  23.         player_damage = self.damage * self.level
  24.         enemy_hero_damage = enemy_hero.damage * enemy_hero.level
  25.  
  26.         self.health -= enemy_hero_damage
  27.         enemy_hero.health -= player_damage
  28.  
  29.         if self.health <= 0 and enemy_hero.health <= 0:
  30.             return "Draw"
  31.  
  32.         if enemy_hero.health <= 0:
  33.             self.level += 1
  34.             self.health += 5
  35.             self.damage += 5
  36.             return "You win"
  37.  
  38.         enemy_hero.level += 1
  39.         enemy_hero.health += 5
  40.         enemy_hero.damage += 5
  41.         return "You lose"
  42.  
  43.     def __str__(self):
  44.         return f"Hero {self.username}: {self.level} lvl\n" \
  45.                f"Health: {self.health}\n" \
  46.                f"Damage: {self.damage}\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement