-
Notifications
You must be signed in to change notification settings - Fork 0
/
theplayer.py
166 lines (112 loc) · 4.37 KB
/
theplayer.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
import items
class player():
def __init__(self):
self.inventory = [items.gold(15), items.dagger()]
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 move(self, dx, dy):
self.location_x += dx
self.location_y += dy
print(world.title_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_west(self):
self.move(dx=-1, dy=0)
def move_east(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):
if i.damage > max_dmg:
max_dmg = 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)")
else:
print("{} hp is {}.format(enemy.name, enemy.hp)")
import player
class action():
def __init__(self, method, name, hotkey, **kwargs):
self.method = method
self.hotkey = hotkey
self.name = name
self.kwargs = kwargs
def __str__(self):
return "{}: {}".format(self.hotkey, self.name)
class movenorth(action):
def __init__(self):
super().__init__(method=player.move_north, name='move north', hotkey='w')
class movesouth(action):
def __init__(self):
super().__init__(method=player.move_south, name='move south', hotkey='s')
class moveeast(action):
def __init__(self):
super().__init__(method=player.move_east, name='move east', hotkey='d')
class movewest(action):
def __init__(self):
super().__init__(method=player.move_west, name='move west', hotkey='a')
class viewinventory(action):
"""prints the player's inventory"""
def __init__(self):
super().__init__(method=player.print_inventory, name='view inventory', hotkey='i')
class attack(action):
def __init__(self, enemy):
super().__init__(method=player.attack, name='attack', hotkey='l', enemy=enemy)
import items, enemies, actions, world
def adjacent_moves(self):
"""returns all move action for adjacent titles."""
moves = []
if world.title_exists(self.x + 1, self.y):
moves.append(actions.moveeast())
if world.title_exists(self.x - 1, self.y):
moves.append(actions.movewest())
if world.title_exists(self.x, self.y - 1):
moves.append(actions.movenorth())
if world.title_exists(self.x, self.y + 1):
move.append(actions.movesouth())
def available_actions(self):
"""returns all of the available actions in this room."""
moves = self.adjacent_moves()
moves.append(actions.viewinventory())
return moves
def do_action(self, action, **kwargs):
action_method = gettr(self, action.method.__name__)
if action_method:
action_method(**kwargs)
import random #note the new import!
import items, world
class player:
#existing code omitted for brevity
def flee(self, title):
"""moves the player randomly to an adjscent title"""
available_moves = title.adjacent_moves()
r = random.randint(0, len(available_moves) - 1)
self.do_action(available_moves[r])
class flee(action):
def __init__(self, title):
super().__init__(method=player.flee, name="flee", hotkey='f', title=title)
class enemyroom(maptitle):
def __init__(self, x, y, enemy):
self.enemy = enemy
super().__init__(x, y)
def modify_player(self, the_player):
if self.enemy.is_alive():
the_player.hp = the_player.hp - self.enemy.damage
print("enemy does {} damage. you have {} hp remaining.".format(self.enemy.damage, the_player.hp))
def available_actions(self):
if self.enemy.is_alive():
return [actions.flee(title=self), actions.attack(enemy=self.enemy)]
else:
return self.adjacent_moves()