1
0
python-roguelike/input_handlers.py

31 lines
858 B
Python
Raw Normal View History

2022-01-06 11:09:43 -05:00
from typing import Optional
import tcod.event
2022-01-07 15:52:53 -05:00
from actions import Action, EscapeAction, BumpAction
2022-01-06 11:09:43 -05:00
class EventHandler(tcod.event.EventDispatch[Action]):
def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]:
raise SystemExit()
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]:
action: Optional[Action] = None
key = event.sym
if key == tcod.event.K_UP:
2022-01-07 15:52:53 -05:00
action = BumpAction(dx=0, dy=-1)
2022-01-06 11:09:43 -05:00
elif key == tcod.event.K_DOWN:
2022-01-07 15:52:53 -05:00
action = BumpAction(dx=0, dy=1)
2022-01-06 11:09:43 -05:00
elif key == tcod.event.K_LEFT:
2022-01-07 15:52:53 -05:00
action = BumpAction(dx=-1, dy=0)
2022-01-06 11:09:43 -05:00
elif key == tcod.event.K_RIGHT:
2022-01-07 15:52:53 -05:00
action = BumpAction(dx=1, dy=0)
2022-01-06 11:09:43 -05:00
elif key == tcod.event.K_ESCAPE:
action = EscapeAction()
# No valid key was pressed
return action