From 2e7d121be303f5da463a5b518abfbb9ce3570288 Mon Sep 17 00:00:00 2001 From: Dan Dembinski Date: Sat, 22 Jun 2019 22:44:41 -0400 Subject: [PATCH] Finished Part 2 --- engine.py | 49 +++++++++++++++++++++++++--------------- entity.py | 14 ++++++++++++ input_handlers.py | 15 ++++++------ map_objects.py | 13 +++++++++++ venv/game_map.py | 24 ++++++++++++++++++++ venv/render_functions.py | 33 +++++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 25 deletions(-) create mode 100644 entity.py create mode 100644 map_objects.py create mode 100644 venv/game_map.py create mode 100644 venv/render_functions.py diff --git a/engine.py b/engine.py index b9e0bd5..7743d5a 100644 --- a/engine.py +++ b/engine.py @@ -1,29 +1,41 @@ -import tcod +import tcod as tc +from entity import Entity +from render_functions import * +from game_map import * from input_handlers import handle_keys + def main(): screen_width = 80 screen_height = 50 + map_width = 80 + map_height = 50 - player_x = int(screen_width/2) - player_y = int(screen_height/2) + colors = { + 'dark_wall': tc.Color(0, 0, 100), + 'dark_ground': tc.Color(50, 50, 150) + } - tcod.console_set_custom_font('arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) + 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] - tcod.console_init_root(screen_width, screen_height, 'tutorial revised', False) - con = tcod.console_new(screen_width, screen_height) + tc.console_set_custom_font('arial10x10.png', tc.FONT_TYPE_GREYSCALE | tc.FONT_LAYOUT_TCOD) - key = tcod.Key() - mouse = tcod.Mouse() + tc.console_init_root(screen_width, screen_height, 'tutorial revised', False) + con = tc.console_new(screen_width, screen_height) - while not tcod.console_is_window_closed(): - tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) - tcod.console_set_default_foreground(con, tcod.white) - tcod.console_put_char(con, player_x, player_y, '@', tcod.BKGND_NONE) - tcod.console_blit(con, 0,0, screen_width, screen_height, 0,0,0) - tcod.console_flush() + game_map = GameMap(map_width, map_height) - tcod.console_put_char(con, player_x, player_y, ' ', tcod.BKGND_NONE) + key = tc.Key() + mouse = tc.Mouse() + + 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() + + clear_all(con, entities) action = handle_keys(key) @@ -33,14 +45,15 @@ def main(): if move: dx, dy = move - player_x += dx - player_y += dy + if not game_map.is_blocked(player.x + dx, player.y + dy): + player.move(dx, dy) if exit: return True if fullscreen: - tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) + tc.console_set_fullscreen(not tc.console_is_fullscreen()) + if __name__ == '__main__': main() \ No newline at end of file diff --git a/entity.py b/entity.py new file mode 100644 index 0000000..d9fb036 --- /dev/null +++ b/entity.py @@ -0,0 +1,14 @@ +class Entity: + """ + Generic object to represent players, enemies, items, etc. + """ + + def __init__(self, x, y, char, color): + self.x = x + self.y = y + self.char = char + self.color = color + + def move(self, dx, dy): + self.x += dx + self.y += dy \ No newline at end of file diff --git a/input_handlers.py b/input_handlers.py index adfc786..3f04f60 100644 --- a/input_handlers.py +++ b/input_handlers.py @@ -1,18 +1,19 @@ -import tcod +import tcod as tc + def handle_keys(key): - if key.vk == tcod.KEY_UP: + if key.vk == tc.KEY_UP: return {'move': (0,-1)} - elif key.vk == tcod.KEY_DOWN: + elif key.vk == tc.KEY_DOWN: return {'move': (0,1)} - elif key.vk == tcod.KEY_LEFT: + elif key.vk == tc.KEY_LEFT: return {'move':(-1,0)} - elif key.vk == tcod.KEY_RIGHT: + elif key.vk == tc.KEY_RIGHT: return {'move':(1,0)} - if key.vk == tcod.KEY_ENTER and key.lalt: + if key.vk == tc.KEY_ENTER and key.lalt: return {'fullscreen':True} - elif key.vk == tcod.KEY_ESCAPE: + elif key.vk == tc.KEY_ESCAPE: return {'exit': True} return {} \ No newline at end of file diff --git a/map_objects.py b/map_objects.py new file mode 100644 index 0000000..3e68315 --- /dev/null +++ b/map_objects.py @@ -0,0 +1,13 @@ +class Tile: + """ + A tile on a map. May or may not be blocked, and may or may not block sight + """ + + def __init__(self, blocked, block_sight=None): + self.blocked = blocked + + # By default, if a tile is blocked, it also blocks sight + if block_sight is None: + block_sight = blocked + + self.block_sight = block_sight \ No newline at end of file diff --git a/venv/game_map.py b/venv/game_map.py new file mode 100644 index 0000000..7a679cb --- /dev/null +++ b/venv/game_map.py @@ -0,0 +1,24 @@ +from map_objects import * + +class GameMap: + def __init__(self, width, height): + self.width = width + self.height = height + self.tiles = self.initialize_tiles() + + def initialize_tiles(self): + tiles = [[Tile(False) for y in range(self.height)] for x in range(self.width)] + + tiles[30][22].blocked = True + tiles[30][22].block_sight = True + tiles[31][22].blocked = True + tiles[31][22].block_sight = True + tiles[32][22].blocked = True + tiles[32][22].block_sight = True + + return tiles + + def is_blocked(self, x, y): + if self.tiles[x][y].blocked: + return True + return False \ No newline at end of file diff --git a/venv/render_functions.py b/venv/render_functions.py new file mode 100644 index 0000000..38481a3 --- /dev/null +++ b/venv/render_functions.py @@ -0,0 +1,33 @@ +import tcod as tc + +def render_all(con, entities, game_map, screen_width, screen_height, colors): + # Draw all tiles in the game map + for y in range(game_map.height): + for x in range(game_map.width): + wall = game_map.tiles[x][y].block_sight + + if wall: + tc.console_set_char_background(con, x, y, colors.get('dark_wall'), tc.BKGND_SET) + else: + tc.console_set_char_background(con, x, y, colors.get('dark_ground'), tc.BKGND_SET) + + # Draw all entities in the list + for entity in entities: + draw_entity(con, entity) + + tc. console_blit(con, 0,0, screen_width, screen_height, 0, 0, 0) + + +def clear_all(con, entities): + for entity in entities: + clear_entity(con, entity) + + +def draw_entity(con, entity): + tc.console_set_default_foreground(con, entity.color) + tc.console_put_char(con, entity.x, entity.y, entity.char, tc.BKGND_NONE) + + +def clear_entity(con, entity): + # Erase the charect that represents this object + tc.console_put_char(con, entity.x, entity.y, ' ', tc.BKGND_NONE)