-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattelexample.py
More file actions
107 lines (76 loc) · 3.05 KB
/
battelexample.py
File metadata and controls
107 lines (76 loc) · 3.05 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# ---------- WARRIORS BATTLE ----------
# We will create a game with this sample output
'''
Sam attacks Paul and deals 9 damage
Paul is down to 10 health
Paul attacks Sam and deals 7 damage
Sam is down to 7 health
Sam attacks Paul and deals 19 damage
Paul is down to -9 health
Paul has Died and Sam is Victorious
Game Over
'''
# We will create a Warrior & Battle class
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, attkMax=0, blockMax=0):
self.name = name
self.health = health
self.attkMax = attkMax
self.blockMax = blockMax
def attack(self):
# Randomly calculate the attack amount
# random() returns a value from 0.0 to 1.0
attkAmt = self.attkMax * (random.random() + .5)
return attkAmt
def block(self):
# Randomly calculate how much of the attack was blocked
blockAmt = self.blockMax * (random.random() + .5)
return blockAmt
# 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 startFight(self, warrior1, warrior2):
# Continue looping until a Warrior dies switching back and
# forth as the Warriors attack each other
while True:
if self.getAttackResult(warrior1, warrior2) == "Game Over":
print("Game Over")
break
if self.getAttackResult(warrior2, warrior1) == "Game Over":
print("Game Over")
break
# A function will receive each Warrior that will attack the other
# Have the attack and block amounts be integers to make the results clean
# Output the results of the fight as it goes
# If a Warrior dies return that result to end the looping in the
# above function
# Make this method static because we don't need to use self
@staticmethod
def getAttackResult(warriorA, warriorB):
warriorAAttkAmt = warriorA.attack()
warriorBBlockAmt = warriorB.block()
damage2WarriorB = math.ceil(warriorAAttkAmt - warriorBBlockAmt)
warriorB.health = warriorB.health - damage2WarriorB
print("{} attacks {} and deals {} damage".format(warriorA.name,
warriorB.name, damage2WarriorB))
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():
# Create 2 Warriors
paul = Warrior("Paul", 50, 20, 10)
sam = Warrior("Sam", 50, 20, 10)
# Create Battle object
battle = Battle()
# Initiate Battle
battle.startFight(paul, sam)
main()