-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
59 lines (46 loc) · 1.65 KB
/
player.py
File metadata and controls
59 lines (46 loc) · 1.65 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
import random
import items, world
class Player():
def __init__(self):
self.inventory = [items.Gold(15), items.Rock()]
self.hp = 100
self.location_x, self.location_y = world.starting_position
self.victory = False
def is_alive(self):
return self.hp > 0
def print_inventory(self):
for item in self.inventory:
print(item,'/n')
def do_action(self,action, **kwargs):
action_method = getattr(self,action.method.__name__)
if action_method:
action_method(**kwargs)
def flee(self, tile):
"""move player randomly to adjacent tile"""
available_moves=tile.adjacent_moves()
r = random.randint(0, len(available_moves)- 1)
self.do_action(available_moves[r])
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())
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):
best_weapon = None
max_dmg = 0
for i in self.inventory:
if isinstance(i, items.Weapon) and i.damage > i.damage:
best_weapon = i
print ("you use {} against {}".format(best_weapon.name, enemy.name))
enemy.hp -= best_weapon.damage
if not enemy.is_alive():
print("You killed {}!".format(enemy.name,enemy.hp))
else:
print("{} HP is {}".format(enemy.name, enemy.hp))