diff --git a/actions.py b/actions.py new file mode 100644 index 0000000..8fbce9d --- /dev/null +++ b/actions.py @@ -0,0 +1,14 @@ +class Action: + pass + + +class EscapeAction(Action): + pass + + +class MovementAction(Action): + def __init__(self, dx: int, dy: int): + super().__init__() + + self.dx = dx + self.dy = dy diff --git a/dejavu10x10_gs_tc.png b/dejavu10x10_gs_tc.png new file mode 100644 index 0000000..ea3adbe Binary files /dev/null and b/dejavu10x10_gs_tc.png differ diff --git a/input_handlers.py b/input_handlers.py new file mode 100644 index 0000000..7fe6682 --- /dev/null +++ b/input_handlers.py @@ -0,0 +1,30 @@ +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 ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]: + action: Optional[Action] = None + + key = event.sym + + 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() + + # No valid key was pressed + return action diff --git a/main.py b/main.py new file mode 100755 index 0000000..ef0189e --- /dev/null +++ b/main.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +import tcod + +from actions import EscapeAction, MovementAction +from input_handlers import EventHandler + + +def main() -> None: + screen_width = 80 + screen_height = 50 + + player_x = int(screen_width / 2) + player_y = int(screen_height / 2) + + tileset = tcod.tileset.load_tilesheet( + "dejavu10x10_gs_tc.png", + 32, + 8, + tcod.tileset.CHARMAP_TCOD + ) + + event_handler = EventHandler() + + with tcod.context.new_terminal( + screen_width, + screen_height, + tileset=tileset, + title="Yet Another Roguelike Tutorial", + vsync=True, + ) as context: + root_console = tcod.Console(screen_width, screen_height, order="F") + while True: + root_console.print(x=player_x, y=player_y, string="@") + + # Actually display on the screen + context.present(root_console) + root_console.clear() + + for event in tcod.event.wait(): + action = event_handler.dispatch(event) + + if action is None: + continue + + if isinstance(action, MovementAction): + player_x += action.dx + player_y += action.dy + elif isinstance(action, EscapeAction): + raise SystemExit() + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..dd23368 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +tcod>=11.13 +numpy>=1.18 \ No newline at end of file