-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
248 lines (210 loc) · 11.3 KB
/
classes.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import random
class Player:
# To create the Player character user input is collected to generate a name, age and desired fantasy class,
# Class and fantasy race determines the characters primary stats upon initialization,
# Stats are categorized as strength, dexterity and wisdom. Defeating monsters raises stats,
# The Player character is not dead upon initialization
# The Player health is initially set to 20, raised by 5 with each successful vanquished monster encounter
def __init__(self, name, age, race, combat_class):
self.name = name
self.age = age
self.race = race
self.combat_class = combat_class
self.health = 30
self.strength = 0
self.dexterity = 0
self.wisdom = 0
self.is_dead = False
# Control flow statement to raise Player stats based on fantasy race selection
if self.race == 'human':
self.strength += 10
self.dexterity += 5
self.wisdom += 3
elif self.race == 'elf':
self.strength += 3
self.dexterity += 10
self.wisdom += 5
else:
self.strength += 7
self.dexterity += 3
self.wisdom += 5
# Control flow statement to raise Player stats based on fantasy class selection
if self.combat_class == 'warrior':
self.strength += 10
self.dexterity += 5
self.wisdom += 3
elif self.combat_class == 'archer':
self.strength += 3
self.dexterity += 10
self.wisdom += 5
else:
self.strength += 3
self.dexterity += 5
self.wisdom += 10
def __repr__(self):
# Printing the Player character will provide a description including their name and stats
# Modify combat_class string from lower() to title() for the description
self.combat_class = self.combat_class.title()
description = 'Greetings {name}! You are a {race} from the Realm of Python! You are a {combat_class} class and your current health is at {health}. Your strength is {strength}, your dexterity is {dexterity} and your wisdom is {wisdom}.'.format(name=self.name, race=self.race.title(), combat_class=self.combat_class.title(), health=self.health, strength=self.strength, dexterity=self.dexterity, wisdom=self.wisdom)
# Control flow to adjust description based on whether the Player character is dead or not
if self.is_dead:
description += ' Sadly you are dead. You lived to about {age} years of age.'.format(age=self.age)
else:
description += ' Fortunately you have not yet died! You are {age} years of age.'.format(age=self.age)
return description
def player_dies(self, monster):
# If the player loses all of their health then the is_dead attribute is switched to false
self.is_dead = True
# A negative value could potentially result in a bug where the player continues to live despite having 0 health
# Set health to 0 to prevent a bug possibility
if self.health != 0:
self.health = 0
print('Oh no! {name} died fighting {monster}!'.format(name=self.name, monster=monster.name))
def gain_health(self):
# If the player successfully defeats a monster, increase their health by 2
self.health += 2
def attack_monster(self, monster):
# Attack patterns are randomly generated
# Depending on the randomly generated attack, attack strength depends on relevant stat compared to monster's
# Generate random attack pattern
random_attack = random.randint(1,3)
if random_attack == 1:
if self.strength > monster.strength:
monster.health -= self.strength / 1.5
print('Your strength is superior to that of the {monster}. Your attack does full strength damage.'.format(monster=monster.name.title()))
else:
monster.health -= self.strength / 2
print('The {monster}\'s strength is greater than yours. Your attack has reduced strength damage.'.format(monster=monster.name.title()))
elif random_attack == 2:
if self.dexterity > monster.dexterity:
monster.health -= self.dexterity / 1.5
print('You prove to be more nimble than the {monster}. You land an attack for full dexterity damage.'.format(monster=monster.name.title()))
else:
monster.health -= self.dexterity / 2
print('The {monster}\'s proves to be more dexterous than you. Your attack has reduced dexterity damage.'.format(monster=monster.name.title()))
else:
if self.wisdom > monster.wisdom:
monster.health -= self.wisdom / 1.5
print('You are much wiser than your foe, the {monster}. Your attack does full wisdom damage.'.format(monster=monster.name.title()))
else:
monster.health -= self.wisdom / 2
print('Your mental fortitude is outmatched by the {monster}. Your attack has reduced wisdom damage.'.format(monster=monster.name.title()))
def boost_stats(self, monster):
# Boosted stat depends on the monster fought
if monster.name == 'ogre':
self.strength += 3
self.dexterity += 1
self.wisdom += 1
elif monster.name == 'goblin':
self.strength += 1
self.dexterity += 3
self.wisdom += 1
else:
self.strength += 1
self.dexterity += 1
self.wisdom += 3
# The Monster class is the parent class for all monster based subclasses
class Monster:
# Attributes shared by all monsters
def __init__(self):
self.is_dead = False
# Methods shared by all monsters
def monster_dies(self):
# If the Ogre loses all its health then the is_dead attribute is switched to true
self.is_dead = True
# A negative value could potentially result in a bug where the monster continues to live despite having 0 health
# Set health to 0 to prevent a bug possibility
if self.health != 0:
self.health = 0
def attack_player(self, player):
# Attack patterns are randomly generated
# Depending on the randomly generated attack, attack strength depends on relevant stat compared to monster's
# Generate random attack pattern
random_attack = random.randint(1,3)
if random_attack == 1:
if self.strength > player.strength:
player.health -= self.strength / 1.25
print('The {monster}\'s strength is greater than {player}. The attack does full strength damage.'.format(monster=self.name.title(), player=player.name))
else:
player.health -= self.strength / 2
print('{player}\'s strength is greater than {monster}\'s. The attack has reduced strength damage.'.format(monster=self.name.title(), player=player.name))
elif random_attack == 2:
if self.dexterity > player.dexterity:
player.health -= self.dexterity / 1.25
print('The {monster} appears to be more nimble than the {player}. The attack lands for full dexterity damage.'.format(monster=self.name.title(), player=player.name))
else:
player.health -= self.dexterity / 2
print('{player} proves to be more dexterous than the {monster}. The attack has reduced dexterity damage.'.format(monster=self.name.title(), player=player.name))
else:
if self.wisdom > player.wisdom:
player.health -= self.wisdom / 1.25
print('Despite being a {monster}, it\'s wisdom is higher than {player}. The attack does full wisdom damage.'.format(monster=self.name.title(), player=player.name))
else:
player.health -= self.wisdom / 2
print('The mental fortitude of the {monster} is weaker than {player}. Your attack has reduced wisdom damage.'.format(monster=self.name.title(), player=player.name))
def raise_stats(self):
# To make the monsters competitive, raise stats after each defeat
self.strength += 2
self.dexterity += 2
self.wisdom += 2
class Ogre(Monster):
# The Ogre is the strongest of the common monsters with the highest strength and health stats
# Attributes of the Ogre class
def __init__(self):
self.name = 'ogre'
self.health = 35
self.strength = 10
self.dexterity = 7
self.wisdom = 5
class Goblin(Monster):
# The Goblin is the most nimble of the common monsters with the highest dexterity stat but lowest health
# Attributes of the Goblin class
def __init__(self):
self.name = 'goblin'
self.health = 20
self.strength = 5
self.dexterity = 10
self.wisdom = 7
class Spectre(Monster):
# The Spectre is an ethereal monster with the highest wisdom stat but the lowest strength stat
# Attributes of the Spectre classS
def __init__(self):
self.name = 'spectre'
self.health = 25
self.strength = 5
self.dexterity = 7
self.wisdom = 10
class BossMonster(Monster):
# Attributes for the boss monster
def __init__(self):
self.name = 'Emberling the Giant Python'
self.health = 100
self.strength = 25
self.dexterity = 25
self.wisdom = 25
def attack_player(self, player):
# Attack patterns are randomly generated
# Depending on the randomly generated attack, attack strength depends on relevant stat compared to monster's
# Generate random attack pattern
random_attack = random.randint(1,3)
if random_attack == 1:
if self.strength > player.strength:
player.health -= self.strength / 1.25
print('{monster}\'s strength is greater than {player}. The attack does full strength damage.'.format(monster=self.name.title(), player=player.name))
else:
player.health -= self.strength / 2
print('{player}\'s strength is greater than {monster}\'s. The attack has reduced strength damage.'.format(monster=self.name.title(), player=player.name))
elif random_attack == 2:
if self.dexterity > player.dexterity:
player.health -= self.dexterity / 1.25
print('{monster} appears to be more dexterous than {player}. The attack lands for full dexterity damage.'.format(monster=self.name.title(), player=player.name))
else:
player.health -= self.dexterity / 2
print('{player} proves to be more dexterous than {monster}. The attack has reduced dexterity damage.'.format(monster=self.name.title(), player=player.name))
else:
if self.wisdom > player.wisdom:
player.health -= self.wisdom / 1.25
print('{monster} has been alive for centuries, it\'s wisdom is higher than {player}. The attack does full wisdom damage.'.format(monster=self.name.title(), player=player.name))
else:
player.health -= self.wisdom / 2
print('The mental fortitude of {monster} is weaker than {player}. Your attack has reduced wisdom damage.'.format(monster=self.name.title(), player=player.name))