-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_handlers.py
90 lines (81 loc) · 2.39 KB
/
input_handlers.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
import tcod as libtcod
from globals import GameStates, CONFIG
def handle_main_menu(key):
if key == ord('1'):
return {'new_game': True}
if key == ord('2'):
return {'exit': True}
return {}
def handle_keys(key, game_state):
if game_state == GameStates.PLAYERS_TURN:
return handle_player_turn(key)
if game_state == GameStates.PLAYER_DEAD:
return handle_player_dead(key)
if game_state == GameStates.TARGETING:
return handle_targeting_keys(key)
if game_state == GameStates.SHOP:
return handle_shop(key)
if game_state == GameStates.INSTRUCTIONS:
if key is not None:
return {'exit_instructions': True}
return {}
def handle_shop(key):
if key == 27:
return {'exit': True}
index = key - ord('1')
if index >= 0:
return {'shop_index': index}
return {}
def handle_targeting_keys(key):
if key == 27: # ESC
return {'exit': True}
return {}
def handle_mouse_move(mouse):
(x, y) = (mouse.tile[0], mouse.tile[1])
return (x, y)
def handle_mouse(mouse):
(x, y) = (mouse.tile[0], mouse.tile[1])
if mouse.button == libtcod.event.BUTTON_LEFT:
return {'left_click': (x, y)}
if mouse.button == libtcod.event.BUTTON_RIGHT:
return {'right_click': (x, y)}
return {}
def handle_player_dead(key):
#if key == ord('r'):
# return {'replay': True}
if key == 27:
return {'exit': True}
else:
return {}
def handle_player_turn(key):
if key:
print('Key: %i' % key)
if key == ord('w'):
return {'move': (0, -1)}
elif key == ord('s'):
return {'move': (0, 1)}
elif key == ord('a'):
return {'move': (-1, 0)}
elif key == ord('d'):
return {'move': (1, 0)}
elif key == ord('q'):
return {'move': (-1, -1)}
elif key == ord('e'):
return {'move': (1, -1)}
elif key == ord('z'):
return {'move': (-1, 1)}
elif key == ord('c'):
return {'move': (1, 1)}
# elif key == ord('v'):
# return {'debug_take_stairs': True}
elif key == 32: # space
return {'take_stairs': True}
elif key == 13:
return {'take_stairs': True}
elif key == 27:
return {'exit': True}
elif key == 47: # ?
return {'show_help': True}
elif key in (ord('1'), ord('2'), ord('3'), ord('4')):
return {'hotkey': chr(key)}
return {}