Part 1 of the tutorial.
This commit is contained in:
13
actions.py
Normal file
13
actions.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
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
|
||||||
71
engine.py
71
engine.py
@@ -1,8 +1,9 @@
|
|||||||
import tcod as tc
|
import tcod
|
||||||
from entity import Entity
|
from entity import Entity
|
||||||
from render_functions import *
|
from render_functions import *
|
||||||
from game_map import *
|
from game_map import *
|
||||||
from input_handlers import handle_keys
|
from input_handlers import EventHandler
|
||||||
|
from actions import EscapeAction, MovementAction
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -17,48 +18,48 @@ def main():
|
|||||||
max_rooms = 30
|
max_rooms = 30
|
||||||
|
|
||||||
colors = {
|
colors = {
|
||||||
'dark_wall': tc.Color(0, 0, 100),
|
'dark_wall': tcod.Color(0, 0, 100),
|
||||||
'dark_ground': tc.Color(50, 50, 150)
|
'dark_ground': tcod.Color(50, 50, 150)
|
||||||
}
|
}
|
||||||
|
|
||||||
player = Entity(int(screen_width/2), int(screen_height/2), '@', tc.white)
|
# player = Entity(int(screen_width/2), int(screen_height/2), '@', tcod.white)
|
||||||
npc = Entity(int(screen_width/2), int(screen_height/2), '@', tc.yellow)
|
# npc = Entity(int(screen_width/2), int(screen_height/2), '@', tcod.yellow)
|
||||||
entities = [npc, player]
|
# entities = [npc, player]
|
||||||
|
player_x = int(screen_width/2)
|
||||||
|
player_y = int(screen_width/2)
|
||||||
|
|
||||||
tc.console_set_custom_font('arial10x10.png', tc.FONT_TYPE_GREYSCALE | tc.FONT_LAYOUT_TCOD)
|
tileset = tcod.tileset.load_tilesheet(
|
||||||
|
"arial10x10.png", 32,8, tcod.tileset.CHARMAP_TCOD
|
||||||
|
)
|
||||||
|
|
||||||
tc.console_init_root(screen_width, screen_height, 'tutorial revised', False)
|
event_handler = EventHandler()
|
||||||
con = tc.console_new(screen_width, screen_height)
|
|
||||||
|
|
||||||
game_map = GameMap(map_width, map_height)
|
with tcod.context.new_terminal(
|
||||||
game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player)
|
screen_width,
|
||||||
|
screen_height,
|
||||||
|
tileset=tileset,
|
||||||
|
title='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="@")
|
||||||
|
|
||||||
key = tc.Key()
|
context.present(root_console)
|
||||||
mouse = tc.Mouse()
|
|
||||||
|
|
||||||
while not tc.console_is_window_closed():
|
root_console.clear()
|
||||||
tc.sys_check_for_event(tc.EVENT_KEY_PRESS, key, mouse)
|
|
||||||
render_all(con, entities, game_map, screen_width, screen_height, colors)
|
for event in tcod.event.wait():
|
||||||
tc.console_flush()
|
action = event_handler.dispatch(event)
|
||||||
|
|
||||||
clear_all(con, entities)
|
if action is None:
|
||||||
|
continue
|
||||||
|
|
||||||
action = handle_keys(key)
|
if isinstance(action, MovementAction):
|
||||||
|
player_x += action.dx
|
||||||
move = action.get('move')
|
player_y += action.dy
|
||||||
exit = action.get('exit')
|
elif isinstance(action, EscapeAction):
|
||||||
fullscreen = action.get('fullscreen')
|
raise SystemExit()
|
||||||
|
|
||||||
if move:
|
|
||||||
dx, dy = move
|
|
||||||
if not game_map.is_blocked(player.x + dx, player.y + dy):
|
|
||||||
player.move(dx, dy)
|
|
||||||
|
|
||||||
if exit:
|
|
||||||
return True
|
|
||||||
|
|
||||||
if fullscreen:
|
|
||||||
tc.console_set_fullscreen(not tc.console_is_fullscreen())
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@@ -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):
|
def ev_keydown(self, event: "tcod.event.KeyDown") -> Optional[Action]:
|
||||||
if key.vk == tc.KEY_UP:
|
action: Optional[Action] = None
|
||||||
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)}
|
|
||||||
|
|
||||||
if key.vk == tc.KEY_ENTER and key.lalt:
|
key = event.sym
|
||||||
return {'fullscreen':True}
|
|
||||||
elif key.vk == tc.KEY_ESCAPE:
|
|
||||||
return {'exit': True}
|
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user