Skip to main content

A Python library for creating hex grid games with pygame.

Project description

PyHexLib - A library for Hexagonal Grids in Python

Python 3.10 pygame 2 License: MIT Code Style: Black CI codecov

pyhexlib is a small Python library for working with hexagonal grids and simple rendering utilities. It contains logic for creating and manipulating hex grids, low-level drawing helpers, and a lightweight renderer that can be used with pygame.

strategic_command.jpg

Main features

  • Mathematical operations and coordinate transformations (axial / offset) and neighborhood queries.
  • Low-level drawing helpers: hex_corner, hex_corners, point-in-polygon checks.
  • Layers and rendering API (pyhexlib.hexagons.Layer(s), pyhexlib.renderer.Renderer) for rendering multiple layers ( grid, color map, color layer).

Note: The README describes the API of the current implementation in pyhexlib/graphics.py, pyhexlib/hexagons.py and pyhexlib/renderer.py.

Requirements

  • Python 3.8+ (tested with Python 3.8–3.12 — please adapt to your target environment if needed)
  • Pygame (used by the example modules)

Install dependencies, e.g. inside a virtual environment:

python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install pygame

If you want to use the package locally in editable mode:

pip install -e .

Quickstart / Examples

The examples below show common usage patterns based on the available API.

screen.blit(surface, (0, 0))

  1. Simple hex grid with HexGridManager and HexGridRenderer (Pygame)
from pyhexlib.hexagons import rectangle_map, HexagonalGrid
from pyhexlib.layers import HexGridManager, HexGridLayer
from pyhexlib.render import HexGridRenderer
import pygame

pygame.init()

ROWS, COLS, RADIUS = 6, 10, 50
hexes = rectangle_map(ROWS, COLS)
hexgrid = HexagonalGrid(hexes)

# create a manager and a simple grid layer
manager = HexGridManager(hexgrid)
grid_layer = HexGridLayer("grid", hexgrid)
manager.add_layer(grid_layer)

renderer = HexGridRenderer(manager, radius=RADIUS)
screen = pygame.display.set_mode(renderer.screen_size)

# render once and blit to the screen
renderer.render()
screen.blit(renderer.surface, (0, 0))
pygame.display.flip()

# simple event loop (keep the window open)
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()

screen.blit(surface, (0, 0))

  1. Using HexGridManager, styled layers and the HexGridRenderer
from pyhexlib.hexagons import rectangle_map, HexagonalGrid
from pyhexlib.layers import HexGridManager, FillGridLayer, OutlineGridLayer
from pyhexlib.render import HexGridRenderer
import pygame

pygame.init()
ROWS, COLS, RADIUS = 15, 20, 40
hexes = rectangle_map(ROWS, COLS)
hexgrid = HexagonalGrid(hexes)

# manager collects layers
manager = HexGridManager(hexgrid)

# background fill layer (default color) and an outline grid
color_layer = FillGridLayer("background", hexgrid, default_color=(200, 200, 255, 255))
# set a single tile to red as an example
color_layer.set_color((5, 5), (255, 0, 0, 255))

grid_layer = OutlineGridLayer("grid", hexgrid, default_color=(0, 0, 0), default_width=1)

manager.add_layer(color_layer)
manager.add_layer(grid_layer)

renderer = HexGridRenderer(manager, RADIUS)
screen = pygame.display.set_mode(renderer.screen_size)

renderer.render()
screen.blit(renderer.surface, (0, 0))
pygame.display.flip()
  1. Low-level functions
  • hex_corner(center, size, i, pointy=False): compute a corner of a hex
  • hex_corners(center, size, pointy=False): returns 6 corners
  • point_in_polygon(x, y, poly): point-in-polygon test

These helpers live in pyhexlib.graphics and pyhexlib.hexagons.

Logging

pyhexlib follows the common convention for libraries: it does not configure output handlers itself and by default attaches a logging.NullHandler to the package logger. This avoids producing log messages unless the calling application configures logging. That gives applications full control over formatting, level and destination of logs.

Recommendations for applications:

  • Configure logging in the application (for example in main.py) with logging.basicConfig() or by adding custom handlers/formatters to logging.getLogger('pyhexlib').
  • To see pyhexlib internal debug logs, set the level for the pyhexlib logger to DEBUG.

Example (Python):

import logging
import pyhexlib

# Application-level logging configuration
logging.basicConfig(level=logging.INFO,
                    format="%(asctime)s [%(levelname)s] %(name)s: %(message)s")

# Make pyhexlib logs more verbose when needed
logging.getLogger('pyhexlib').setLevel(logging.DEBUG)

# Optionally initialize pyhexlib with the same level
pyhexlib.init(orientation=pyhexlib.Orientation.FLAT, log_level=logging.DEBUG)

PowerShell (setup & run):

python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt
python examples\02_token_game\main.py

Note: The example scripts in examples/ already include a simple logging configuration so that running them produces sensible output.

Module / API overview

(pyhexlib currently contains three primary files in the package folder — here are the most important available classes/functions)

  • pyhexlib.graphics

    • Hex: representation of a cell (row, col, center, size, edges, fill_color)
    • HexGrid(rows, cols, hex_size, line_color, line_width, fill_color): grid with a draw(surface) method; conversion methods xy_to_rq, rq_to_xy, get_hex_at, neighbors, distance.
    • Helper functions: hex_corner, hex_corners, point_in_polygon, axial_to_offset, offset_to_axial.
  • pyhexlib.hexagons

    • Hexagon / Hexagons: alternative data structures (similar to graphics).
    • Layer, GridLayer, ColorLayer, ColorMapLayer, Layers: simple layer objects for storing color/display properties.
  • pyhexlib.renderer

    • Renderer(layers, hex_size): creates an internal hex layout and provides render(surface) as well as render_layer, render_grid and render_color.

Note: The implementation is intentionally simple and targets Pygame as a rendering backend, but it can be adapted for other surfaces.

Development

Recommended development setup (Windows PowerShell):

python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -e .[dev]
pip install -r requirements-dev.txt  # optional, if present

Recommended tools:

  • pytest for tests
  • black / flake8 / pylint for formatting and linting
  • pre-commit hooks for automatic formatting (optional)

Tests: Put tests in tests/. Useful small tests include:

  • Grid creation, size and compute_screen_size()
  • Conversion functions rq_to_xy / xy_to_rq
  • neighbors() and distance()

CI: Use GitHub Actions with a Python matrix for automated testing and linting.

License

This software is published under the MIT license. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyhexlib-0.1.1.tar.gz (44.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyhexlib-0.1.1-py3-none-any.whl (54.2 kB view details)

Uploaded Python 3

File details

Details for the file pyhexlib-0.1.1.tar.gz.

File metadata

  • Download URL: pyhexlib-0.1.1.tar.gz
  • Upload date:
  • Size: 44.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for pyhexlib-0.1.1.tar.gz
Algorithm Hash digest
SHA256 68a8d482bd8e0d4a840ff17ec7f9b1b60ddee350c380560ed663be908a092fa0
MD5 9ae0e9f951728237beb4ce571b7a3465
BLAKE2b-256 be737017f76cde2ca4ac01e2163bd560ece45900b60bd9f6de94fa24e4936a83

See more details on using hashes here.

File details

Details for the file pyhexlib-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyhexlib-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 54.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for pyhexlib-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 28edbdc6ce1e4e7ab3f71e126e070606ce14256a37444646a5c184c57a5717e7
MD5 6a25209fe35ce6164ebfdc7951619e54
BLAKE2b-256 bc83088c7f17c6e4568effa6ac85328693c028905bb5c8e01611a1afe5132844

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page