Newer
Older
from gameparser import normalise_input
class Player:
def __init__(self):
self.inventory = []
self.current_room = rooms["spawn_room"]
self.alive = True
self.level = 1
self.xp = 0
# stats
self.base_hp = 100
self.base_attack = 10
self.base_defense = 10
self.base_luck = 10
# equipped items
Thomas Glasper
committed
self.equipped = {
Thomas Glasper
committed
"defense": None,
"magic": None,
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
def gain_xp(self, xp):
# calculate required xp for next level
required_xp = self.level * 10
self.xp += xp
print("You gained", str(xp), "xp.")
if self.xp >= required_xp:
self.xp -= required_xp
self.level += 1
print("You levelled up to level", str(self.level) + "!")
while True:
print("")
print("You can now select a base stat to make stronger.")
print("You can use the following commands:")
print("HP to increase your base HP.")
print("ATTACK to increase your base attack.")
print("DEFENSE to increase your base defense.")
print("LUCK to increase your base luck.")
user_input = input("> ")
user_input = normalise_input(user_input)
if user_input[0] == "hp":
print("You increased your base HP!\n")
self.base_hp += 5
break
elif user_input[0] == "attack":
print("You increased your base attack!\n")
self.base_attack += 5
break
elif user_input[0] == "defense":
print("You increased your base defense!\n")
self.base_defense += 5
break
elif user_input[0] == "luck":
print("You increased your base luck!\n")
self.base_luck += 5
break
else:
print("Did not recognise that category.")
def get_equipped_name(self, category):
if self.equipped[category] == None:
return "none"
else:
return self.equipped[category].name
Thomas Glasper
committed
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
def inspect_item(self, item_id):
for item in self.inventory:
if item.id == item_id:
item.inspect()
return
for category in self.equipped:
item = self.equipped[category]
if item.id == item_id:
item.inspect()
return
print("Could not find that item to inspect.")
def equip(self, item_id):
# find item to equip
to_equip = None
for item in self.inventory:
if item.id == item_id:
to_equip = item
if to_equip == None:
print("Cannot find that item to equip.")
return
if type(to_equip) == Weapon:
if self.equipped["weapon"] != None:
print("You already have a weapon equipped, unequip it first.")
return
else:
self.equipped["weapon"] = to_equip
self.inventory.remove(to_equip)
return
if type(to_equip) == Defense:
if self.equipped["defense"] != None:
print("You already have a defense item equipped, unequip it first.")
return
else:
self.equipped["defense"] = to_equip
self.inventory.remove(to_equip)
return
if type(to_equip) == Magic:
if self.equipped["magic"] != None:
print("You already have a magic item equipped, unequip it first.")
return
else:
self.equipped["magic"] = to_equip
self.inventory.remove(to_equip)
return
print("Could not equip that item.\n")
def unequip(self, item_id):
for category in self.equipped:
item = self.equipped[category]
if item != None and item.id == item_id:
self.inventory.append(item)
self.equipped[category] = None
return
print("Could not find that item to unequip.\n")
def get_luck(self):
luck = self.base_luck
if self.equipped["magic"] != None:
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
return luck
def apply_defense(self, damage):
if self.equipped["defense"] != None:
return self.equipped["defense"].get_defense(damage, self.base_defense)
else:
return damage - self.base_defense
def check_attack_missed(self, multiplier=1):
# check if attack missed, higher multiplier means more chance
return (self.get_luck() * multiplier) > random.randint(0, 100)
def get_damage(self, multiplier=1):
damage = self.base_attack
if self.equipped["weapon"] != None:
damage += self.equipped["weapon"].get_attack()
critical = False
if self.get_luck() > random.randint(0, 100):
critical = True
damage *= 2
return int(damage * multiplier), critical
def take_attack_wear(self, wear):
if self.equipped["weapon"] != None:
self.equipped["weapon"].checkout_dura(self, rooms, wear)
return True
return False
def take_damage(self, damage):
self.hp -= damage
if self.hp < 0:
self.alive = False