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 render_functions import *
|
||||
from game_map import *
|
||||
from input_handlers import handle_keys
|
||||
from input_handlers import EventHandler
|
||||
from actions import EscapeAction, MovementAction
|
||||
|
||||
|
||||
def main():
|
||||
@@ -17,48 +18,48 @@ def main():
|
||||
max_rooms = 30
|
||||
|
||||
colors = {
|
||||
'dark_wall': tc.Color(0, 0, 100),
|
||||
'dark_ground': tc.Color(50, 50, 150)
|
||||
'dark_wall': tcod.Color(0, 0, 100),
|
||||
'dark_ground': tcod.Color(50, 50, 150)
|
||||
}
|
||||
|
||||
player = Entity(int(screen_width/2), int(screen_height/2), '@', tc.white)
|
||||
npc = Entity(int(screen_width/2), int(screen_height/2), '@', tc.yellow)
|
||||
entities = [npc, player]
|
||||
# player = Entity(int(screen_width/2), int(screen_height/2), '@', tcod.white)
|
||||
# npc = Entity(int(screen_width/2), int(screen_height/2), '@', tcod.yellow)
|
||||
# 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)
|
||||
con = tc.console_new(screen_width, screen_height)
|
||||
event_handler = EventHandler()
|
||||
|
||||
game_map = GameMap(map_width, map_height)
|
||||
game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player)
|
||||
with tcod.context.new_terminal(
|
||||
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()
|
||||
mouse = tc.Mouse()
|
||||
context.present(root_console)
|
||||
|
||||
while not tc.console_is_window_closed():
|
||||
tc.sys_check_for_event(tc.EVENT_KEY_PRESS, key, mouse)
|
||||
render_all(con, entities, game_map, screen_width, screen_height, colors)
|
||||
tc.console_flush()
|
||||
root_console.clear()
|
||||
|
||||
for event in tcod.event.wait():
|
||||
action = event_handler.dispatch(event)
|
||||
|
||||
clear_all(con, entities)
|
||||
if action is None:
|
||||
continue
|
||||
|
||||
action = handle_keys(key)
|
||||
|
||||
move = action.get('move')
|
||||
exit = action.get('exit')
|
||||
fullscreen = action.get('fullscreen')
|
||||
|
||||
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 isinstance(action, MovementAction):
|
||||
player_x += action.dx
|
||||
player_y += action.dy
|
||||
elif isinstance(action, EscapeAction):
|
||||
raise SystemExit()
|
||||
|
||||
|
||||
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):
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user