#rpg

Пользователь

от bankuwer , в категории: Python , 2 года назад
1 ответ последнее сообщение год назад от jaren

Есть код, туда должны добавить ещё три класса героев

  1. Thor, удар по боссу имеет шанс оглушить босса на 1 раунд, вследствие чего босс пропускает 1 раунд и не наносит урон героям
  2. Avrora, которая может входить в режим невидимости на 2 раунда (т.е не получает урон от босса), в тоже время полученный урон в режиме невидимости возвращает боссу в последующих раундах. Она может исчезать только один раз за игру.
  3. Witcher, не наносит урон боссу, но получает урон от босса. Имеет 1 шанс оживить первого погибшего героя, отдав ему свою жизнь, при этом погибает сам

Сам код

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from enum import Enum
import random


class SuperAbility(Enum):
  CRITICAL_DAMAGE = 1
  HEAL = 2
  BOOST = 3
  SAVE_DAMAGE_AND_REVERT = 4


class GameEntity:
  def __init__(self, name, health, damage):
    self.__name = name
    self.__health = health
    self.__damage = damage

  @property
  def name(self):
    return self.__name

  @name.setter
  def name(self, value):
    self.__name = value

  @property
  def health(self):
    return self.__health

  @health.setter
  def health(self, value):
    if value < 0:
      self.__health = 0
    else:
      self.__health = value

  @property
  def damage(self):
    return self.__damage

  @damage.setter
  def damage(self, value):
    self.__damage = value

  def __str__(self):
    return f"{self.__name} health: {self.__health} damage: {self.__damage}"


class Boss(GameEntity):
  def __init__(self, name, health, damage):
    super(Boss, self).__init__(name, health, damage)
    self.__defence = None

  @property
  def defence(self):
    return self.__defence

  def choose_defence(self, heroes):
    selected_hero = random.choice(heroes)
    self.__defence = selected_hero.super_ability

  def hit(self, heroes):
    for hero in heroes:
      if hero.health > 0:
        hero.health -= self.damage

  def __str__(self):
    return "BOSS " + super(Boss, self).__str__() + f" defence: {self.__defence}"


class Hero(GameEntity):
  def __init__(self, name, health, damage, super_ability):
    super(Hero, self).__init__(name, health, damage)
    self.__super_ability = super_ability

  @property
  def super_ability(self):
    return self.__super_ability

  def hit(self, boss):
    if boss.health > 0 and self.health > 0 and self.__super_ability != boss.defence:
      boss.health -= self.damage

  def apply_super_power(self, boss, heroes):
    pass


class Warrior(Hero):
  def __init__(self, name, health, damage):
    super(Warrior, self).__init__(name, health, damage, SuperAbility.CRITICAL_DAMAGE)

  def apply_super_power(self, boss, heroes):
    coef = random.randint(1, 5)
    print(f"Coeficent of critical: {coef}")
    boss.health -= self.damage * coef


class Magic(Hero):
  def __init__(self, name, health, damage):
    super(Magic, self).__init__(name, health, damage, SuperAbility.BOOST)

  def apply_super_power(self, boss, heroes):
    boost_points = random.randint(1, 11)
    print(f"Boost: {boost_points}")
    for hero in heroes:
      if hero != self and hero.health > 0 and hero.super_ability != SuperAbility.BOOST:
        hero.damage += boost_points


class Medic(Hero):
  def __init__(self, name, health, damage, heal_point):
    super(Medic, self).__init__(name, health, damage, SuperAbility.HEAL)
    self.__heal_point = heal_point

  def apply_super_power(self, boss, heroes):
    for hero in heroes:
      if hero != self and hero.health > 0:
        hero.health += self.__heal_point


class Berserk(Hero):
  def __init__(self, name, health, damage):
    super(Berserk, self).__init__(name, health, damage, SuperAbility.SAVE_DAMAGE_AND_REVERT)
    self.__saved_damage = 0

  def apply_super_power(self, boss, heroes):
    self.__saved_damage += boss.damage // 10
    if self.__saved_damage >= 15:
      boss.health -= self.__saved_damage
      print(f"SAVE_DAMAGE_AND_REVERT: {self.__saved_damage}")
      self.__saved_damage = 0


rounds = 1


def is_game_finished(boss, heroes):
  if boss.health <= 0:
    print("Герои победили!")
    return True
  else:
    all_heroes_dead = True
    for hero in heroes:
      if hero.health > 0:
        all_heroes_dead = False
        break
    if all_heroes_dead:
      print("Победил Босс!")
    return all_heroes_dead


def play_round(boss, heroes):
  global rounds
  rounds += 1
  boss.choose_defence(heroes)
  boss.hit(heroes)
  for hero in heroes:
    if hero.health > 0 and hero.super_ability != boss.defence:
      hero.hit(boss)
      hero.apply_super_power(boss, heroes)
  print_statistic(boss, heroes)


def print_statistic(boss, heroes):
  print(f"------------ ROUND {rounds} ------------")
  print(boss)
  for hero in heroes:
    print(hero)


def start_game():
  boss = Boss("Uran", 1000, 50)

  warrior = Warrior("Ahiles", 270, 10)
  medic_1 = Medic("Aibolit", 250, 5, 15)
  medic_2 = Medic("Liza", 290, 10, 5)
  magic = Magic("Merlin", 280, 20)
  berserk = Berserk("Gats", 290, 15)

  heroes = [warrior, medic_1, medic_2, magic, berserk]

  print_statistic(boss, heroes)

  while not is_game_finished(boss, heroes):
    play_round(boss, heroes)


start_game()