/
nk1383
/
oop
Обзор
Документация
Войти
/
nk1383
/
oop
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
PROJECT.py
207 строк
6 KB
Анастасия Ковалевич
update PROJECT.py
04 ноя 2025, 14:50
04 ноя 2025, 14:50
bc0bd88
Код
Авторство
О чём код?
import time from random import random class Hero: def __init__(self, name, max_hp, atk): self._name = name self._max_hp = max_hp self._hp = max_hp self._atk = atk @property def name(self): return self._name @property def max_hp(self): return self._max_hp @property def hp(self): return self._hp @property def atk(self): return self._atk def alive(self): return self._hp > 0 def take_damage(self, damage): actual_damage = max(1, int(damage)) self._hp = max(0, self._hp - actual_damage) print(f"{self.name} takes {actual_damage} damage! {self.name}'s HP: {self._hp}") def attack(self, other): damage = self.atk print(f"{self.name} attacks {other.name} for {damage} damage!") other.take_damage(damage) def heal(self, amount): old_hp = self._hp self._hp = min(self._hp + amount, self._max_hp) healed = self._hp - old_hp if healed > 0: print(f"{self.name} heals {healed} HP! Now {self._hp} HP") class Knight(Hero): def __init__(self): super().__init__('knight', max_hp=100, atk=15) self.potion = 2 self.shield = False def use_potion(self): if self.potion > 0: self.heal(20) self.potion -= 1 print(f"{self.name} uses potion! Potions left: {self.potion}") return True else: print(f"{self.name} has no potions left!") return False def activate_shield(self): if not self.shield: self.shield = True print(f"{self.name} activates shield!") else: print(f"{self.name} already has shield!") def take_damage(self, damage): if self.shield: damage = int(damage * 0.5) self.shield = False print(f"Shield reduces damage to {damage}!") super().take_damage(damage) def attack(self, other): if random() < 0.2: crit_damage = self.atk * 1.8 print(f"{self.name} CRITICAL HIT!") other.take_damage(crit_damage) else: super().attack(other) class Dragon(Hero): def __init__(self): super().__init__('dragon', max_hp=100, atk=18) self.fire = 2 self.regen_turns = 0 self.fire_used_this_fight = False def use_fireball(self, other): if self.fire> 0: fire_damage = self.atk * 1.8 print(f"{self.name} uses FIRE!") other.take_damage(fire_damage) self.fire -= 1 self.fire_used_this_fight = True def healing(self): self.heal(10) self.regen_turns = 0 def action(self, knight): self.regen_turns += 1 if self._hp < 30 and self.fire > 0: self.use_fire(knight) elif self.regen_turns >= 3 and self._hp < self._max_hp: print(f"{self.name} regenerates 10 HP!") self.healing() else: self.attack(knight) class Game: def __init__(self): self.knight = Knight() self.dragon = Dragon() self.round = 0 self.diplomacy_counter = 0 def start(self): print("start fight") def run_display(self): print("\n" + "=" * 50) print(f"Round {self.round}") print(f"{self.knight.name}: HP {self.knight.hp}/{self.knight.max_hp}") print(f"{self.dragon.name}: HP {self.dragon.hp}/{self.dragon.max_hp}") print("=" * 50) def knight_turn(self): print(f"\n{self.knight.name}'s turn:") print("1. Attack") print("2. Use potion(x2)") print("3. Activate shield") print("4. Diplomacy (try to talk)") valid_choices = {"1", "2", "3", "4"} while True: choice = input("Choose action (1-4): ").strip() if choice in valid_choices: if choice == "1": self.knight.attack(self.dragon) elif choice == "2": self.knight.use_potion() elif choice == "3": self.knight.activate_shield() elif choice == "4": print("knight decide diplomacy...") self.diplomacy_counter += 1 break else: print("Invalid choice! Please enter 1, 2, 3, or 4.") def dragon_turn(self): print(f"\n{self.dragon.name}'s turn:") time.sleep(1) if self.dragon.alive(): self.dragon.action(self.knight) def check_end(self): if self.diplomacy_counter >= 3: print("diplomacy!") return True if not self.knight.alive(): print("\n💀 dragon wins!") return True if not self.dragon.alive(): print("\n knight wins!") return True return False def runs(self): print("war begin!") print("your name is knight") time.sleep(1) while True: self.round += 1 self.run_display() self.knight_turn() if self.check_end(): break self.dragon_turn() if self.check_end(): break time.sleep(1) print("\ngame over!") if __name__ == "__main__": game = Game() game.runs()