Sprixel

class pygamelib.gfx.core.Sprixel(model='', bg_color=None, fg_color=None, is_bg_transparent=None)

Bases: object

A sprixel is the representation of 1 cell of the sprite or one cell on the Board. It is not really a pixel but it is the closest notion we’ll have. A Sprixel has a background color, a foreground color and a model. All regular BoardItems can now use a sprixel instead of a model (but simple model is still supported of course).

If the background color and the is_bg_transparent are None, the sprixel will be automatically configured with transparent background. In that case, as we cannot really achieve transparency in the console, the sprixel will take the background color of whatever it is overlapping.

Important

BREAKING CHANGE: in version 1.3.0 background and foreground colors use the new Color object. Therefor, Sprixel does not accept ANSI sequences anymore for the bg_color and fg_color parameters.

Example:

player = Player(sprixel=Sprixel(
                                '#',
                                Color(128,56,32),
                                Color(255,255,0),
                                ))
__init__(model='', bg_color=None, fg_color=None, is_bg_transparent=None)
Parameters:
  • model (str) – The model, it can be any string. Preferrably a single character.
  • bg_color (Color) – A Color object to configure the background color.
  • fg_color (Color) – A Color object to configure the foreground color.
  • is_bg_transparent (bool) – Set the background of the Sprixel to be transparent. It tells the engine to replace the background of the Sprixel by the background color of the overlapped sprixel.

Methods

__init__([model, bg_color, fg_color, …])
param model:The model, it can be any string. Preferrably a single character.
black_rect() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.BLACK_RECT.
black_square() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.BLACK_SQUARE.
blue_rect() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.BLUE_RECT.
blue_square() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.BLUE_SQUARE.
cyan_rect() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.CYAN_RECT.
cyan_square() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.CYAN_SQUARE.
from_ansi(string[, model]) Takes an ANSI string, parse it and return a Sprixel.
green_rect() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.GREEN_RECT.
green_square() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.GREEN_SQUARE.
load(data) Create a new Sprixel object based on serialized data.
magenta_rect() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.MAGENTA_RECT.
magenta_square() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.MAGENTA_SQUARE.
red_rect() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.RED_RECT.
red_square() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.RED_SQUARE.
serialize() Serialize a Sprixel into a dictionary.
white_rect() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.WHITE_RECT.
white_square() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.WHITE_SQUARE.
yellow_rect() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.YELLOW_RECT.
yellow_square() This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.YELLOW_SQUARE.

Attributes

bg_color A property to get/set the background color of the Sprixel.
fg_color A property to get/set the foreground color of the Sprixel.
length Return the true length of the model.
model A property to get/set the model of the Sprixel.
bg_color

A property to get/set the background color of the Sprixel.

Parameters:value (Color) – The new color

Example:

# Access the sprixel's color
sprix.bg_color
# Set the sprixel's color to some blue
sprix.bg_color = Color(0,128,255)
classmethod black_rect()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.BLACK_RECT. The difference is that BLACK_RECT is a string and this one is a Sprixel that can be manipulated more easily.

Example:

sprixel = Sprixel.black_rect()
classmethod black_square()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.BLACK_SQUARE. The difference is that BLACK_SQUARE is a string and this one is a Sprixel that can be manipulated more easily.

Example:

sprixel = Sprixel.black_square()
classmethod blue_rect()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.BLUE_RECT. The difference is that BLUE_RECT is a string and this one is a Sprixel that can be manipulated more easily.

Example:

sprixel = Sprixel.blue_rect()
classmethod blue_square()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.BLUE_SQUARE. The difference is that BLUE_SQUARE is a string and this one is a Sprixel that can be manipulated more easily.

Example:

sprixel = Sprixel.blue_square()
classmethod cyan_rect()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.CYAN_RECT. The difference is that CYAN_RECT is a string and this one is a Sprixel that can be manipulated more easily.

Example:

sprixel = Sprixel.cyan_rect()
classmethod cyan_square()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.CYAN_SQUARE. The difference is that CYAN_SQUARE is a string and this one is a Sprixel that can be manipulated more easily.

Example:

sprixel = Sprixel.cyan_square()
fg_color

A property to get/set the foreground color of the Sprixel.

Parameters:value (Color) – The new color

Example:

# Access the sprixel's color
sprix.fg_color
# Set the sprixel's color to some green
sprix.fg_color = Color(0,255,128)
static from_ansi(string, model='▄')

Takes an ANSI string, parse it and return a Sprixel.

Parameters:
  • string (str) – The ANSI string to parse.
  • model (str) – The character used to represent the sprixel in the ANSI sequence. Default is “▄”

Example:

new_sprixel = Sprixel.from_ansi(
    "\x1b[48;2;139;22;19m\x1b[38;2;160;26;23m▄\x1b[0m"
)

Warning

This has mainly be tested with ANSI string generated by climage.

classmethod green_rect()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.GREEN_RECT. The difference is that GREEN_RECT is a string and this one is a Sprixel that can be manipulated more easily.

Example:

sprixel = Sprixel.green_rect()
classmethod green_square()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.GREEN_SQUARE. The difference is that GREEN_SQUARE is a string and this one is a Sprixel that can be manipulated more easily.

Example:

sprixel = Sprixel.green_square()
length

Return the true length of the model.

With UTF8 and emojis the length of a string as returned by python’s len() function is often very wrong. For example, the len(“x1b[48;2;139;22;19mx1b[38;2;160;26;23m▄x1b[0m”) returns 39 when it should return 1.

This method returns the actual printing/display size of the sprixel’s model.

Note

This is a read only value. It is automatically updated when the model is changed.

Example:

if sprix.length > 2:
    print(
        f"Warning: that sprixel {sprix} will break the rest of the "
        "board's alignement"
        )
classmethod load(data)

Create a new Sprixel object based on serialized data.

Parameters:data (dict) – Data loaded from JSON data (deserialized).
Return type:Sprixel

Example:

new_sprite = Sprixel.load(json_parsed_data['default_sprixel'])
classmethod magenta_rect()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.MAGENTA_RECT. The difference is that MAGENTA_RECT is a string and this one is a Sprixel that can be manipulated more easily.

Example:

sprixel = Sprixel.magenta_rect()
classmethod magenta_square()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.MAGENTA_SQUARE. The difference is that MAGENTA_SQUARE is a string and this one is a Sprixel that can be manipulated more easily.

Example:

sprixel = Sprixel.magenta_square()
model

A property to get/set the model of the Sprixel.

Parameters:value (str) – The new model

Example:

# Get the sprixel's model
sprix.model
# Set the sprixel's model to "@"
sprix.model = "@"
classmethod red_rect()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.RED_RECT. The difference is that RED_RECT is a string and this one is a Sprixel that can be manipulated more easily.

Example:

sprixel = Sprixel.red_rect()
classmethod red_square()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.RED_SQUARE. The difference is that RED_SQUARE is a string and this one is a Sprixel that can be manipulated more easily.

Example:

sprixel = Sprixel.red_square()
serialize()

Serialize a Sprixel into a dictionary.

Returns:The class as a dictionary
Return type:dict

Example:

json.dump( sprixel.serialize() )
classmethod white_rect()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.WHITE_RECT. The difference is that WHITE_RECT is a string and this one is a Sprixel that can be manipulated more easily.

Example:

sprixel = Sprixel.white_rect()
classmethod white_square()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.WHITE_SQUARE. The difference is that WHITE_SQUARE is a string and this one is a Sprixel that can be manipulated more easily.

Example:

sprixel = Sprixel.white_square()
classmethod yellow_rect()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.YELLOW_RECT. The difference is that YELLOW_RECT is a string and this one is a Sprixel that can be manipulated more easily.

Note

Yellow is often rendered as brown.

Example:

sprixel = Sprixel.yellow_rect()
classmethod yellow_square()

This class method returns a sprixel that is the equivalent of pygamelib.assets.graphics.YELLOW_SQUARE. The difference is that YELLOW_SQUARE is a string and this one is a Sprixel that can be manipulated more easily.

Note

Yellow is often rendered as brown.

Example:

sprixel = Sprixel.yellow_square()