1
0

Add the basic Entity class

This commit is contained in:
Timothy Warren 2022-01-06 11:19:56 -05:00
parent d88ab46453
commit 26a3c13b06
2 changed files with 24 additions and 6 deletions

17
entity.py Normal file
View File

@ -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

13
main.py
View File

@ -2,6 +2,7 @@
import tcod import tcod
from actions import EscapeAction, MovementAction from actions import EscapeAction, MovementAction
from entity import Entity
from input_handlers import EventHandler from input_handlers import EventHandler
@ -9,9 +10,6 @@ def main() -> None:
screen_width = 80 screen_width = 80
screen_height = 50 screen_height = 50
player_x = int(screen_width / 2)
player_y = int(screen_height / 2)
tileset = tcod.tileset.load_tilesheet( tileset = tcod.tileset.load_tilesheet(
"dejavu10x10_gs_tc.png", "dejavu10x10_gs_tc.png",
32, 32,
@ -21,6 +19,10 @@ def main() -> None:
event_handler = EventHandler() 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( with tcod.context.new_terminal(
screen_width, screen_width,
screen_height, screen_height,
@ -30,7 +32,7 @@ def main() -> None:
) as context: ) as context:
root_console = tcod.Console(screen_width, screen_height, order="F") root_console = tcod.Console(screen_width, screen_height, order="F")
while True: 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 # Actually display on the screen
context.present(root_console) context.present(root_console)
@ -43,8 +45,7 @@ def main() -> None:
continue continue
if isinstance(action, MovementAction): if isinstance(action, MovementAction):
player_x += action.dx player.move(dx=action.dx, dy=action.dy)
player_y += action.dy
elif isinstance(action, EscapeAction): elif isinstance(action, EscapeAction):
raise SystemExit() raise SystemExit()