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,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__':