-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcharacter.py
271 lines (218 loc) · 8.6 KB
/
character.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# character.py
# Thorin Schmidt
# 11/16/2016
''' Module that contains our game's Character base class
CHANGELOG:
11/17/2016
added a combat_choice method for use by player
11/17/2016
changed the Character class. weapon, armor and potion attributes.
These are all objects imported from the new items module. These
attributes are all implemented as objects. Potions are implemented
additionally as a list of objects. This leaves inventory currently
empty. also, a new constructor parameter has been added:
numberOfPotions = 2. this replaces the old inventory item
["potion", 2]...
Additionally, a number of properties were added to the base class:
strBonus, dexBonus, intBonus, potionCount, potionList, and AC.
see the property docstrings for more information
11/21/2016
added constitution, wisdom, and charisma attributes. modified attack
to do a minimum of 1 damage, as well as added the possibility of a
critical fumble (roll of 1).
11/21/2016
added __str__ method to allow easy printing.
'''
from random import randint
from items import *
class Character(object):
''' Base Character Class '''
def __init__(self,
name = "Average Joe",
maxHealth = 10,
speed = 25,
stamina = 25,
strength = 10,
dexterity = 10,
constitution = 10,
intelligence = 10,
wisdom = 10,
charisma = 10,
numberOfPotions = 2,
inventory = [],
weapon = "",
armor = ""):
''' All values represent the average score '''
self.name = name
self.maxHealth = maxHealth
self.health = maxHealth
self.speed = speed
self.hunger = 100 # 100 = Full, 0 = starving
self.stamina = stamina
self.strength = strength
self.dexterity = dexterity
self.constitution = constitution
self.intelligence = intelligence
self.wisdom = wisdom
self.charisma = charisma
self.inventory = []
for item in inventory:
self.inventory.append(item[:])
self.potions = []
for i in range(numberOfPotions):
self.potions.append(Potion())
if weapon == "":
self.weapon = Weapon()
else:
self.weapon = weapon
if armor == "":
self.armor = Armor()
else:
self.armor = armor
@property
def strBonus(self):
''' calculates d20 OGL bonus for strength'''
return (self.strength//2) - 5
@property
def dexBonus(self):
''' calculates d20 OGL bonus for dexterity'''
return (self.dexterity//2) - 5
@property
def conBonus(self):
''' calculates d20 OGL bonus for dexterity'''
return (self.constitution//2) - 5
@property
def intBonus(self):
''' calculates d20 OGL bonus for intelligence'''
return (self.intelligence//2) - 5
@property
def wisBonus(self):
''' calculates d20 OGL bonus for dexterity'''
return (self.wisdom//2) - 5
@property
def chaBonus(self):
''' calculates d20 OGL bonus for dexterity'''
return (self.charisma//2) - 5
@property
def potionCount(self):
''' counts your potions... :/ <- david, that is an emoji'''
return len(self.potions)
@property
def potionList(self):
''' produces a list of potions by name '''
potionNames = ""
for potion in self.potions:
potionNames += potion.name + ", "
potionNames = potionNames[:-2] #strip off the last ", "
return potionNames
@property
def AC(self):
''' calculates the overall d20 OGL Armor Class (AC) value'''
return 10 + self.dexBonus + self.armor.defense
def get_damaged(self, damage):
''' inflicts damage from an outside source '''
self.health -= damage
def heal(self):
''' randomly heal 1d8+1 points
this method, like the other action methods, returns two values
which may or may not be used by the main program. the first value
is a Boolean: success. Hopefully, that one is self-explanatory.
message is just a text string that gives the game some descriptive
text to give the user.'''
success = False
message = ""
#first check if there is a potion in inventory
if self.potionCount > 0:
#if so, use the last one, then pop it off the list
''' NOTE: this is fine for now, since there's only one type of
potion, but later the user should be given a choice of which
to use...'''
amount = self.potions[-1].use()
self.health += amount
self.potions.pop()
if self.health > self.maxHealth:
self.health = self.maxHealth
success = True
message = self.name + " drinks a potion, and heals " +\
str(amount) + " points."
if not success:
message = self.name + " has no potions!"
return success, message
def flee(self):
''' attempt to flee the combat, based on speed.
this method, like the other action methods, returns two values
which may or may not be used by the main program. the first value
is a Boolean: success. Hopefully, that one is self-explanatory.
message is just a text string that gives the game some descriptive
text to give the user.'''
success = False
message = ""
chance = randint(1,100)
if chance <= self.speed:
success = True
message = "When danger reared it's ugly head,\n" + self.name +\
" bravely turned and fled!"
else:
message = self.name + " tried to flee, but couldn't get away!"
return success, message
def attack(self, enemy):
''' attack another Character
this method, like the other action methods, returns two values
which may or may not be used by the main program. the first value
is a Boolean: success. Hopefully, that one is self-explanatory.
message is just a text string that gives the game some descriptive
text to give the user.'''
success = False
message = ""
roll = randint(1,20)
if roll == 1:
success = False
message = self.name + "fumbles their attack!"
else:
attack = roll + self.strBonus + self.weapon.attack
if attack >= enemy.AC:
damage = self.weapon.damage + self.strBonus
if damage < 1:
damage = 1
enemy.get_damaged(damage)
success = True
message = self.name + " hits " + enemy.name + " and does " +\
str(damage) + " damage."
else:
message = self.name + " misses " + enemy.name + "."
return success, message
def combat_choice(self):
''' player's combat choices'''
choice = input("""
YOU ARE IN COMBAT!
What do you want to do?
You can:
A)ttack
H)eal
F)lee
Your Choice [A/h/f]: """)
return choice
def __str__(self):
info = "NAME: " + self.name + "\n" +\
"-----------------------------------\n" +\
"|STR| "+str(self.strength)+"\t"+str(self.strBonus)+"\n"+\
"|DEX| "+str(self.dexterity)+"\t"+str(self.dexBonus)+"\n"+\
"|CON| "+str(self.constitution)+"\t"+str(self.conBonus)+"\n"+\
"|INT| "+str(self.intelligence)+"\t"+str(self.intBonus)+"\n"+\
"|WIS| "+str(self.wisdom)+"\t"+str(self.wisBonus)+"\n"+\
"|CHA| "+str(self.charisma)+"\t"+str(self.chaBonus)+"\n"+\
"-----------------------------------\n" +\
"Potions: "+self.potionList+"\n"+\
"AC: "+str(self.AC)+"\n"+\
"-----------------------------------\n"
return info
if __name__ == "__main__":
hero = Character(name = "Mr. Peebles")
orc = Character(name = "Magilla")
print(hero.potionCount)
print(hero.potionList)
hero.heal()
print(hero.potionList)
print(hero.attack(orc))
print(hero)
print(orc)