-
Notifications
You must be signed in to change notification settings - Fork 0
/
hero.py
84 lines (69 loc) · 2.74 KB
/
hero.py
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
from monster import Monster
from monster_team import MonsterTeam
from monster_custom import Wolf
import random
import text_utils
class Hero():
def __init__(self, name: str, gold: int = 0):
self.name = name
self.health = 100.00
self.power = 50.00
self.gold = 0
self.critical_chance = 0.09
@property
def is_alive(self) -> bool:
""" Checks if hero is alive or not """
return self.health > 0
def __str__(self) -> str:
power = f'{self.power} power'
bar = text_utils.health_bar(self.health)
name_n_health = f'{self.name:<10} [{bar}] {self.health} HP'
pwr_crit_gold = f'{power} | {round(100 * self.critical_chance, 2)}% crit chance'
pwr_crit_gold += f' | {self.gold} gold'
return text_utils.nyoko_scroll([name_n_health, pwr_crit_gold])
def attack_specific(self, enemy: object) -> None:
""" Attacks a specific enemy, enemy counter attacks """
if not isinstance(enemy, Monster):
raise TypeError("Function takes a Monster object")
# Tries for critical damage
if random.random() < self.critical_chance:
enemy.health -= (2 * self.power)
critical = True
else:
enemy.health -= self.power
critical = False
enemy.health = round(enemy.health, 2)
self.health -= enemy.power
self.health= round(self.health, 2)
return critical
def attack(self, monster_team: object) -> None:
""" Damages random, hero suffers counter damage"""
if not isinstance(monster_team, MonsterTeam):
raise TypeError("function takes a MonsterTeam")
enemy = monster_team.get_alive_monster()
# Tries for critical strike chance
return self.attack_specific(enemy)
def block(self, monster_team: object) -> None:
""" Counterattacks enemy, hero takes 30% damage """
if not isinstance(monster_team, MonsterTeam):
raise TypeError("function takes a MonsterTeam")
enemy = monster_team.get_alive_monster()
# Tries for critical strike chance
if random.random() < self.critical_chance:
enemy.health -= (2 * self.power) * 0.5
critical = True
else:
enemy.health -= self.power * 0.5
critical = False
enemy.health = round(enemy.health, 2)
self.health -= enemy.power * 0.3
self.health = round(self.health, 2)
return critical, enemy
if __name__ == "__main__":
hati = Hero("Hati")
wolf = Wolf(3)
print(hati)
hati.critical_chance = 1
critical = hati.attack(wolf)
if critical:
print(f'You hit {type(wolf).__name__} for critical damage!')