Move around a player character
This commit is contained in:
parent
1ef21900c7
commit
8b10b4228e
14
actions.py
Normal file
14
actions.py
Normal file
@ -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
|
BIN
dejavu10x10_gs_tc.png
Normal file
BIN
dejavu10x10_gs_tc.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.2 KiB |
30
input_handlers.py
Normal file
30
input_handlers.py
Normal file
@ -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
|
53
main.py
Executable file
53
main.py
Executable file
@ -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()
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
tcod>=11.13
|
||||
numpy>=1.18
|
Loading…
Reference in New Issue
Block a user