Part 2 in progress.
This commit is contained in:
84
engine.py
84
engine.py
@@ -1,66 +1,34 @@
|
|||||||
import tcod
|
from typing import Set, Iterable, Any
|
||||||
from entity import Entity
|
|
||||||
from render_functions import *
|
from tcod.context import Context
|
||||||
from game_map import *
|
from tcod.console import Console
|
||||||
from input_handlers import EventHandler
|
|
||||||
from actions import EscapeAction, MovementAction
|
from actions import EscapeAction, MovementAction
|
||||||
|
from entity import Entity
|
||||||
|
from input_handlers import EventHandler
|
||||||
|
|
||||||
|
class Engine:
|
||||||
|
def __init__(self, entities: Set[Entity], event_handler: EventHandler, player: Entity):
|
||||||
|
self.entities = entities
|
||||||
|
self.event_handler = event_handler
|
||||||
|
self.player = player
|
||||||
|
|
||||||
def main():
|
def handle_events(self, events: Iterable[Any]) -> None:
|
||||||
screen_width = 80
|
for event in events:
|
||||||
screen_height = 50
|
action = self.event_handler.dispatch(event)
|
||||||
|
|
||||||
map_width = 80
|
if action is None:
|
||||||
map_height = 50
|
continue
|
||||||
|
|
||||||
room_max_size = 10
|
if isinstance(action, MovementAction):
|
||||||
room_min_size = 6
|
self.player.move(dx=action.dx, dy=action.dy)
|
||||||
max_rooms = 30
|
elif isinstance(action, EscapeAction):
|
||||||
|
raise SystemExit()
|
||||||
|
|
||||||
colors = {
|
def render(self, console: Console, context: Context) -> None:
|
||||||
'dark_wall': tcod.Color(0, 0, 100),
|
for entity in self.entities:
|
||||||
'dark_ground': tcod.Color(50, 50, 150)
|
console.print(entity.x, entity.y, entity.char, fg=entity.color)
|
||||||
}
|
|
||||||
|
|
||||||
# player = Entity(int(screen_width/2), int(screen_height/2), '@', tcod.white)
|
context.present(console)
|
||||||
# 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)
|
|
||||||
|
|
||||||
tileset = tcod.tileset.load_tilesheet(
|
console.clear()
|
||||||
"arial10x10.png", 32,8, tcod.tileset.CHARMAP_TCOD
|
|
||||||
)
|
|
||||||
|
|
||||||
event_handler = EventHandler()
|
|
||||||
|
|
||||||
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="@")
|
|
||||||
|
|
||||||
context.present(root_console)
|
|
||||||
|
|
||||||
root_console.clear()
|
|
||||||
|
|
||||||
for event in tcod.event.wait():
|
|
||||||
action = event_handler.dispatch(event)
|
|
||||||
|
|
||||||
if action is None:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if isinstance(action, MovementAction):
|
|
||||||
player_x += action.dx
|
|
||||||
player_y += action.dy
|
|
||||||
elif isinstance(action, EscapeAction):
|
|
||||||
raise SystemExit()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
|
from typing import Tuple
|
||||||
|
|
||||||
class Entity:
|
class Entity:
|
||||||
"""
|
"""
|
||||||
Generic object to represent players, enemies, items, etc.
|
Generic object to represent players, enemies, items, etc.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, x, y, char, color):
|
def __init__(self, x: int, y: int, char: str, color: tuple[int, int, int]):
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.char = char
|
self.char = char
|
||||||
self.color = color
|
self.color = color
|
||||||
|
|
||||||
def move(self, dx, dy):
|
def move(self, dx: int, dy: int) -> None:
|
||||||
self.x += dx
|
self.x += dx
|
||||||
self.y += dy
|
self.y += dy
|
||||||
51
main.py
Normal file
51
main.py
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
import tcod
|
||||||
|
|
||||||
|
from engine import Engine
|
||||||
|
from entity import Entity
|
||||||
|
from input_handlers import EventHandler
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
screen_width = 80
|
||||||
|
screen_height = 50
|
||||||
|
|
||||||
|
room_max_size = 10
|
||||||
|
room_min_size = 6
|
||||||
|
max_rooms = 30
|
||||||
|
|
||||||
|
colors = {
|
||||||
|
'dark_wall': tcod.Color(0, 0, 100),
|
||||||
|
'dark_ground': tcod.Color(50, 50, 150)
|
||||||
|
}
|
||||||
|
|
||||||
|
tileset = tcod.tileset.load_tilesheet(
|
||||||
|
"arial10x10.png", 32,8, tcod.tileset.CHARMAP_TCOD
|
||||||
|
)
|
||||||
|
|
||||||
|
event_handler = EventHandler()
|
||||||
|
|
||||||
|
player = Entity(int(screen_width/2), int(screen_height/2), '@', (255, 255, 255))
|
||||||
|
npc = Entity(int(screen_width/2), int(screen_height/2), '@', (255, 255, 0))
|
||||||
|
entities = {npc, player}
|
||||||
|
|
||||||
|
engine = Engine(entities=entities, event_handler=event_handler, player=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:
|
||||||
|
engine.render(console=root_console, context=context)
|
||||||
|
|
||||||
|
events = tcod.event.wait()
|
||||||
|
|
||||||
|
engine.handle_events(events)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user