Rendering System

The rendering module provides an abstract renderer interface and Pygame implementation for visualizing the game.

Renderer Base

Abstract rendering interface.

This module provides the abstract renderer interface that different rendering backends can implement.

class barebones_rpg.rendering.renderer.Color(r, g, b, a=255)[source]

Bases: object

RGB color representation.

r: int
g: int
b: int
a: int = 255
to_tuple()[source]

Convert to tuple (R, G, B, A).

Return type:

Tuple[int, int, int, int]

to_rgb()[source]

Convert to RGB tuple.

Return type:

Tuple[int, int, int]

__init__(r, g, b, a=255)
class barebones_rpg.rendering.renderer.Colors[source]

Bases: object

Common color constants.

BLACK = Color(r=0, g=0, b=0, a=255)
WHITE = Color(r=255, g=255, b=255, a=255)
RED = Color(r=255, g=0, b=0, a=255)
GREEN = Color(r=0, g=255, b=0, a=255)
BLUE = Color(r=0, g=0, b=255, a=255)
YELLOW = Color(r=255, g=255, b=0, a=255)
CYAN = Color(r=0, g=255, b=255, a=255)
MAGENTA = Color(r=255, g=0, b=255, a=255)
GRAY = Color(r=128, g=128, b=128, a=255)
DARK_GRAY = Color(r=64, g=64, b=64, a=255)
LIGHT_GRAY = Color(r=192, g=192, b=192, a=255)
class barebones_rpg.rendering.renderer.Renderer(width, height, title='RPG Game')[source]

Bases: ABC

Abstract base class for renderers.

Different rendering backends (Pygame, terminal, web, etc.) implement this interface.

__init__(width, height, title='RPG Game')[source]

Initialize the renderer.

Parameters:
  • width (int) – Screen width

  • height (int) – Screen height

  • title (str) – Window title

abstractmethod initialize()[source]

Initialize the rendering system.

Return type:

None

abstractmethod shutdown()[source]

Shutdown the rendering system.

Return type:

None

abstractmethod clear(color=None)[source]

Clear the screen.

Parameters:

color (Optional[Color]) – Background color (default: black)

Return type:

None

abstractmethod present()[source]

Present/flip the rendered frame to screen.

Return type:

None

abstractmethod draw_rect(x, y, width, height, color, filled=True)[source]

Draw a rectangle.

Parameters:
  • x (int) – X position

  • y (int) – Y position

  • width (int) – Rectangle width

  • height (int) – Rectangle height

  • color (Color) – Rectangle color

  • filled (bool) – Whether to fill the rectangle

Return type:

None

abstractmethod draw_text(text, x, y, color=Color(r=255, g=255, b=255, a=255), font_size=16)[source]

Draw text.

Parameters:
  • text (str) – Text to draw

  • x (int) – X position

  • y (int) – Y position

  • color (Color) – Text color

  • font_size (int) – Font size

Return type:

None

abstractmethod draw_circle(x, y, radius, color, filled=True)[source]

Draw a circle.

Parameters:
  • x (int) – Center X position

  • y (int) – Center Y position

  • radius (int) – Circle radius

  • color (Color) – Circle color

  • filled (bool) – Whether to fill the circle

Return type:

None

abstractmethod draw_sprite(sprite_id, x, y, width=None, height=None)[source]

Draw a sprite/image.

Parameters:
  • sprite_id (str) – ID/path of sprite

  • x (int) – X position

  • y (int) – Y position

  • width (Optional[int]) – Sprite width (None = original)

  • height (Optional[int]) – Sprite height (None = original)

Return type:

None

abstractmethod handle_events()[source]

Handle input events.

Return type:

List[Any]

Returns:

List of events

abstractmethod get_delta_time()[source]

Get time since last frame in seconds.

Return type:

float

Returns:

Delta time

is_running()[source]

Check if renderer is running.

Return type:

bool

Returns:

True if renderer is active

class barebones_rpg.rendering.renderer.UIElement(x, y, width, height)[source]

Bases: ABC

Base class for UI elements.

__init__(x, y, width, height)[source]

Initialize UI element.

Parameters:
  • x (int) – X position

  • y (int) – Y position

  • width (int) – Element width

  • height (int) – Element height

abstractmethod render(renderer)[source]

Render the UI element.

Parameters:

renderer (Renderer) – Renderer to draw with

Return type:

None

abstractmethod handle_event(event)[source]

Handle an input event.

Parameters:

event (Any) – Input event

Return type:

bool

