-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
89 lines (69 loc) · 2.5 KB
/
player.py
File metadata and controls
89 lines (69 loc) · 2.5 KB
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
# E Y E B L I N K
# ========================================== PLAYER ===============================================
import items
import world
class Player:
def __init__(self):
self.inventory = [items.GrapheneBag(),
items.Videophone(),
items.ShockStick(),
items.RecoveryPill()]
self.x = 1 # Player starts at this tile
self.y = 2
self.hp = 100
def move(self, dx, dy):
self.x += dx
self.y += dy
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 print_inventory(self):
print()
print("Inventory:")
for item in self.inventory:
print('- ' + str(item))
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 attack(self):
best_weapon = self.most_powerful_weapon()
room = world.tile_at(self.x, self.y)
enemy = room.enemy
print("You use the ShockStick against the {}!".format(enemy.name))
enemy.hp -= best_weapon.damage
if not enemy.is_alive():
print("You destroyed the {}!".format(enemy.name))
else:
print("{} energy is now {}".format(enemy.name, enemy.hp))
def heal(self):
consumables = [item for item in self.inventory if isinstance(item, items.Consumable)]
if not consumables:
print("You don't have any item to heal you!")
return
for i, item in enumerate(consumables,1):
print("Choose an item to use to heal: ")
print("{}. {}".format(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)
self.inventory.remove(to_eat)
print("Current HP: {}".format(self.hp))
valid = True
except (ValueError, IndexError):
print("Invalid choice, try again.")