forked from drsudhanshupanda/Software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
92 lines (76 loc) · 2.93 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 game
import world
import enemies
class Player(object):
def __init__(self):
self.backpack = [game.Pencil(),
game.Ruler(),
game.Coffee()]
self.x = 2
self.y = 3
self.lifepoints =100
self.Money = 100
self.victory = False
def is_alive(self):
return self.lifepoints > 0
def move(self, dx, dy):
self.x += dx
self.y += dy
def move_forward(self):
self.move(dx=0,dy=-1)
def move_backward(self):
self.move(dx=0,dy=1)
def move_right(self):
self.move(dx=1,dy=0)
def move_left(self):
self.move(dx=-1,dy=0)
def print_pack(self):
print ("***Backpack Contents***")
best_weapon = self.most_powerful_weapon()
for item in self.backpack:
print(item)
print ("\nYour best weapon is your {}\n" .format(best_weapon.name))
print ("Money: {}".format(self.Money))
def most_powerful_weapon(self):
max_damage = 0
best_weapon = None
for item in self.backpack:
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("\nYou use the {} against {}!".format(best_weapon.name, enemy.name))
enemy.lifepoints -= best_weapon.damage
if not enemy.is_alive():
print("\nYou Killed the {}!" .format(enemy.name))
else:
print ("\n{}'s Life Points are {}." .format(enemy.name, enemy.lifepoints))
def heal(self):
consumables = [item for item in self.backpack if isinstance( item,game.Consumable)]
if not consumables:
print ("\nYou don't have any items to heal you!")
return
for i, item in enumerate(consumables,1):
print("Choose an item to restore Life Points: ")
print("{}. {}".format(i,item))
valid = False
while not valid:
choice = raw_input("")
try:
to_eat = consumables[int(choice) -1]
self.lifepoints = min(100, self.lifepoints + to_eat.healing_value)
self.backpack.remove(to_eat)
print("Current Life Points {}".format(self.lifepoints))
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)