Returns:

True if event was handled

class barebones_rpg.rendering.renderer.TextBox(x, y, width, height, text='', font_size=16, text_color=Color(r=255, g=255, b=255, a=255), bg_color=Color(r=0, g=0, b=0, a=255))[source]

Bases: UIElement

Simple text box UI element.

__init__(x, y, width, height, text='', font_size=16, text_color=Color(r=255, g=255, b=255, a=255), bg_color=Color(r=0, g=0, b=0, a=255))[source]

Initialize text box.

Parameters:
  • x (int) – X position

  • y (int) – Y position

  • width (int) – Box width

  • height (int) – Box height

  • text (str) – Text to display

  • font_size (int) – Font size

  • text_color (Color) – Text color

  • bg_color (Color) – Background color

render(renderer)[source]

Render the text box.

Return type:

None

handle_event(event)[source]

Handle event (text boxes don’t handle events by default).

Return type:

bool

set_text(text)[source]

Set the text content.

Parameters:

text (str) – New text

Return type:

None

Pygame Renderer

Pygame implementation of the renderer.

This provides a basic Pygame-based renderer for the framework.

class barebones_rpg.rendering.pygame_renderer.PygameRenderer(width, height, title='RPG Game')[source]

Bases: Renderer

Pygame-based renderer implementation.

Example

>>> renderer = PygameRenderer(800, 600, "My RPG")
>>> renderer.initialize()
>>> renderer.clear()
>>> renderer.draw_text("Hello World!", 100, 100)
>>> renderer.present()
__init__(width, height, title='RPG Game')[source]

Initialize Pygame renderer.

Parameters:
  • width (int) – Screen width

  • height (int) – Screen height

  • title (str) – Window title

initialize()[source]

Initialize Pygame and create window.

Return type:

None

shutdown()[source]

Shutdown Pygame.

Return type:

None

clear(color=None)[source]

Clear screen to color.

Parameters:

color (Optional[Color]) – Background color

Return type:

None

present()[source]

Present the rendered frame.

Return type:

None

draw_rect(x, y, width, height, color, filled=True)[source]

Draw a rectangle.

Parameters:
  • x (int) – X position

  • y (int) – Y position

  • width (int) – Rectangle width

  • height (int) – Rectangle height

  • color (Color) – Rectangle color

  • filled (bool) – Whether to fill

Return type:

None

draw_circle(x, y, radius, color, filled=True)[source]

Draw a circle.

Parameters:
  • x (int) – X center position

  • y (int) – Y center position

  • radius (int) – Circle radius

  • color (Color) – Circle color

  • filled (bool) – Whether to fill (True) or just outline (False)

Return type:

None

draw_text(text, x, y, color=Color(r=255, g=255, b=255, a=255), font_size=16)[source]

Draw text.

Parameters:
  • text (str) – Text to draw

  • x (int) – X position

  • y (int) – Y position

  • color (Color) – Text color

  • font_size (int) – Font size

Return type:

None

draw_sprite(sprite_id, x, y, width=None, height=None)[source]

Draw a sprite.

Parameters:
  • sprite_id (str) – Sprite ID/path

  • x (int) – X position

  • y (int) – Y position

  • width (Optional[int]) – Scaled width

  • height (Optional[int]) – Scaled height

Return type:

None

handle_events()[source]

Handle Pygame events.

Return type:

List[Any]

Returns:

List of pygame events

get_delta_time()[source]

Get delta time in seconds.

Return type:

float

Returns:

Time since last frame

load_sprite(sprite_id, path)[source]

Preload a sprite.

Parameters:
  • sprite_id (str) – ID to reference sprite by

  • path (str) – Path to image file

Return type:

bool

Returns:

True if loaded successfully

class barebones_rpg.rendering.pygame_renderer.PygameGameLoop(game, renderer=None)[source]

Bases: object

Game loop using Pygame renderer.

This integrates the Pygame renderer with the core Game class.

Example

>>> from barebones_rpg.core import Game, GameConfig
>>> game = Game(GameConfig(title="My RPG"))
>>> loop = PygameGameLoop(game)
>>> loop.run()
__init__(game, renderer=None)[source]

Initialize game loop.

Parameters:
run()[source]

Run the game loop.

Return type:

None

Tile Renderer

Tile-based rendering utilities.

This module provides rendering helpers specifically for tile-based maps, including grid rendering, entity rendering, and visual effects like movement highlights and path previews.

class barebones_rpg.rendering.tile_renderer.TileRenderer(renderer, tile_size=32)[source]

