API

Subpackages

Submodules

termgame.logger module

logger.py Description: Logging for the application Author: Gregory Glatzer Date: 12/12/2022

termgame.settings module

Settings for the project.

class termgame.settings.LogSettings(level: str = 'INFO', file: str = 'logs/game.log')

Bases: object

A class for configuring logging settings.

Variables:
  • level (str) – The logging level (default: “INFO”).

  • file (str) – The file to log to (default: “logs/game.log”).

file: str = 'logs/game.log'
level: str = 'INFO'
class termgame.settings.RenderSettings(fontsize: int = 6)

Bases: object

A class for configuring rendering settings.

Variables:

fontsize (int) – The recommended font size for the terminal (default: 6).

fontsize: int = 6
class termgame.settings.RuntimeSettings(fps: int = 20, headless: bool = False, ppf: int = 10, wait_for_start: bool = True)

Bases: object

A class for configuring runtime settings.

Variables:
  • fps (int) – The number of frames per second to render (default: 20).

  • headless (bool) – Whether to run the game in headless mode (default: False).

  • ppf (int) – The number of physics steps per frame (default: 10).

  • wait_for_start (bool) – Whether to wait for the user to press a key before starting the game (default: True).

fps: int = 20
headless: bool = False
ppf: int = 10
wait_for_start: bool = True
class termgame.settings.Settings(log_settings: LogSettings = LogSettings(level='INFO', file='logs/game.log'), render_settings: RenderSettings = RenderSettings(fontsize=6), runtime_settings: RuntimeSettings = RuntimeSettings(fps=20, headless=False, ppf=10, wait_for_start=True))

Bases: object

A class for configuring all settings required for the project.

Variables:
log_settings: LogSettings = LogSettings(level='INFO', file='logs/game.log')
render_settings: RenderSettings = RenderSettings(fontsize=6)
runtime_settings: RuntimeSettings = RuntimeSettings(fps=20, headless=False, ppf=10, wait_for_start=True)

termgame.util module

Description: This module contains utility functions for termgame. Author: Gregory Glatzer Date: 4/04/2023

termgame.util.clamp(value: Any, smallest: Any, largest: Any) Any
termgame.util.flip_animation(sprites: List[Screen], axis: str = 'x') List[Screen]

Flip an animation along the given axis (x or y).

Parameters:
  • sprites (List[Screen]) – List of sprites to flip.

  • axis (str) – Axis to flip the animation along, either x or y.

Returns:

List of flipped sprites.

Return type:

List[Screen]

termgame.util.get_bb_poly(go) Poly

Get a polygon for a gameobject to be used pymunk shape that is a rectangle the width and height of the gameobject.

Parameters:

go (GameObject) – Gameobject to get the bounding box for.

Returns:

pymunk.Poly object representing the bounding box.

Return type:

pymunk.Poly

termgame.util.scroll_sprite(sprite: Screen, dx: int, dy: int) Screen

Scroll a sprite by dx and dy.

Parameters:
  • sprite (Screen) – Sprite to scroll.

  • dx (int) – Number of pixels to scroll in the x direction.

  • dy (int) – Number of pixels to scroll in the y direction.

Returns:

Scrolled sprite.

Return type:

Screen

termgame.util.stretch_animation(sprites: List[Screen], stretch: int)

Stretch an animation out (multiply frames). This effectively slows down the animation.

Parameters:
  • sprites (List[Screen]) – List of sprites to stretch.

  • stretch (int) – Number of times to stretch the animation.

Returns:

List of stretched sprites.

Return type:

List[Screen]

Module contents

Termgame. Create graphical games that run in the terminal. All written in Python.

Created by Gregory Glatzer.

class termgame.Engine(width: int, height: int, gameobjects: List[Gameobject] | None = None)

Bases: object

add_gameobject(gameobject: Gameobject) None

Add a gameobject to the engine.

clear() None

Clear the screen without any blinking.

property elapsed_time: float
property frame: int
property gameobjects: List[Gameobject]
get_gameobjects(names: str | List[str] = '') List[Gameobject]

Get all gameobjects with given name(s). Empty returns all gameobjects.

run() None

Run the engine.

class termgame.Gameobject(x: 'int' = 0, y: 'int' = 0, depth: 'int' = 0, update_order: 'int' = 0, sprites: 'List | None' = None, meshes: 'List | None' = None, on_start: 'Callable' = <function Gameobject.<lambda> at 0x7f7d9efaeb00>, on_update: 'Callable' = <function Gameobject.<lambda> at 0x7f7d9efaeb90>, name: 'str' = '')

