termgame.graphics package

Submodules

termgame.graphics.pixel module

Description: This file defines two classes for working with RGB colors: RGBColor and RGBPixel. RGBColor is a simple class for representing an RGB color with values between 0-255 for the red, green, and blue channels. RGBPixel extends RGBColor to include a symbol and transparency value for displaying pixels in a terminal.

Author: Gregory Glatzer

Date: 3/25/2023

class termgame.graphics.pixel.RGBColor(r: int = 0, g: int = 0, b: int = 0)

Bases: object

Represents an RGB color. It stores values for red, green, and blue channels, each with a range of 0 to 255.

set(value: int, channel: str) RGBColor

Set the color value for a specific RGB channel.

Parameters:
  • value (int) – An integer value between 0 and 255, inclusive, representing the color value to set.

  • channel (str) – The channel for which to set the color value. Must be one of “r”, “g”, or “b”.

Returns:

The updated RGBColor object.

Return type:

RGBColor

Raises:

ValueError – If the value is not between 0 and 255, inclusive, or if the channel is not one of “r”, “g”, or “b”.

set_tuple(values: Tuple[int, int, int]) RGBColor

Sets the color for the RGB channels using a tuple of 3 integer values between 0-255, inclusive.

Parameters:

values (Tuple[int, int, int]) – A tuple of 3 integers representing the RGB values of the color.

Returns:

Returns the updated RGBColor object after setting the RGB values.

Return type:

RGBColor

Raises:

ValueError – If the input values are not integers between 0-255, inclusive.

class termgame.graphics.pixel.RGBPixel(color: RGBColor = (0, 0, 0), symbol: str = '\u2001\u2001', transparent: bool = False)

Bases: object

A class representing an RGB pixel that can keep track of its color.

Attributes

colorRGBColor

The color of the pixel in RGB format.

symbolstr, optional

The symbol that represents the pixel (default ”  “).

transparentbool, optional

Whether the pixel is transparent (default False).

Methods

display() -> None:

Prints the pixel to the console with its current color.

get() -> str:

Returns a string representation of the pixel with its current color.

__eq__(self, other: object) -> bool:

Compares the pixel with another object for equality.

Notes

A pixel’s color can be set by creating an RGBColor object and passing it to the constructor.

color: RGBColor = (0, 0, 0)
display() None

Prints the pixel to the console with its current color.

get() str

Returns a string representation of the pixel with its current color.

symbol: str = '\u2001\u2001'
transparent: bool = False

termgame.graphics.screen module

Description: Define the Screen class. Author: Gregory Glatzer Date: 10/22/2022

class termgame.graphics.screen.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

Module contents

The graphics module contains classes for working with the screen and pixels.

termgame.graphics.Pixel

alias of RGBPixel

class termgame.graphics.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