Bases: object

Renderer for tile-based maps and entities.

This class handles rendering of: - Tile grids with borders - Movement range highlights - Path previews - Hover effects - Entities on tiles - Entity HP bars

Parameters:
  • renderer (Renderer) – The base renderer to use for drawing

  • tile_size (int) – Size of each tile in pixels (default: 32)

__init__(renderer, tile_size=32)[source]

Initialize the tile renderer.

Parameters:
  • renderer (Renderer) – The base renderer to use for drawing

  • tile_size (int) – Size of each tile in pixels

render_tile_grid(location, walkable_color=None, wall_color=None, grid_line_color=None)[source]

Render the tile grid for a location.

Parameters:
  • location (Location) – The location to render

  • walkable_color (Optional[Color]) – Color for walkable tiles (default: dark gray)

  • wall_color (Optional[Color]) – Color for non-walkable tiles (default: darker gray)

  • grid_line_color (Optional[Color]) – Color for grid lines (default: medium gray)

render_movement_highlights(valid_moves, current_position=None, highlight_color=None, padding=2)[source]

Render highlights for valid movement tiles.

Parameters:
  • valid_moves (Set[Tuple[int, int]]) – Set of valid move positions

  • current_position (Optional[Tuple[int, int]]) – Position to exclude from highlighting (usually current position)

  • highlight_color (Optional[Color]) – Color for the highlight (default: blue)

  • padding (int) – Padding from tile edges in pixels

render_path_preview(path, skip_first=True, path_color=None, padding=4)[source]

Render a path preview showing movement trajectory.

Parameters:
  • path (List[Tuple[int, int]]) – List of positions in the path

  • skip_first (bool) – Whether to skip rendering the first position (current position)

  • path_color (Optional[Color]) – Color for the path (default: light blue)

  • padding (int) – Padding from tile edges in pixels

render_hover_highlight(position, hover_color=None)[source]

Render a hover highlight on a tile.

Parameters:
  • position (Tuple[int, int]) – The tile position to highlight

  • hover_color (Optional[Color]) – Color for the hover effect (default: yellow)

render_entity(entity, faction_colors=None, draw_name=True, draw_hp_bar=True)[source]

Render an entity on a tile.

Parameters:
  • entity (Entity) – The entity to render

  • faction_colors (Optional[dict]) – Dict mapping faction names to colors (optional)

  • draw_name (bool) – Whether to draw the entity’s name

  • draw_hp_bar (bool) – Whether to draw the entity’s HP bar

render_entity_hp_bar(entity, bar_height=4, padding=2, bg_color=None, hp_color=None)[source]

Render an HP bar for an entity.

Parameters:
  • entity (Entity) – The entity to render HP bar for

  • bar_height (int) – Height of the HP bar in pixels

  • padding (int) – Padding from tile edges

  • bg_color (Optional[Color]) – Background color (default: red)

  • hp_color (Optional[Color]) – HP fill color (default: green)

render_location(location, valid_moves=None, path_preview=None, hover_tile=None, current_entity_position=None, draw_entity_names=True, draw_entity_hp_bars=True)[source]

Render a complete location with all entities and effects.

This is a convenience method that renders everything in the correct order.

Parameters:
  • location (Location) – The location to render

  • valid_moves (Optional[Set[Tuple[int, int]]]) – Set of valid move positions to highlight

  • path_preview (Optional[List[Tuple[int, int]]]) – Path to preview

  • hover_tile (Optional[Tuple[int, int]]) – Tile being hovered over

  • current_entity_position (Optional[Tuple[int, int]]) – Position of current entity (excluded from highlights)

  • draw_entity_names (bool) – Whether to draw entity names

  • draw_entity_hp_bars (bool) – Whether to draw entity HP bars

screen_to_tile(screen_x, screen_y)[source]

Convert screen coordinates to tile coordinates.

Parameters:
  • screen_x (int) – Screen X coordinate in pixels

  • screen_y (int) – Screen Y coordinate in pixels

Return type:

Tuple[int, int]

Returns:

Tile coordinates (grid_x, grid_y)

tile_to_screen(tile_x, tile_y)[source]

Convert tile coordinates to screen coordinates (top-left of tile).

Parameters:
  • tile_x (int) – Tile X coordinate

  • tile_y (int) – Tile Y coordinate

Return type:

Tuple[int, int]

Returns:

Screen coordinates (screen_x, screen_y)

tile_to_screen_center(tile_x, tile_y)[source]

