Part 2 in progress.
This commit is contained in:
84
engine.py
84
engine.py
@@ -1,66 +1,34 @@
|
||||
import tcod
|
||||
from entity import Entity
|
||||
from render_functions import *
|
||||
from game_map import *
|
||||
from input_handlers import EventHandler
|
||||
from typing import Set, Iterable, Any
|
||||
|
||||
from tcod.context import Context
|
||||
from tcod.console import Console
|
||||
|
||||
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():
|
||||
screen_width = 80
|
||||
screen_height = 50
|
||||
def handle_events(self, events: Iterable[Any]) -> None:
|
||||
for event in events:
|
||||
action = self.event_handler.dispatch(event)
|
||||
|
||||
map_width = 80
|
||||
map_height = 50
|
||||
if action is None:
|
||||
continue
|
||||
|
||||
room_max_size = 10
|
||||
room_min_size = 6
|
||||
max_rooms = 30
|
||||
if isinstance(action, MovementAction):
|
||||
self.player.move(dx=action.dx, dy=action.dy)
|
||||
elif isinstance(action, EscapeAction):
|
||||
raise SystemExit()
|
||||
|
||||
colors = {
|
||||
'dark_wall': tcod.Color(0, 0, 100),
|
||||
'dark_ground': tcod.Color(50, 50, 150)
|
||||
}
|
||||
def render(self, console: Console, context: Context) -> None:
|
||||
for entity in self.entities:
|
||||
console.print(entity.x, entity.y, entity.char, fg=entity.color)
|
||||
|
||||
# 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)
|
||||
context.present(console)
|
||||
|
||||
tileset = tcod.tileset.load_tilesheet(
|
||||
"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()
|
||||
console.clear()
|
||||
@@ -1,14 +1,16 @@
|
||||
from typing import Tuple
|
||||
|
||||
class Entity:
|
||||
"""
|
||||
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.y = y
|
||||
self.char = char
|
||||
self.color = color
|
||||
|
||||
def move(self, dx, dy):
|
||||
def move(self, dx: int, dy: int) -> None:
|
||||
self.x += dx
|
||||
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