Advertisement
JmihPodvalbniy

Untitled

Oct 28th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | Software | 0 0
  1. Дз от 23.10.2024г.
  2.  
  3. 1)
  4. import abc
  5.  
  6. class Character(abc.ABC):
  7.     @abc.abstractmethod
  8.     def attack(self, target):
  9.         pass
  10.  
  11.     @abc.abstractmethod
  12.     def defend(self, damage):
  13.         pass
  14.  
  15. class Warrior(Character):
  16.     def __init__(self, name, weapon):
  17.         self.name = name
  18.         self.weapon = weapon
  19.         self.health = 100
  20.  
  21.     def attack(self, target):
  22.         print(f"{self.name} атакует {target.name} с помощью оружия: {self.weapon.name}!")
  23.         target.defend(self.weapon.damage)
  24.  
  25.     def defend(self, damage):
  26.         print(f"{self.name} получает {damage} урона.")
  27.         self.health -= damage
  28.         print(f'Состояние здоровья воина: {self.health}')
  29.         if self.health <= 0:
  30.             print(f"{self.name} погиб!")
  31.  
  32. class Mage(Character):
  33.     def __init__(self, name, spell):
  34.         self.name = name
  35.         self.spell = spell
  36.         self.health = 80
  37.  
  38.     def attack(self, target):
  39.         print(f"{self.name} использует магическое заклинание {self.spell.name}!")
  40.         target.defend(self.spell.damage)
  41.  
  42.     def defend(self, damage):
  43.         print(f"{self.name} защищается барьером!")   # (снижает получаемый урон на 50%)
  44.         self.health -= damage * 0.5
  45.         print(f'Состояние здоровья Мага: {self.health}')
  46.         if self.health <= 0:
  47.             print(f"{self.name} погиб!")
  48.  
  49. class Weapon:
  50.     def __init__(self, name, damage):
  51.         self.name = name
  52.         self.damage = damage
  53.  
  54. class Spell:
  55.     def __init__(self, name, damage):
  56.         self.name = name
  57.         self.damage = damage
  58.  
  59. """Пример использования"""
  60. sword = Weapon("Катана", 20)
  61. fireball = Spell("fireball", 30)
  62.  
  63. warrior = Warrior("Артур", sword)
  64. mage = Mage('Омникус', fireball)
  65.  
  66. warrior.attack(mage)
  67. mage.attack(warrior)
  68.  
  69. 2)
  70. import abc
  71.  
  72. class Book:
  73.  
  74.     def __init__(self, title, author):
  75.         self.title = title
  76.         self.author = author
  77.  
  78.     @abc.abstractmethod
  79.     def get_summary(self):
  80.         pass
  81.  
  82. class Fiction(Book):
  83.     def get_summary(self):
  84.         return f'{self.title} - это художественная книга, написанная автором: {self.author}'
  85.  
  86. class NonFiction(Book):
  87.     def get_summary(self):
  88.         return f'{self.title} - это информативная книга, написанная автором: {self.author}'
  89.  
  90. class Poetry(Book):
  91.     def get_summary(self):
  92.         return f'{self.title} - это сборник стихов от автора: {self.author}'
  93.  
  94. """Пример использования с созданием списка книг"""
  95. books = [
  96.     Fiction("Муму", "Иван Тургенев"),
  97.     NonFiction("Бойцовский клуб", "Чак Паланик"),
  98.     Poetry("Сборник стихотворений", "Александр Пушкин")
  99. ]
  100.  
  101. for book in books:
  102.     print(book.get_summary())
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement