Skip to main content

A Python library to render text from bitmap font images

Project description

Pygame-BitmapFont

A lightweight and easy-to-use library for rendering bitmap fonts in Pygame.

extractor.png

Overview

Pygame-BitmapFont provides a simple way to load and render text using pre-rendered bitmap font images and associated font data files. This approach is often preferred in game development for its performance and consistent look across different platforms.

Key Features:

  • Easy Integration: Designed specifically for Pygame, ensuring smooth integration into your game projects.
  • Multiple Font Formats: Supports custom .json human-readable and editable font formats.
  • Efficient Rendering: Leverages Pygame's Surface and drawing capabilities for fast text rendering.
  • Customizable: Offers options for color, scaling and text alignment.
  • Clear Examples: Comes with illustrative examples to get you started quickly.
  • Tools Included: Comes with CLI bitmapfont-extract tool for easy preparation of .json fonts from bitmap images.
  • Installable via pip: Easily install the library using the Python package installer.

Installation

  1. Prerequisites: Make sure you have Pygame installed in your Python environment. If not, you can install it using pip:
    pip install pygame
    
  2. Install via pip (Recommended): Once the package is available on PyPI, you can install BitmapFont using:
    pip install git+https://github.com/xdoko01/Pygame-BitmapFont.git
    
    or
    pip install pgbitmapfont
    
  3. Alternatively (for development or if not on PyPI):
    • Download or Clone: Clone the entire repository:
      git clone [https://github.com/xdoko01/Pygame-BitmapFont.git](https://github.com/xdoko01/Pygame-BitmapFont.git)
      cd Pygame-BitmapFont
      
    • Place the pgbitmapfont package: Ensure the pgbitmapfont directory (containing the library's __init__.py and other modules) is in your Python project's site-packages directory or within your project structure where Python can import it.

Usage

Here's a basic example of how to use the Pygame-BitmapFont library in your Pygame project after installing it (either via pip or by placing the package):

import pygame
from pgbitmapfont import BitmapFont

# Initialize Pygame
pygame.init()

# Set up display
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pygame-BitmapFont Example")

# Load the font
font = BitmapFont("path/to/your/font.json")

# Text to render
text = "Hello, Pygame!"
text_position = (100, 100)

# Game loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Clear the screen
    screen.fill((0, 0, 0))  # Black

    # Render and blit the text
    screen.blit(font.render(text)[0], text_position)

    # Update the display
    pygame.display.flip()

# Quit Pygame
pygame.quit()

Usage of bitmapfont-extract tool

extractor.png

Once the Pygame-BitmapFont (pgbitmapfont) package is installed via pip, you can use the following command to run the bitmapfont-extract tool:

bitmapfont-extract --img font_image.png --out font.json

Controls

  • Select the suitable grid resolution by 'PgUp', 'PgDown'
  • Select the cell with texture by mouse and/or cursor keys
  • Press INSERT and enter the character under which it will be stored.
  • Once done, press RETURN to save and continue.

Tips

  • once saved, the cell is grey

TODOs

  • More test cases
  • FreeDim font having different heights of characters
  • Usage of default colorkey
  • Adding spaces vertical/horizontal
  • Dummy pgcolor for FreeDim font

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

pgbitmapfont-0.1.3.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

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

pgbitmapfont-0.1.3-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file pgbitmapfont-0.1.3.tar.gz.

File metadata

  • Download URL: pgbitmapfont-0.1.3.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.0

File hashes

Hashes for pgbitmapfont-0.1.3.tar.gz
Algorithm Hash digest
SHA256 c57c2f83dc43c9b9c221463d0a1e14f11edb2333724a1e96ca638c971bee110c
MD5 de5a1d61b9e85de1c04669a297ae40a8
BLAKE2b-256 c555b8a8e4873b7ef916b81f26b4c2588b5056de573cf1aa7afde1a606532e6b

See more details on using hashes here.

File details

Details for the file pgbitmapfont-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pgbitmapfont-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.0

File hashes

Hashes for pgbitmapfont-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4ed0656457a847c56bd7411bddc6248e27c1985d97c61dedfbf9a8a36efe32d5
MD5 0d506526399c39a06f8f5934bfca2ac3
BLAKE2b-256 2aae6099cd248807deda2fb3345cc103acc918c07a838dd18e390f200f5280a6

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