-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathc06_dnd.py
43 lines (30 loc) · 936 Bytes
/
c06_dnd.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
#!/usr/bin/env python3
"""
@author Michele Tomaiuolo - https://tomamic.github.io/
@license This software is free - https://opensource.org/license/mit
"""
from random import randint
class DnDCharacter:
def __init__(self, name: str):
self._name = name
self._hp = randint(15, 30) # hit points
def hit(self, damage: int) -> None:
self._hp = max(self._hp - damage, 0)
def heal(self, cure: int) -> None:
if (self.alive()):
self._hp = min(self._hp + cure, 30)
def alive(self) -> bool:
return self._hp > 0
def describe(self) -> str:
return f"I’m {self._name}. I have {self._hp} hit points."
def main():
c = DnDCharacter("Hero")
print(c.describe())
for _ in range(3):
c.hit(randint(5, 10))
print(c.describe())
c.heal(randint(5, 10))
print(c.describe())
print(c.alive())
if __name__ == "__main__":
main()