-
Notifications
You must be signed in to change notification settings - Fork 2
/
player.py
executable file
·288 lines (262 loc) · 9.88 KB
/
player.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import random
import items
import mod_slow_text
import mod_sound_effects
import world
import mod_input_validation
import time
class Player():
def __init__(self):
# Inventory on startup
self.inventory = []
# Player Companions
self.companions = []
# Health Points
self.hp = 100
# Armor Points
self.armor = self.best_armor()
# Start Position
self.location_x, self.location_y = world.starting_position # (0, 0)
# Last Position
self.last_x = 0
self.last_y = 0
# End game victory condition
self.victory = False # no victory on start up
def flee(self, tile):
"""Moves the player randomly to an adjacent tile"""
available_moves = tile.adjacent_moves()
r = random.randint(0, len(available_moves) - 1)
self.do_action(available_moves[r])
def last_position(self, x, y):
if self.last_x != x or self.last_y != y:
self.last_x = x
self.last_y = y
return True
else:
return False
def is_alive(self):
# Checks if still alive
return self.hp > 0 # Greater than zero value then you are still alive
def room_inventory(self):
# Prints room inventory
item_number = 1
room = world.tile_exists(self.location_x, self.location_y)
print('***** Area Items *****')
if len(room.room_inventory) == 0:
# Returns if no items
print("There's nothing here. Cold and empty like this world.\n")
return
for item in room.room_inventory:
print('(', item_number, ')', item, '\n')
item_number += 1
# Loot item prompts and validations
user_input = mod_input_validation.yes_or_no('Would you like to take any items?(y/n)')
if user_input == 'n':
return
if user_input == 'y':
room = world.tile_exists(self.location_x, self.location_y)
selection = mod_input_validation.item_select('Select item to take (0 to exit):', len(room.room_inventory))
if selection == 0:
return
else:
# Pick up item from current room
loot = room.room_inventory[int(selection) - 1]
print('You picked up a', loot)
self.inventory.append(loot)
mod_sound_effects.inventory_pickup()
# Removes from room inventory
del room.room_inventory[int(selection) - 1]
self.room_inventory()
def inventory_actions(self):
self.print_inventory()
# Prints inventory actions and executes them
print('---------------------------')
print('Choose an action:')
print('u: Use item(s)')
print('d: Drop item(s)')
print('x: Exit inventory menu')
# Prompts user and validates input
user_input = mod_input_validation.inventory_action('Action:')
# Runs user requested actions
if user_input == 'u':
self.use_inventory()
if user_input == 'd':
self.drop_inventory()
if user_input == 'x':
return
def print_inventory(self):
# Prints inventory menu
item_number = 1
print('***** Inventory *****')
if len(self.inventory) == 0:
# Returns if no items
print('You have no items.\n')
return
for item in self.inventory:
print('(', item_number, ')', item, '\n')
item_number += 1
def use_inventory(self):
# Use item prompts and validation
self.print_inventory()
selection = mod_input_validation.item_select('Select item to use (0 to exit):', len(self.inventory))
use = self.inventory[int(selection) - 1]
# Returns to menu
if use == 0:
return
# Add health, delete from inventory
health_list = ['First Aid Kit', 'Bandages']
if use.name in health_list:
self.add_health(use.health)
del self.inventory[int(selection) - 1]
# Does nothing
else:
print("Not usable in this instance.")
def drop_inventory(self):
# Drop item prompts and validation
self.print_inventory()
selection = mod_input_validation.item_select('Select item to remove (0 to exit):', len(self.inventory))
# Returns to menu if zero
if selection == 0:
return
# Drops item into current room
room = world.tile_exists(self.location_x, self.location_y)
drop = self.inventory[int(selection) - 1]
room.room_inventory.append(drop)
# Removes from player inventory
del self.inventory[int(selection) - 1]
def player_stats(self):
# Prints health and armor
print('***** Player Statistics *****')
print('Health: ', self.hp)
print('Armor: ', self.armor, '\n')
def add_health(self, hp2add):
# Adds health not to exceed 100
self.hp += hp2add
if self.hp > 100:
self.hp = 100
mod_sound_effects.health()
print('Player Health is now:', self.hp)
def move(self, dx, dy):
self.location_x += dx
self.location_y += dy
print(world.tile_exists(self.location_x, self.location_y).intro_text(self))
def move_north(self):
self.move(dx=0, dy=-1)
def move_south(self):
self.move(dx=0, dy=1)
def move_east(self):
self.move(dx=1, dy=0)
def move_west(self):
self.move(dx=-1, dy=0)
def attack(self, enemy):
weapon = self.best_weapon()
if weapon is None:
print('For Christ Sakes!', weapon.description)
print("\nYou use {} against {}!".format(weapon.name, enemy.name))
enemy.hp -= weapon.damage
companion_damage = self.companion_attack(enemy.damage, enemy.hp)
enemy.hp -= companion_damage
self.hp -= (enemy.damage - self.armor * 0.1)
weapon.sound_effect()
time.sleep(1)
print("Your HP: ", self.hp)
if not enemy.is_alive():
print("You killed {}!".format(enemy.name))
mod_sound_effects.killed_enemy()
else:
print("{} HP: {}".format(enemy.name, enemy.hp))
def best_armor(self):
max_hp = 0
for i in self.inventory:
if isinstance(i, items.Armor):
if i.hp > max_hp:
max_hp = i.hp
return max_hp
def best_weapon(self):
weapon = None
max_dmg = 0
for i in self.inventory:
if isinstance(i, items.Weapon):
if i.damage > max_dmg:
max_dmg = i.damage
weapon = i
if weapon is None:
weapon = items.Fists()
return weapon
def companion_actions(self):
self.print_companions()
# Prints actions and executes them
print('---------------------------')
print('Choose an action:')
print('h: Heal companion')
print('x: Exit companions menu')
# Prompts user and validates input
user_input = mod_input_validation.companion_action('Action:')
if user_input == 'h':
self.companion_heal()
if user_input == 'x':
return
def print_companions(self):
# Prints inventory menu
item_number = 1
print('***** Companions *****')
if len(self.companions) == 0:
# Returns if no items
print('You have no friends.\n')
return
for friends in self.companions:
print('(', item_number, ')', friends)
item_number += 1
def companion_heal(self):
# Use item prompts and validation
self.print_companions()
selection = mod_input_validation.item_select('Select companion to heal (0 to exit):', len(self.companions))
companion = self.companions[int(selection) - 1]
if companion == 0:
return
# Player select item to use
self.print_inventory()
selection = mod_input_validation.item_select('Select item to heal companion (0 to exit):', len(self.inventory))
use = self.inventory[int(selection) - 1]
# Returns to menu
if use == 0:
return
# Add health to companion, don't over 100hp, delete from inventory
health_list = ['First Aid Kit', 'Bandages']
if use.name in health_list:
companion.hp += use.health
mod_sound_effects.health()
print('{} HP: {}'.format(companion.name, companion.hp))
del self.inventory[int(selection) - 1]
if companion.hp > 100:
companion.hp = 100
# Does nothing
else:
print("Not usable in this instance.")
def companion_attack(self, enemy_damage, enemy_hp):
# Zero damage returned
if len(self.companions) == 0:
return 0
# Loop through companions on attacks
initial_enemy_hp = enemy_hp
total_damage = 0
for companion in self.companions:
index = 0
print('{} did {} damage.'.format(companion.name, companion.damage))
companion.hp -= enemy_damage
enemy_hp -= companion.damage
total_damage += companion.damage
if companion.hp <= 0:
del self.companions[index]
mod_sound_effects.companion_death()
mod_slow_text.super_slow('........................... {} died fighting.\n'.format(companion.name))
if companion.hp > 0:
print('{} HP: {}'.format(companion.name, companion.hp))
if enemy_hp <= 0:
return initial_enemy_hp
index += 1
return total_damage
def do_action(self, action, **kwargs):
action_method = getattr(self, action.method.__name__)
if action_method:
action_method(**kwargs)