From 26a3c13b06b23a2be38c5001a54c3e67ed8454f8 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 6 Jan 2022 11:19:56 -0500 Subject: [PATCH] Add the basic Entity class --- entity.py | 17 +++++++++++++++++ main.py | 13 +++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 entity.py diff --git a/entity.py b/entity.py new file mode 100644 index 0000000..5404984 --- /dev/null +++ b/entity.py @@ -0,0 +1,17 @@ +from typing import Tuple + + +class Entity: + """ + A generic object to represent players, enemies, items, etc. + """ + def __init__(self, x: int, y: int, char: str, color: Tuple[int, int, int]): + self.x = x + self.y = y + self.char = char + self.color = color + + def move(self, dx: int, dy: int): + # Move the entity by a given amount + self.x += dx + self.y += dy diff --git a/main.py b/main.py index ef0189e..766397e 100755 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ import tcod from actions import EscapeAction, MovementAction +from entity import Entity from input_handlers import EventHandler @@ -9,9 +10,6 @@ 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, @@ -21,6 +19,10 @@ def main() -> None: event_handler = EventHandler() + player = Entity(int(screen_width / 2), int(screen_height/ 2), "@", (255, 255, 255)) + npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), "@", (255, 255, 0)) + entities = {npc, player} + with tcod.context.new_terminal( screen_width, screen_height, @@ -30,7 +32,7 @@ def main() -> None: ) as context: root_console = tcod.Console(screen_width, screen_height, order="F") while True: - root_console.print(x=player_x, y=player_y, string="@") + root_console.print(x=player.x, y=player.y, string=player.char, fg=player.color) # Actually display on the screen context.present(root_console) @@ -43,8 +45,7 @@ def main() -> None: continue if isinstance(action, MovementAction): - player_x += action.dx - player_y += action.dy + player.move(dx=action.dx, dy=action.dy) elif isinstance(action, EscapeAction): raise SystemExit()