Convert tile coordinates to screen coordinates (center of tile).

Parameters:
  • tile_x (int) – Tile X coordinate

  • tile_y (int) – Tile Y coordinate

Return type:

Tuple[int, int]

Returns:

Screen coordinates at center of tile (screen_x, screen_y)

UI Components

Common UI components for RPG games.

This module provides reusable UI components like resource bars, turn indicators, quest displays, and instruction panels.

class barebones_rpg.rendering.ui_components.UIComponents(renderer)[source]

Bases: object

Collection of reusable UI components.

This class provides common UI elements that can be easily rendered in RPG games, such as turn indicators, resource bars, quest lists, etc.

Parameters:

renderer (Renderer) – The base renderer to use

__init__(renderer)[source]

Initialize the UI components.

Parameters:

renderer (Renderer) – The base renderer to use

render_turn_indicator(is_player_turn, position, player_text='YOUR TURN', enemy_text='ENEMY TURN', font_size=20)[source]

Render a turn indicator showing whose turn it is.

Parameters:
  • is_player_turn (bool) – Whether it’s the player’s turn

  • position (Tuple[int, int]) – Position to render at (x, y)

  • player_text (str) – Text to show on player’s turn

  • enemy_text (str) – Text to show on enemy’s turn

  • font_size (int) – Font size for the text

render_resource_bar(label, current, maximum, position, font_size=20, text_color=None)[source]

Render a resource bar (HP, MP, AP, etc.).

Parameters:
  • label (str) – Label for the resource (e.g., “HP”, “MP”, “AP”)

  • current (int) – Current value

  • maximum (int) – Maximum value

  • position (Tuple[int, int]) – Position to render at (x, y)

  • font_size (int) – Font size for the text

  • text_color (Optional[Color]) – Color for the text (default: white)

render_resource_bar_with_fill(label, current, maximum, position, bar_width=100, bar_height=20, font_size=16, fill_color=None, bg_color=None)[source]

Render a resource bar with a visual fill indicator.

Parameters:
  • label (str) – Label for the resource

  • current (int) – Current value

  • maximum (int) – Maximum value

  • position (Tuple[int, int]) – Position to render at (x, y)

  • bar_width (int) – Width of the bar in pixels

  • bar_height (int) – Height of the bar in pixels

  • font_size (int) – Font size for the text

  • fill_color (Optional[Color]) – Color for the filled portion

  • bg_color (Optional[Color]) – Color for the background

render_quest_list(quest_manager, position, max_quests=None, show_objectives=True, title='Active Quests:', title_font_size=16, quest_font_size=14, objective_font_size=12)[source]

Render a list of active quests with objectives.

Parameters:
  • quest_manager (QuestManager) – The quest manager containing quests

  • position (Tuple[int, int]) – Position to render at (x, y)

  • max_quests (Optional[int]) – Maximum number of quests to show (None = all)

  • show_objectives (bool) – Whether to show objectives

  • title (str) – Title text

  • title_font_size (int) – Font size for the title

  • quest_font_size (int) – Font size for quest names

  • objective_font_size (int) – Font size for objectives

render_instructions(instructions, position, font_size=12, line_spacing=15, text_color=None)[source]

Render a list of instruction text.

Parameters:
  • instructions (List[str]) – List of instruction strings

  • position (Tuple[int, int]) – Position to render at (x, y)

  • font_size (int) – Font size for the text

  • line_spacing (int) – Spacing between lines in pixels

  • text_color (Optional[Color]) – Color for the text (default: light gray)

render_message_log(messages, position, max_messages=10, font_size=14, line_spacing=20, text_color=None)[source]

Render a message log (e.g., combat log).

Parameters:
  • messages (List[str]) – List of message strings

  • position (Tuple[int, int]) – Position to render at (x, y)

  • max_messages (int) – Maximum number of messages to show

  • font_size (int) – Font size for the text

  • line_spacing (int) – Spacing between lines in pixels

  • text_color (Optional[Color]) – Color for the text (default: white)

render_stat_panel(entity, position, show_name=True, show_hp=True, show_mp=True, show_atk=True, show_def=True, name_font_size=20, stat_font_size=16, line_spacing=20)[source]

Render a stat panel for an entity.

Parameters:
  • entity (Any) – Entity to show stats for (must have stats attribute)

  • position (Tuple[int, int]) – Position to render at (x, y)

  • show_name (bool) – Whether to show entity name

  • show_hp (bool) – Whether to show HP

  • show_mp (bool) – Whether to show MP

  • show_atk (bool) – Whether to show attack

  • show_def (bool) – Whether to show defense

  • name_font_size (int) – Font size for the name

  • stat_font_size (int) – Font size for stats

  • line_spacing (int) – Spacing between lines

