def input(self, engine):
'''This method is called by `engine.play_game`.
It receives a character from the user and interprets it.
Invokes `self.travel` for the steering of the cursor.
It doesn't do any output except for printing the field
initialization message, and forcing the entire screen to be
redrawn on unrecognised input (to de-fuck-up the screen).
'''
if self.gametype == 'hex':
direction_keys = self.direction_keys['hex']
else:
direction_keys = self.direction_keys['square']
look_for = ['reveal', 'flag', 'toggle-attention'] + direction_keys
# Receive input from player.
ch = self.window.getch()
# Interpret.
command = None
if ch == curses.KEY_MOUSE:
_, x, y, _, buttons = curses.getmouse()
valid = True
if self.gametype == 'hex':
valid = self.mouse_travel_hex(x, y, engine.field)
else:
valid = self.mouse_travel_square(x, y, engine.field)
if valid:
for tmp_command in ('flag', 'reveal'):
for mask in self.cfg['curses-mouse-input'][tmp_command]:
if buttons & mask:
command = tmp_command
else:
continue
break
else:
# Keyboard input:
for key in look_for:
if ch in self.cfg['curses-input'][key]:
command = key
# Act.
if command == 'flag':
engine.flag(self.cursor)
elif command == 'reveal':
pre_game = engine.game_status == 'pre-game'
if pre_game:
self.message('Initializing field... This may take a while.')
curses.reset_shell_mode() # BUG: see comments above __init__
engine.reveal(self.cursor)
if pre_game:
curses.reset_prog_mode() # BUG: see comments above __init__
# Clear junk that gets on the screen from impatient players.
self.window.redrawwin()
elif command in direction_keys:
self.travel(engine.field, command)
elif command == 'toggle-attention':
self.attention_mode = not self.attention_mode
elif ch != curses.KEY_MOUSE:
# Don't do this all the time, that'd be a little wasteful.
self.window.redrawwin()
评论列表
文章目录