Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ***tile_types.py***
- from typing import Tuple
- import numpy as np # type: ignore
- # Tile graphics are structured type compatible with Consoles.tiles_rgb
- graphic_dt = np.dtype(
- [
- ("ch", np.int32), # Unicode codepoint
- ("fg", "3B"), # 3 unsigned bytes, for RGB colors
- ("bg", "3B")
- ]
- )
- # Tile struct used for statically defined tile data.
- tile_dt = np.dtype(
- [
- ("walkable", np.bool), # True if this tile can be walked over
- ("transparent", np.bool), # True if this tile doesn't block FOV.
- ("dark", graphic_dt), # Graphics for when this tile is not in FOV.
- ]
- )
- def new_tile(
- *, # Enforce the use of keywords, so that parameter order doesn't matter
- walkable: int,
- transparent: int,
- dark: Tuple[int, Tuple[int, int, int], Tuple[int, int, int]],
- ) -> np.ndarray:
- """Helper function for defining individual tile types """
- return np.array((walkable, transparent, dark), dtype=tile_dt)
- floor = new_tile(
- walkable=True, transparent=True, dark=(ord(" "), (255, 255, 255), (50, 50, 150))
- )
- wall = new_tile(
- walkable=False, transparent=False, dark=(ord(" "), (255, 255, 255), (0, 0, 100)),
- )
- ***engine.py***
- 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 game_map import GameMap
- from input_handlers import EventHandler
- class Engine:
- def __init__(self, entities: Set[Entity], event_handler: EventHandler, game_map: GameMap, player: Entity):
- self.entities = entities
- self.event_handler = event_handler
- self.game_map = game_map
- self.player = player
- def handle_events(self, events: Iterable[Any]) -> None:
- for event in events:
- action = self.event_handler.dispatch(event) # Essentially (event_handler.ev_quit or event_handler.ev_keydown)
- if action is None: # Remember that action is either None or an action subclass.
- continue
- action.perform(self, self.player)
- def render(self, console: Console, context: Context) -> None:
- self.game_map.render(console)
- for entity in self.entities:
- console.print(entity.x, entity.y, entity.char, fg=entity.color)
- context.present(console)
- console.clear()
- ***game_map.py***
- import numpy as np
- from tcod.console import Console
- import tile_types
- class GameMap:
- def __init__(self, width: int, height: int):
- self.width, self.height = width, height
- self.tiles = np.full((width, height), fill_value=tile_types.wall, order = "F")
- def in_bounds(self, x: int, y: int) -> bool:
- """Return True if x and y are inside of the bounds of this map."""
- return 0 <= x < self.width and 0 <= y < self.height
- def render(self, console: Console) -> None:
- console.tiles_rgb[0:self.width, 0:self.height] = self.tiles["dark"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement