/
player.py
350 lines (287 loc) · 11.5 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""
This is module for player class
"""
from __future__ import annotations
import random
# ← ↑ → ↓
class Character:
"""
Base class for characters - This should be inherited by both player and mobs alike
"""
def __init__(self, y, x, world_map, game_instance):
self.game_instance = game_instance
self.y = y
self.x = x
self.glyph = "c"
self.glyph_inverted = False
self.glyph_color = "White"
self.world_map = world_map
self.look_at_y = self.y + 1
self.look_at_x = self.x
self.is_mob = True
self.EXP_value = 10
# character sheet:
self.name = "Character"
self.short_name = self.name[0:5]
self.health = 10
self.current_health = 10
self.melee_skill = 20
self.action_points = 4
self.strengh = 30
self.endurance = 20
self.hit_points = round(self.strengh / 10)
self.defence_points = round(self.endurance / 10)
self.deck = []
# clothes:
self.arm_head = None
self.arm_torso = None
self.arm_hands = None
self.arm_legs = None
# weapon
self.weapon = None
# inventory
self.inv_1 = None
self.inv_2 = None
self.inv_3 = None
self.inv_4 = None
def on_create(self):
self.current_health = self.health
self.hit_points = round(self.strengh / 10)
self.defence_points = round(self.endurance / 10)
def remove_self(self):
self.vacate_position()
self.world_map = None
self.game_instance = None
def update(self, *args, **kwargs):
pass
def update_stats(self):
self.hit_points = round(self.strengh / 10)
self.defence_points = round(self.endurance / 10)
def escape_player(self, directions):
way_to_go = {"left": [0, -1, "←"], "right": [0, 1, "→"], "up": [-1, 0, "↑"], "down": [1, 0, "↓"],
"stop": [0, 0, "↑"]}
distances = []
direction_values = {}
for direction in directions:
distance = self.world_map.check_distance_to_player(self.y + way_to_go[direction][0],
self.x + way_to_go[direction][1])
if self.world_map.check_content(self.y + way_to_go[direction][0],
self.x + way_to_go[direction][1]) == "free":
distances.append(distance)
direction_values[distance] = direction
random.shuffle(distances)
if len(distances) == 0:
return "stop"
else:
return direction_values[max(distances)]
def chase_player(self, directions):
way_to_go = {"left": [0, -1, "←"], "right": [0, 1, "→"], "up": [-1, 0, "↑"], "down": [1, 0, "↓"],
"stop": [0, 0, "↑"]}
distances = []
direction_values = {}
for direction in directions:
distance = self.world_map.check_distance_to_player(self.y + way_to_go[direction][0],
self.x + way_to_go[direction][1])
if self.world_map.check_content(self.y + way_to_go[direction][0],
self.x + way_to_go[direction][1]) == "free":
distances.append(distance)
direction_values[distance] = direction
random.shuffle(distances)
return direction_values[min(distances)]
def move(self, direction):
"""
method to move player on grid
"""
if direction == "stop":
pass
else:
way_to_go = {"left": [0, -1, "←"], "right": [0, 1, "→"], "up": [-1, 0, "↑"], "down": [1, 0, "↓"]}
if self.world_map.check_content(self.y + way_to_go[direction][0],
self.x + way_to_go[direction][1]) == "free":
self.vacate_position()
self.y = self.y + way_to_go[direction][0]
self.x = self.x + way_to_go[direction][1]
self.update_position()
# self.glyph = way_to_go[direction][2] - can't change mob glyph to players
self.look_at_y = self.y + way_to_go[direction][0]
self.look_at_x = self.x + way_to_go[direction][1]
def vacate_position(self):
self.world_map.grid[self.y][self.x].occupation = "free"
def update_position(self):
self.world_map.grid[self.y][self.x].occupation = self
def __str__(self):
return self.name
class Player(Character):
"""
Player class
"""
def __init__(self, y, x, character_sheet, world_map, game_instance):
super().__init__(y, x, world_map, game_instance)
self.y = y
self.x = x
self.glyph = "↑"
self.glyph_color = "Red"
self.glyph_inverted = True
self.world_map = world_map
self.look_at_y = self.y + 1
self.look_at_x = self.x
self.is_mob = False
# character sheet:
self.name = character_sheet["name"]
self.short_name = self.name[0:6]
self.health = character_sheet["health"]
self.current_health = self.health
self.melee_skill = character_sheet["melee"]
self.action_points = character_sheet["action_points"]
self.strengh = character_sheet["str"]
self.endurance = character_sheet["end"]
self.hit_points = round(self.strengh / 10)
self.defence_points = round(self.endurance / 10)
self.exp = character_sheet["exp"]
self.level = character_sheet["level"]
self.next_level = character_sheet["next_level"]
# clothes:
self.arm_head = character_sheet["arm_head"]
self.arm_torso = character_sheet["arm_torso"]
self.arm_hands = character_sheet["arm_hands"]
self.arm_legs = character_sheet["arm_legs"]
# weapon:
self.weapon = character_sheet["weapon"]
# inventory:
self.inv_1 = character_sheet["inv_1"]
self.inv_2 = character_sheet["inv_2"]
self.inv_3 = character_sheet["inv_3"]
self.inv_4 = character_sheet["inv_4"]
# deck:
self.deck = character_sheet["deck"]
# "save game"
self.current_map = character_sheet["current_map"]
def update(self, key):
"""
This is logic of players character based on players input
"""
if key in ["up", "down", "left", "right"]:
self.move(key)
elif key in ["1", "2", "3", "4", "5"]:
if key == "1" and self.inv_1 is not None:
self.game_instance.ask_Dump_or_Equip(key)
elif key == "2" and self.inv_2 is not None:
self.game_instance.ask_Dump_or_Equip(key)
elif key == "3" and self.inv_3 is not None:
self.game_instance.ask_Dump_or_Equip(key)
elif key == "4" and self.inv_4 is not None:
self.game_instance.ask_Dump_or_Equip(key)
elif key == "5":
self.game_instance.show_deck()
else:
pass
elif key == " ":
if self.world_map.grid[self.y][self.x].pickable != "free":
self.pickup()
else:
self.use()
else:
pass
self.game_instance.check_level_end()
self.game_instance.check_neighbours()
self.hit_points = round(self.strengh / 10)
self.defence_points = round(self.endurance / 10)
def move(self, direction):
"""
method to move player on grid
"""
way_to_go = {"left": [0, -1, "←"], "right": [0, 1, "→"], "up": [-1, 0, "↑"], "down": [1, 0, "↓"]}
if self.world_map.check_content(self.y + way_to_go[direction][0],
self.x + way_to_go[direction][1]) == "free":
self.vacate_position()
self.y = self.y + way_to_go[direction][0]
self.x = self.x + way_to_go[direction][1]
self.update_position()
self.glyph = way_to_go[direction][2]
self.look_at_y = self.y + way_to_go[direction][0]
self.look_at_x = self.x + way_to_go[direction][1]
def vacate_position(self):
self.world_map.grid[self.y][self.x].occupation = "free"
def update_position(self):
self.world_map.player_y = self.y
self.world_map.player_x = self.x
self.world_map.grid[self.y][self.x].occupation = self
def use(self):
if self.world_map.grid[self.look_at_y][self.look_at_x].occupation == "free":
pass
elif self.world_map.grid[self.look_at_y][self.look_at_x].occupation.destructable:
self.world_map.grid[self.look_at_y][self.look_at_x].occupation = "free"
else:
pass
def get_bodypart_state(self, bodypart: str):
if bodypart == "weapon":
return self.weapon
elif bodypart == "arm_head":
return self.arm_head
elif bodypart == "arm_torso":
return self.arm_torso
elif bodypart == "arm_hands":
return self.arm_hands
elif bodypart == "arm_legs":
return self.arm_legs
def set_bodypart_state(self, bodypart, item):
if bodypart == "weapon":
self.weapon = item
elif bodypart == "arm_head":
self.arm_head = item
elif bodypart == "arm_torso":
self.arm_torso = item
elif bodypart == "arm_hands":
self.arm_hands = item
elif bodypart == "arm_legs":
self.arm_legs = item
def get_inventory_state(self, item_slot):
if item_slot == "1":
return self.inv_1
elif item_slot == "2":
return self.inv_2
elif item_slot == "3":
return self.inv_3
elif item_slot == "4":
return self.inv_4
else:
pass
def set_inventory_state(self, item_slot, item):
if item_slot == "1":
self.inv_1 = item
elif item_slot == "2":
self.inv_2 = item
elif item_slot == "3":
self.inv_3 = item
elif item_slot == "4":
self.inv_4 = item
else:
pass
def pickup(self):
object_to_pick = self.world_map.grid[self.y][self.x].pickable
print(object_to_pick)
if object_to_pick.consumable:
if self.current_health + self.current_map >= self.health:
self.current_health = self.health
else:
self.current_health += self.current_map
self.game_instance.grid_map.info_label = "Health potion brought back " + str(
self.current_map) + " of Health"
elif object_to_pick.destination == "deck":
self.deck.append(object_to_pick)
else:
# check if default body part is available:
if self.get_bodypart_state(object_to_pick.destination) is None:
self.set_bodypart_state(object_to_pick.destination, object_to_pick)
elif self.inv_1 is None:
self.inv_1 = object_to_pick
elif self.inv_2 is None:
self.inv_2 = object_to_pick
elif self.inv_3 is None:
self.inv_3 = object_to_pick
elif self.inv_4 is None:
self.inv_4 = object_to_pick
else:
self.game_instance.grid_map.info_label = "No place in inventory. Throw something out"
return "No place in inventory. Throw something out"
self.world_map.grid[self.y][self.x].pickable = "free"