-
Notifications
You must be signed in to change notification settings - Fork 7
/
engine.py
350 lines (264 loc) · 13.1 KB
/
engine.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
import tdl
from tcod import image_load
from death_functions import kill_monster, kill_player
from entity import get_blocking_entities_at_location
from game_messages import Message
from game_states import GameStates
from input_handlers import handle_keys, handle_mouse, handle_main_menu
from loader_functions.initialize_new_game import get_constants, get_game_variables
from loader_functions.data_loaders import load_game, save_game
from map_utils import next_floor
from menus import main_menu, message_box
from render_functions import clear_all, render_all
def play_game(player, entities, game_map, message_log, game_state, root_console, con, panel, constants):
tdl.set_font('arial10x10.png', greyscale=True, altLayout=True)
fov_recompute = True
mouse_coordinates = (0, 0)
previous_game_state = game_state
targeting_item = None
while not tdl.event.is_window_closed():
if fov_recompute:
game_map.compute_fov(player.x, player.y, fov=constants['fov_algorithm'], radius=constants['fov_radius'],
light_walls=constants['fov_light_walls'])
render_all(con, panel, entities, player, game_map, fov_recompute, root_console, message_log,
constants['screen_width'], constants['screen_height'], constants['bar_width'],
constants['panel_height'], constants['panel_y'], mouse_coordinates, constants['colors'],
game_state)
tdl.flush()
clear_all(con, entities)
fov_recompute = False
for event in tdl.event.get():
if event.type == 'KEYDOWN':
user_input = event
break
elif event.type == 'MOUSEMOTION':
mouse_coordinates = event.cell
elif event.type == 'MOUSEDOWN':
user_mouse_input = event
break
else:
user_input = None
user_mouse_input = None
if not (user_input or user_mouse_input):
continue
action = handle_keys(user_input, game_state)
mouse_action = handle_mouse(user_mouse_input)
move = action.get('move')
wait = action.get('wait')
pickup = action.get('pickup')
show_inventory = action.get('show_inventory')
drop_inventory = action.get('drop_inventory')
inventory_index = action.get('inventory_index')
take_stairs = action.get('take_stairs')
level_up = action.get('level_up')
show_character_screen = action.get('show_character_screen')
exit = action.get('exit')
fullscreen = action.get('fullscreen')
left_click = mouse_action.get('left_click')
right_click = mouse_action.get('right_click')
player_turn_results = []
if move and game_state == GameStates.PLAYERS_TURN:
dx, dy = move
destination_x = player.x + dx
destination_y = player.y + dy
if game_map.walkable[destination_x, destination_y]:
target = get_blocking_entities_at_location(entities, destination_x, destination_y)
if target:
attack_results = player.fighter.attack(target)
player_turn_results.extend(attack_results)
else:
player.move(dx, dy)
fov_recompute = True
game_state = GameStates.ENEMY_TURN
elif wait:
game_state = GameStates.ENEMY_TURN
elif pickup and game_state == GameStates.PLAYERS_TURN:
for entity in entities:
if entity.item and entity.x == player.x and entity.y == player.y:
pickup_results = player.inventory.add_item(entity, constants['colors'])
player_turn_results.extend(pickup_results)
break
else:
message_log.add_message(Message('There is nothing here to pick up.', constants['colors'].get('yellow')))
if show_inventory:
previous_game_state = game_state
game_state = GameStates.SHOW_INVENTORY
if drop_inventory:
previous_game_state = game_state
game_state = GameStates.DROP_INVENTORY
if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len(
player.inventory.items):
item = player.inventory.items[inventory_index]
if game_state == GameStates.SHOW_INVENTORY:
player_turn_results.extend(player.inventory.use(item, constants['colors'], entities=entities,
game_map=game_map))
elif game_state == GameStates.DROP_INVENTORY:
player_turn_results.extend(player.inventory.drop_item(item, constants['colors']))
if take_stairs and game_state == GameStates.PLAYERS_TURN:
for entity in entities:
if entity.stairs and entity.x == player.x and entity.y == player.y:
game_map, entities = next_floor(player, message_log, entity.stairs.floor, constants)
fov_recompute = True
con.clear()
break
else:
message_log.add_message(Message('There are no stairs here.', constants['colors'].get('yellow')))
if level_up:
if level_up == 'hp':
player.fighter.base_max_hp += 20
player.fighter.hp += 20
elif level_up == 'str':
player.fighter.base_power += 1
elif level_up == 'def':
player.fighter.base_defense += 1
game_state = previous_game_state
if show_character_screen:
previous_game_state = game_state
game_state = GameStates.CHARACTER_SCREEN
if game_state == GameStates.TARGETING:
if left_click:
target_x, target_y = left_click
item_use_results = player.inventory.use(targeting_item, constants['colors'], entities=entities,
game_map=game_map, target_x=target_x, target_y=target_y)
player_turn_results.extend(item_use_results)
elif right_click:
player_turn_results.append({'targeting_cancelled': True})
if exit:
if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.CHARACTER_SCREEN):
game_state = previous_game_state
elif game_state == GameStates.TARGETING:
player_turn_results.append({'targeting_cancelled': True})
else:
save_game(player, entities, game_map, message_log, game_state)
return True
if fullscreen:
tdl.set_fullscreen(not tdl.get_fullscreen())
for player_turn_result in player_turn_results:
message = player_turn_result.get('message')
dead_entity = player_turn_result.get('dead')
item_added = player_turn_result.get('item_added')
item_consumed = player_turn_result.get('consumed')
item_dropped = player_turn_result.get('item_dropped')
equip = player_turn_result.get('equip')
targeting = player_turn_result.get('targeting')
targeting_cancelled = player_turn_result.get('targeting_cancelled')
xp = player_turn_result.get('xp')
if message:
message_log.add_message(message)
if dead_entity:
if dead_entity == player:
message, game_state = kill_player(dead_entity, constants['colors'])
else:
message = kill_monster(dead_entity, constants['colors'])
message_log.add_message(message)
if item_added:
entities.remove(item_added)
game_state = GameStates.ENEMY_TURN
if item_consumed:
game_state = GameStates.ENEMY_TURN
if item_dropped:
entities.append(item_dropped)
game_state = GameStates.ENEMY_TURN
if equip:
equip_results = player.equipment.toggle_equip(equip)
for equip_result in equip_results:
equipped = equip_result.get('equipped')
dequipped = equip_result.get('dequipped')
if equipped:
message_log.add_message(Message('You equipped the {0}'.format(equipped.name)))
if dequipped:
message_log.add_message(Message('You dequipped the {0}'.format(dequipped.name)))
game_state = GameStates.ENEMY_TURN
if targeting:
previous_game_state = GameStates.PLAYERS_TURN
game_state = GameStates.TARGETING
targeting_item = targeting
message_log.add_message(targeting_item.item.targeting_message)
if targeting_cancelled:
game_state = previous_game_state
message_log.add_message(Message('Targeting cancelled'))
if xp:
leveled_up = player.level.add_xp(xp)
message_log.add_message(Message('You gain {0} experience points.'.format(xp)))
if leveled_up:
message_log.add_message(Message(
'Your battle skills grow stronger! You reached level {0}'.format(
player.level.current_level) + '!',
constants['colors'].get('yellow')))
previous_game_state = game_state
game_state = GameStates.LEVEL_UP
if game_state == GameStates.ENEMY_TURN:
for entity in entities:
if entity.ai:
enemy_turn_results = entity.ai.take_turn(player, game_map, entities)
for enemy_turn_result in enemy_turn_results:
message = enemy_turn_result.get('message')
dead_entity = enemy_turn_result.get('dead')
if message:
message_log.add_message(message)
if dead_entity:
if dead_entity == player:
message, game_state = kill_player(dead_entity, constants['colors'])
else:
message = kill_monster(dead_entity, constants['colors'])
message_log.add_message(message)
if game_state == GameStates.PLAYER_DEAD:
break
if game_state == GameStates.PLAYER_DEAD:
break
else:
game_state = GameStates.PLAYERS_TURN
def main():
constants = get_constants()
tdl.set_font('arial10x10.png', greyscale=True, altLayout=True)
root_console = tdl.init(constants['screen_width'], constants['screen_height'], constants['window_title'])
con = tdl.Console(constants['screen_width'], constants['screen_height'])
panel = tdl.Console(constants['screen_width'], constants['panel_height'])
player = None
entities = []
game_map = None
message_log = None
game_state = None
show_main_menu = True
show_load_error_message = False
main_menu_background_image = image_load('menu_background.png')
while not tdl.event.is_window_closed():
for event in tdl.event.get():
if event.type == 'KEYDOWN':
user_input = event
break
else:
user_input = None
if show_main_menu:
main_menu(con, root_console, main_menu_background_image, constants['screen_width'],
constants['screen_height'], constants['colors'])
if show_load_error_message:
message_box(con, root_console, 'No save game to load', 50, constants['screen_width'],
constants['screen_height'])
tdl.flush()
action = handle_main_menu(user_input)
new_game = action.get('new_game')
load_saved_game = action.get('load_game')
exit_game = action.get('exit')
if show_load_error_message and (new_game or load_saved_game or exit_game):
show_load_error_message = False
elif new_game:
player, entities, game_map, message_log, game_state = get_game_variables(constants)
game_state = GameStates.PLAYERS_TURN
show_main_menu = False
elif load_saved_game:
try:
player, entities, game_map, message_log, game_state = load_game()
show_main_menu = False
except FileNotFoundError:
show_load_error_message = True
elif exit_game:
break
else:
root_console.clear()
con.clear()
panel.clear()
play_game(player, entities, game_map, message_log, game_state, root_console, con, panel, constants)
show_main_menu = True
if __name__ == '__main__':
main()