Bases: object

get_active_mesh() Any

Access the active mesh (3D) of the gameobject, if any.

get_active_sprite() Any

Access the active sprite (2D) of the gameobject, if any.

get_meshes() Any

Access the meshes (3D) of the gameobject, if any.

get_sprites() Any

Access the sprites (2D) of the gameobject, if any.

property has_physics: bool
property height: int | float
property name: str
set_meshes(meshes) None
set_sprites(sprites) None
property width: int | float
class termgame.PhysicsEngine(width: int, height: int, gravity: tuple[float, float] = (0, 98.1), **engine_kwargs)

Bases: Engine

add_gameobject(gameobject: Gameobject) None
Add a gameobject to the engine. Will also add the rigidbody

to the physics space, if it has one.

run() None

Run the engine.

property space: Space

The pymunk space for 2d physics.

class termgame.PhysicsGameobject(on_fixed_update: ~typing.Callable = <function PhysicsGameobject.<lambda>>, static_body: bool = False, max_velocity: int | None = None, **gameobject_kwargs)

Bases: Gameobject

static from_gameobject(gameobject: Gameobject, static_body: bool = True) PhysicsGameobject

Convert a Gameobject to a PhysicsGameobject.

property has_physics: bool
property rb: Body

Shorthand for self.rigidbody

property rigidbody: Body
class termgame.Screen(width: int, height: int)

Bases: object

clear() None

Clear the screen by setting all pixels to transparent Pixels with no color.

Returns:

None

fill(color: Tuple[int, int, int]) Screen

Fill the entire screen with a given color.

Parameters:

color (Tuple[int, int, int]) – Tuple of three integers (R,G,B) representing the color of the pixel.

Returns:

Screen object with updated pixels.

Return type:

Screen

static from_image(image: ndarray | str, resize: Tuple[int, int] | None = None, has_alpha: bool = False) Screen

Create a screen from an image (can be a path to an image or a numpy array). If resize is not None, the image will be resized to the given size.

Parameters:
  • image (numpy.ndarray | str) – The image to create the screen from.

  • resize (tuple[int, int] | None) – The size to resize the image to.

  • has_alpha (bool) – A boolean indicating whether the image has an alpha channel.

Returns:

The screen created from the image.

Return type:

Screen

paint_image(image: ndarray | str, x: int = 0, y: int = 0, has_alpha: bool = False, resize: Tuple[int, int] | None = None) Screen

Paint an image on the screen (can be a path to an image or a numpy array). Resize the image to the given size if resize is not None.

Parameters:
  • image (np.ndarray | str) – Path to an image or a numpy array.

  • x (int) – x-coordinate of the top-left corner of the image on the screen. Default is 0.

  • y (int) – y-coordinate of the top-left corner of the image on the screen. Default is 0.

  • has_alpha – Whether the image has an alpha channel

  • resize (Tuple[int, int]) – Resize the image to the given size

Returns:

Screen object with updated pixels.

Return type:

Screen

paint_screen(screen: Screen, x: int, y: int) Screen

Paint another screen onto the screen at the given coordinates.

Parameters:
  • screen (Screen) – The screen to paint onto this screen.

  • x (int) – The x-coordinate to paint the screen.

  • y (int) – The y-coordinate to paint the screen.

Returns:

The resulting screen after painting the other screen onto this screen.

Return type:

Screen

render() None

Render the screen and print it to stdout

set_px(px: RGBPixel, x: int, y: int) Screen

Set pixel at (x,y) on the screen to a new pixel.

Parameters:
  • px (RGBPixel) – RGBPixel object to be set on the screen.

  • x (int) – x-coordinate of the pixel on the screen.

  • y (int) – y-coordinate of the pixel on the screen.

Returns:

Screen object with updated pixel.

Return type:

Screen

set_px_color(color: Tuple[int, int, int], x: int, y: int) Screen

Shorthand to set pixel at (x, y) when all you want to do is change the color.

Parameters:
  • color (Tuple[int, int, int]) – Tuple of three integers (R,G,B) representing the color of the pixel.

  • x (int) – x-coordinate of the pixel on the screen.

  • y (int) – y-coordinate of the pixel on the screen.

Returns:

Screen object with updated pixel.

Return type:

Screen