main.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import json
  2. import random
  3. import time
  4. import sys
  5. class House:
  6. def __init__(self, house_name, name, slogan, starting_gold, starting_health, damage, gold_won, gold=0, health=0):
  7. self.house_name = house_name
  8. self.name = name
  9. self.slogan = slogan
  10. self.starting_gold = starting_gold
  11. self.starting_health = starting_health
  12. self.damage = damage
  13. self.gold_won = gold_won
  14. self.gold = starting_gold
  15. self.health = starting_health
  16. def __repr__(self):
  17. return f"<{self.house_name}>"
  18. def get_damage(self, damage):
  19. self.health -= damage
  20. printc(Colors.CYAN, f"{self.name} get {damage} point damage. Remaining HP: {0 if self.health < 0 else self.health}")
  21. class Player(House):
  22. def __repr__(self):
  23. return f"<Player '{self.name}' hp:{self.health} gold:{self.gold}>"
  24. def make_move(self):
  25. if self.health > 1:
  26. printc(Colors.BLUE, "What is your move?")
  27. printc(Colors.BLUE, "Fight: f")
  28. printc(Colors.BLUE, "Retreat: r")
  29. move = input('>')
  30. if move == "f":
  31. if random.randint(0, 4) > 0:
  32. time.sleep(random.randint(1, 3))
  33. printc(Colors.GREEN, "You hit your enemy")
  34. enemy.get_damage(self.damage)
  35. enemy.make_move()
  36. else:
  37. time.sleep(random.randint(1, 3))
  38. printc(Colors.WARNING, "You miss. Enemy's turn.")
  39. enemy.make_move()
  40. elif move == "r":
  41. player.retreat()
  42. else:
  43. printc(Colors.FAIL, "Wrong move.")
  44. player.make_move()
  45. else:
  46. printc(Colors.FAIL, "You defeated by your enemy.")
  47. printc(Colors.BOLD, f"{enemy.name} said: {enemy.slogan}")
  48. def retreat(self):
  49. printc(Colors.FAIL, f"You choose to retreat.\nHealth: {self.health}. Total reward: {self.gold}")
  50. printc(Colors.BOLD, f"{enemy.name} said: {enemy.slogan}")
  51. sys.exit()
  52. class Enemy(House):
  53. def __repr__(self):
  54. return f"<Enemy '{self.name}' hp:{self.health} gold:{self.gold}>"
  55. def make_move(self):
  56. if self.health > 1:
  57. if random.randint(0, 5) < 3:
  58. time.sleep(random.randint(1, 3))
  59. player.get_damage(self.damage)
  60. player.make_move()
  61. else:
  62. time.sleep(random.randint(1, 3))
  63. printc(Colors.GREEN, "Enemy missed. Your turn.")
  64. player.make_move()
  65. else:
  66. printc(Colors.GREEN, f"You defeated your enemy {self.name}. {len(house_list)} enemies left.")
  67. printc(Colors.BOLD, f"You said: {player.slogan}")
  68. player.gold += player.gold_won
  69. if player.gold > player.starting_gold:
  70. player.gold = player.starting_gold
  71. class Colors:
  72. HEADER = '\033[95m'
  73. BLUE = '\033[94m'
  74. CYAN = '\033[96m'
  75. GREEN = '\033[92m'
  76. WARNING = '\033[93m'
  77. FAIL = '\033[91m'
  78. END = '\033[0m'
  79. BOLD = '\033[1m'
  80. UNDERLINE = '\033[4m'
  81. def printc(color, *text):
  82. print(*map(lambda t: f"{color}{t}{Colors.END}", text))
  83. def select_char(t, house):
  84. return t(**house.__dict__)
  85. def generate_house_list():
  86. _house_list = []
  87. with open("house_data.json") as house_data:
  88. house_data = json.loads(house_data.read())
  89. return [House(data['name'], data['hero'], data['slogan'], data['starting_gold'], data['starting_health'],
  90. data['damage'], data['gold_won']) for data in house_data]
  91. def get_selected_house():
  92. for i, house in enumerate(house_list):
  93. printc(Colors.BLUE, i + 1, house.house_name)
  94. try:
  95. s = int(input('Which house you are belong to? :'))
  96. if 1 > s > 11:
  97. raise ValueError
  98. selected = house_list[s - 1]
  99. house_list.remove(selected)
  100. return selected
  101. except ValueError:
  102. printc(Colors.FAIL, "Wrong answer! You have to enter valid number.")
  103. get_selected_house()
  104. def make_first_attack():
  105. if random.randint(0, 1) == 0:
  106. player.make_move()
  107. else:
  108. enemy.make_move()
  109. def ask_for_health():
  110. printc(Colors.CYAN, f"Your remaining HP: {player.health} Gold: {player.gold}")
  111. printc(Colors.BLUE, "How much HP you want to buy? (0 for none)")
  112. try:
  113. amount = int(input(">"))
  114. if -1 < amount < player.starting_health and amount + player.health <= player.starting_health and amount <= player.gold:
  115. player.gold -= amount
  116. player.health += amount
  117. printc(Colors.CYAN, f"You bought {amount} HP.")
  118. else:
  119. raise ValueError
  120. except ValueError:
  121. printc(Colors.FAIL, "You cannot buy this amount of health")
  122. ask_for_health()
  123. if __name__ == '__main__':
  124. logo = """\
  125. '...............................,
  126. @ @ @ `
  127. @ ' # +:' @ @ @ @,@ +;'` @ .. @;@ # #
  128. +` @ @ '` ; .` .` @ @ @ @ ; @ ; ' @@ @ +
  129. # #. +@ :+ ;`, @` .: @ @;;;@ @ `` ; :@ @ @`' ';
  130. +, @ +'@ ',`; @ ; #` ;: @ @ @ @,@ `. ; '@ @ @ ` :'
  131. @ @ . @ . @: @ ; ., ; @ @ @ @ + @ ; . @ `+ @ @
  132. ;@ @ @ .@`; ` @.##; @ @` @ @. .+ #'; @` ` @:@ ';
  133. #@@, ';;
  134. """
  135. print(logo)
  136. house_list = generate_house_list()
  137. selected_house = get_selected_house()
  138. player = select_char(Player, selected_house)
  139. printc(Colors.CYAN, f"You're {player.name}. You have {player.health} health points and {player.gold} golds.")
  140. printc(Colors.CYAN, "You must defeat your 10 enemies to reach Iron Throne.")
  141. printc(Colors.CYAN, "You can buy health point with your golds after each round. 1 HP = 1 GOLD")
  142. printc(Colors.CYAN, "Good luck...")
  143. print()
  144. time.sleep(2)
  145. random.shuffle(house_list)
  146. while len(house_list) > 0 and player.health > 1:
  147. enemy = select_char(Enemy, house_list.pop())
  148. time.sleep(1)
  149. if len(house_list) <= 8:
  150. ask_for_health()
  151. printc(Colors.CYAN, f"You're facing your enemy {enemy.name} right now.")
  152. make_first_attack()
  153. if len(house_list) == 0:
  154. printc(Colors.GREEN, "† You reach the Iron throne †")