-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
92 lines (78 loc) · 2.82 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
import items
import world
class Player:
def __init__(self):
self.inventory = [items.Rock(),
items.Dagger(),
items.WaterPouch(),
items.CrustyBread()]
self.x = world.start_tile_location[0]
self.y = world.start_tile_location[1]
self.hp = 100
self.gold = 5
self.victory = False
def is_alive(self):
return self.hp > 0
def print_invetory(self):
print("Inventory:")
for item in self.inventory:
print('* ' + str(item))
print(f"Gold: {self.gold}")
def most_powerful_weapon(self):
max_damage = 0
best_weapon = None
for item in self.inventory:
try:
if item.damage > max_damage:
best_weapon = item
max_damage = item.damage
except AttributeError:
pass
return best_weapon
def move(self, dx, dy):
self.x += dx
self.y += dy
def move_up(self):
self.move(0,-1)
def move_down(self):
self.move(0, 1)
def move_left(self):
self.move(-1, 0)
def move_right(self):
self.move(1, 0)
def attack(self):
best_weapon = self.most_powerful_weapon()
room = world.tile_at(self.x, self.y)
enemy = room.enemy
print(f"You use {best_weapon} against {enemy.name}. You dealt {best_weapon.damage} damage!")
enemy.hp -= best_weapon.damage
if not enemy.is_alive():
self.gold += enemy.value
print(f"You killed {enemy.name}! You have earned {enemy.value} gold!")
else:
print(f"{enemy.name} has {enemy.hp} HP remaining.")
def heal(self):
consumables = [item for item in self.inventory
if isinstance(item, items.Consumable)]
if not consumables:
print("You don't have any healing items!")
return
print("Choose an item to use to heal: ")
for i, item in enumerate(consumables, 1):
print(f"{i}. {item}")
valid = False
while not valid:
choice = input("")
try:
to_eat = consumables[int(choice) - 1]
self.hp = min(100, self.hp + to_eat.healing_value)
print(f"{to_eat} has been used! + {to_eat.healing_value}HP.")
self.inventory.remove(to_eat)
print(f"Current HP: {self.hp}")
print("==================================")
valid = True
except (ValueError, IndexError):
print("Invalid choice, try again.")
def trade(self):
room = world.tile_at(self.x, self.y)
room.check_if_trade(self)