render_title_screen_text(title, position, font_size=32, color=None)[source]

Render large title text.

Parameters:
  • title (str) – Title text

  • position (Tuple[int, int]) – Position to render at (x, y)

  • font_size (int) – Font size for the title

  • color (Optional[Color]) – Color for the text (default: white)

render_button(text, position, width, height, is_hovered=False, bg_color=None, hover_color=None, text_color=None, font_size=16)[source]

Render a button with hover effect.

Parameters:
  • text (str) – Button text

  • position (Tuple[int, int]) – Position to render at (x, y)

  • width (int) – Button width

  • height (int) – Button height

  • is_hovered (bool) – Whether the button is being hovered

  • bg_color (Optional[Color]) – Background color (default: dark gray)

  • hover_color (Optional[Color]) – Hover background color (default: lighter gray)

  • text_color (Optional[Color]) – Text color (default: white)

  • font_size (int) – Font size for the text

Click to Move

Click-to-move handler for tile-based games.

This module provides an input handler for click-based movement on tile maps, including hover effects, path previews, and click handling.

class barebones_rpg.rendering.click_to_move.ClickToMoveHandler(tile_size, grid_width, grid_height, pathfinder=None)[source]

Bases: object

Handler for click-to-move input on tile-based maps.

This class manages: - Mouse hover tracking - Path preview calculation - Click detection and tile selection - Callbacks for movement and interaction

Parameters:
  • tile_size (int) – Size of each tile in pixels

  • grid_width (int) – Width of the grid in tiles

  • grid_height (int) – Height of the grid in tiles

  • pathfinder (Optional[TilemapPathfinder]) – Optional pathfinder for path previews

__init__(tile_size, grid_width, grid_height, pathfinder=None)[source]

Initialize the click-to-move handler.

Parameters:
  • tile_size (int) – Size of each tile in pixels

  • grid_width (int) – Width of the grid in tiles

  • grid_height (int) – Height of the grid in tiles

  • pathfinder (Optional[TilemapPathfinder]) – Optional pathfinder for path previews

set_pathfinder(pathfinder)[source]

Set or update the pathfinder for path previews.

Parameters:

pathfinder (TilemapPathfinder) – The pathfinder to use

screen_to_tile(screen_x, screen_y)[source]

Convert screen coordinates to tile coordinates.

Parameters:
  • screen_x (int) – Screen X coordinate in pixels

  • screen_y (int) – Screen Y coordinate in pixels

Return type:

Optional[Tuple[int, int]]

Returns:

Tile coordinates (x, y) or None if out of bounds

handle_mouse_motion(event, valid_moves=None, current_position=None)[source]

Handle mouse motion events to update hover and path preview.

Parameters:
handle_mouse_click(event, get_entity_at=None)[source]

Handle mouse click events.

Parameters:
Return type:

Optional[Tuple[int, int]]

Returns:

Clicked tile position or None

get_hover_tile()[source]

Get the currently hovered tile.

Return type:

Optional[Tuple[int, int]]

Returns:

Hovered tile position or None

get_path_preview()[source]

Get the current path preview.

Return type:

List[Tuple[int, int]]

Returns:

List of tile positions in the preview path

clear_hover()[source]

Clear hover state and path preview.

class barebones_rpg.rendering.click_to_move.TileInteractionHandler(click_handler)[source]

Bases: object

Higher-level handler for tile-based interactions.

This combines ClickToMoveHandler with logic for handling different types of interactions (movement, attacking, talking to NPCs, etc.)

Parameters:

click_handler (ClickToMoveHandler) – The click-to-move handler to use

__init__(click_handler)[source]

Initialize the interaction handler.

Parameters:

click_handler (ClickToMoveHandler) – The click-to-move handler to use

handle_tile_click(tile_pos, entity_at_tile, valid_moves)[source]

Handle a tile click with interaction logic.

Parameters:
  • tile_pos (Tuple[int, int]) – The clicked tile position

  • entity_at_tile (Optional[Any]) – Entity at the clicked position (if any)

  • valid_moves (Set[Tuple[int, int]]) – Set of valid movement tiles

set_callbacks(on_move=None, on_attack_enemy=None, on_interact_npc=None, on_interact_item=None)[source]

Set interaction callbacks.

Parameters: