Part 1 of the tutorial.

This commit is contained in:
2021-05-26 13:06:40 -04:00
parent ab4031075f
commit ca0f09cc07
3 changed files with 71 additions and 50 deletions

View File

@@ -1,19 +1,26 @@
import tcod as tc
from typing import Optional
import tcod.event
from actions import Action, EscapeAction, MovementAction
class EventHandler(tcod.event.EventDispatch[Action]):
def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]:
raise SystemExit()
def handle_keys(key):
if key.vk == tc.KEY_UP:
return {'move': (0,-1)}
elif key.vk == tc.KEY_DOWN:
return {'move': (0,1)}
elif key.vk == tc.KEY_LEFT:
return {'move':(-1,0)}
elif key.vk == tc.KEY_RIGHT:
return {'move':(1,0)}
def ev_keydown(self, event: "tcod.event.KeyDown") -> Optional[Action]:
action: Optional[Action] = None
if key.vk == tc.KEY_ENTER and key.lalt:
return {'fullscreen':True}
elif key.vk == tc.KEY_ESCAPE:
return {'exit': True}
key = event.sym
return {}
if key == tcod.event.K_UP:
action = MovementAction(dx=0, dy=-1)
elif key == tcod.event.K_DOWN:
action = MovementAction(dx=0, dy=1)
elif key == tcod.event.K_LEFT:
action = MovementAction(dx=-1, dy=0)
elif key == tcod.event.K_RIGHT:
action = MovementAction(dx=1, dy=0)
elif key == tcod.event.K_ESCAPE:
action = EscapeAction()
return action