def draw_preview(self, context: Context, time: int): state = self.preview_state(time) self.erase() self.tile.draw_tile(context) context.color(self.color) context.layer(1) context.put(state.pos, self.glyph) context.layer(0) context.color(self.window.fg_color)
def perform_draw(self, ctx=None): ctx = ctx or BearLibTerminalContext() if self.is_hidden: return self.draw(ctx) for view in self.subviews: with ctx.translate(view.frame.origin): view.perform_draw(ctx)
def __init__(self): super().__init__() self.pprint_center(["Generating..."]) self.__input_map = { bearlib.TK_Q: self.quit, bearlib.TK_ESCAPE: self.quit } self.context = Context() try: self.map = Map(self) except Exception as err: # This is here for debugging purposes pass self.player = self.map.player self.input = Input(self.player, self.context, self) self.update_skills()
class GameScene(PrintScene): time = Time() def __init__(self): super().__init__() self.pprint_center(["Generating..."]) self.__input_map = { bearlib.TK_Q: self.quit, bearlib.TK_ESCAPE: self.quit } self.context = Context() try: self.map = Map(self) except Exception as err: # This is here for debugging purposes pass self.player = self.map.player self.input = Input(self.player, self.context, self) self.update_skills() @property def entities(self): return self.map.floor.entities @property def relative_pos(self) -> Point: return self.player.relative_position + self.map.view_center @property def bounds(self) -> Rect: return Rect(self.map.origin, self.map.view_size) def quit(self): self.context.clear() self.pprint_center([ "Are you sure you", "want to quit?", "", "Space - Yes ", "Esc - No" ]) self.context.refresh() while True: key = bearlib.read() if key == bearlib.TK_SPACE: self.director.quit() break elif key == bearlib.TK_ESCAPE: break def draw_tiles(self): for cell in self.map.floor.cells: if self.bounds.contains(cell.point + self.relative_pos): if not cell.occupied and self.player.in_sight(cell): cell.draw_tile(self.context) def draw_entities(self): for entity in self.entities: if self.player.visible_to(entity): entity.draw(self.context) def print_stats(self, hp: int = None, tp: int = None, tick: int = None, left_arrow: bool = False, right_arrow: bool = False): color = bearlib.state(bearlib.TK_COLOR) corner = self.map.view_rect.point_bottom_left bearlib.clear_area(corner.x, corner.y + 1, self.window.width, self.window.height - corner.y + 1) if hp is None: hp = self.player.hp if tp is None: tp = self.player.tp hp_string = f'HP: {"|" * hp}{" " * (self.player.max_hp - self.player.hp)} '\ f'({self.player.hp}/{self.player.max_hp})' tp_string = f'MP: {"|" * tp}{" " * (self.player.max_tp - self.player.tp)} '\ f'({self.player.tp}/{self.player.max_tp})' xp_string = f'XP: {self.player.xp} - Level {self.player.level}' time_string = f'{"<" if left_arrow else " "}Time: {self.time.clock(tick)}{">" if right_arrow else ""}' bearlib.color(Color.RED) self.pprint(corner.x, corner.y + 1, hp_string) bearlib.color(Color.VIOLET) self.pprint(corner.x, corner.y + 2, tp_string) bearlib.color(Color.YELLOW) self.pprint(corner.x, corner.y + 3, xp_string) bearlib.color(color) self.pprint(corner.x, corner.y + 4, time_string) bearlib.color(self.window.fg_color) def update_skills(self): skill_strings = [] for skill in self.player.skills: skill_string = f"{skill.NAME} ({skill.COST} MP): {skill.KEY}" skill_desc = f"{skill.DESCRIPTION}" skill_strings.extend([skill_string, skill_desc, ""]) self.gutter = skill_strings def print_gutter(self, strings: Optional[List[str]] = None): if strings is None: strings = self.gutter for i in range(0, len(strings)): string = strings[i] self.pprint(self.gutter_rect.x, self.gutter_rect.y + i, string) def terminal_read(self, val) -> None: if val in self.__input_map: self.__input_map[val]() with self.context.translate(self.relative_pos): if self.input.handle_key(val): self.player.turn() for entity in self.entities: if self.bounds.contains(entity.position + self.relative_pos): if entity.type == EntityType.ENEMY: entity.ai_behavior() self.time.tick() def terminal_update(self, is_active: bool = False) -> None: if self.player.state == ActorState.DEAD: self.director.replace_scene(DeathScene()) elif self.player.state == ActorState.VICTORIOUS: self.director.replace_scene(VictoryScene()) bearlib.clear() with self.context.translate(self.relative_pos): self.draw_tiles() for entity in self.entities: if isinstance(entity, Actor): if entity.state == ActorState.DEAD: self.entities.remove(entity) continue if self.bounds.contains(entity.position + self.relative_pos): if self.player.visible_to(entity): entity.draw(self.context) self.player.draw(self.context) self.print_stats() self.print_log() self.print_gutter() bearlib.refresh()
def __init__(self, fps=80): super().__init__(fps) self.should_exit = False self.scene_stack = [] self.ctx = BearLibTerminalContext()
""" Demonstrates BearLibTerminalContext (more convenient rendering) and blt_state """ from clubsandwich.blt.state import blt_state from clubsandwich.blt.context import BearLibTerminalContext from clubsandwich.geom import Rect, Size, Point terminal = BearLibTerminalContext() terminal.open() terminal.bkcolor('#ff0000') # move frame of reference to middle of screen with terminal.translate( (Point(blt_state.width, blt_state.height) / 2).floored): terminal.clear_area(Rect(Point(-1, -1), Size(3, 2))) terminal.refresh() # less verbose than terminal.state(terminal.TK_ESCAPE)! while not blt_state.escape: terminal.read() terminal.close()
def draw_tile(self, context: Context): context.color(self.color) context.put(self.point, self.glyph) context.color(self.window.fg_color)