forked from idaeschool/yeardream_04
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prac6_bh.py
50 lines (39 loc) · 1.43 KB
/
prac6_bh.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
# prac6.py
import random
class Character:
def __init__(self, name, health):
self.name = name
self.health = health
def is_alive(self):
return self.health > 0
def attack(self, other):
raise NotImplementedError("Subclasses must implement this method.")
class Player(Character):
def __init__(self, name, health=100):
self.name = name
self.health = health
def attack(self, other:Character):
damage = random.randint(5, 20)
other.health = other.health - damage
print(f"{self.name} attacks {other.name} for {damage} damage. {other.name} now has {other.health} health.")
class Monster(Character):
def __init__(self, name, health=50):
super().__init__(name, health)
def attack(self, other:Character):
## TO-DO ##
damage = random.randint(5, 20)
other.health = other.health - damage
print(f"{self.name} attacks {other.name} for {damage} damage. {other.name} now has {other.health} health.")
def game_loop():
player = Player("Hero")
monster = Monster("Goblin")
while player.is_alive() and monster.is_alive():
player.attack(monster)
if monster.is_alive():
monster.attack(player)
if player.is_alive():
print(f"{player.name} defeated {monster.name}!")
else:
print("You have been defeated by the monster...")
if __name__ == "__main__":
game_loop()