-
Notifications
You must be signed in to change notification settings - Fork 1
/
section_19.py
60 lines (52 loc) · 2.21 KB
/
section_19.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
import random
import math
# Warriors will have names, health, and attack and block maximums
# They will have the capabilities to attack and block random amounts
class Warrior:
def __init__(self, name="warrior", health=0, attk_max=0, block_max=0):
self.name = name
self.health = health
self.attk_max = attk_max
self.block_max = block_max
def attack(self):
# Randomly calculate the attack amount
# random() returns a value from 0.0 to 1.0
attk_amt = self.attk_max * (random.random() + .5)
return attk_amt
def block(self):
# Randomly calculate how much of the attack was blocked
block_amt = self.block_max * (random.random() + .5)
return block_amt
# The Battle class will have the capability to loop until 1 Warrior dies
# The Warriors will each get a turn to attack each turn
class Battle:
def start_fight(self, warrior1, warrior2):
# Continue looping until a Warrior dies switching back and
# forth as the Warriors attack each other
while True:
if self.get_attack_result(warrior1, warrior2) == "Game Over":
print("Game Over")
break
if self.get_attack_result(warrior2, warrior1) == "Game Over":
print("Game Over")
break
# Make this method static because we don't need to use self
@staticmethod
def get_attack_result(self, warriorA, warriorB):
warrior_a_attk_amt = warriorA.attack()
warrior_b_block_amt = warriorB.block()
damage_2_warrior_b = math.ceil(warrior_a_attk_amt - warrior_b_block_amt)
warriorB.health = warriorB.health - damage_2_warrior_b
print("{} attacks {} and deals {} damage".format(warriorA.name, warriorB.name, damage_2_warrior_b))
print("{} is down to {} health".format(warriorB.name, warriorB.health))
if warriorB.health <= 0:
print("{} has died and {} is victorious!".format(warriorB.name, warriorA.name))
return "Game Over"
else:
return "Fight Again"
def main():
thor = Warrior("Thor", 50, 20, 10)
loki = Warrior("Loki", 50, 20, 10)
battle = Battle()
battle.start_fight(thor, loki)
main()