Text

class pygamelib.base.Text(text='', fg_color=None, bg_color=None, style='', font=None)

Bases: PglBaseObject

An object to manipulate and display text in multiple contexts.

New in version 1.2.0.

The Text class is a collection of text formatting and display static methods.

You can either instantiate an object or use the static methods.

The Text object allow for easy text manipulation through its collection of independent attributes. They help to set the text, its style and the foreground and background colors.

The Text object can be converted to a Sprite through the Sprite.from_text() method. This is particularly useful to the place text on the game Board.

__init__(text='', fg_color=None, bg_color=None, style='', font=None)
Parameters:
  • text (str) – The text to manipulate

  • fg_color (Color) – The foreground color for the text.

  • bg_color (Color) – The background color for the text.

  • style (str) – The style for the text.

  • font (Font) – The font in which the text is going to be displayed (only works when using Screen.place() and Screen.update())

Methods

__init__([text, fg_color, bg_color, style, font])

param text:

The text to manipulate

attach(observer)

Attach an observer to this instance.

handle_notification(target[, attribute, value])

A virtual method that needs to be implemented by the observer.

black(message)

This method works exactly the way green_bright() work with different color.

black_bright(message)

This method works exactly the way green_bright() work with different color.

black_dim(message)

This method works exactly the way green_bright() work with different color.

blue(message)

This method works exactly the way green_bright() work with different color.

blue_bright(message)

This method works exactly the way green_bright() work with different color.

blue_dim(message)

This method works exactly the way green_bright() work with different color.

cyan(message)

This method works exactly the way green_bright() work with different color.

cyan_bright(message)

This method works exactly the way green_bright() work with different color.

cyan_dim(message)

This method works exactly the way green_bright() work with different color.

debug(message)

Print a debug message.

detach(observer)

Detach an observer from this instance.

fatal(message)

Print a fatal message.

green(message)

This method works exactly the way green_bright() work with different color.

green_bright(message)

Return a string formatted to be bright green

green_dim(message)

This method works exactly the way green_bright() work with different color.

info(message)

Print an informative message.

magenta(message)

This method works exactly the way green_bright() work with different color.

magenta_bright(message)

This method works exactly the way green_bright() work with different color.

magenta_dim(message)

This method works exactly the way green_bright() work with different color.

notify([modifier, attribute, value])

Notify all the observers that a change occurred.

print_white_on_red(message)

Print a white message over a red background.

red(message)

This method works exactly the way green_bright() work with different color.

red_bright(message)

This method works exactly the way green_bright() work with different color.

red_dim(message)

This method works exactly the way green_bright() work with different color.

render_to_buffer(buffer, row, column, ...)

Render the Text object from the display buffer to the frame buffer.

warn(message)

Print a warning message.

white(message)

This method works exactly the way green_bright() work with different color.

white_bright(message)

This method works exactly the way green_bright() work with different color.

white_dim(message)

This method works exactly the way green_bright() work with different color.

yellow(message)

This method works exactly the way green_bright() work with different color.

yellow_bright(message)

This method works exactly the way green_bright() work with different color.

yellow_dim(message)

This method works exactly the way green_bright() work with different color.

Attributes

bg_color

The bg_color attribute sets the background color.

fg_color

The fg_color attribute sets the foreground color.

length

Return the true length of the text.

screen_column

A property to get/set the screen column.

screen_row

A property to get/set the screen row.

text

The text attribute.

style

The style attribute sets the style of the text.

parent

This object's parent.

attach(observer)

Attach an observer to this instance. It means that until it is detached, it will be notified every time that a notification is issued (usually on changes).

An object cannot add itself to the list of observers (to avoid infinite recursions).

Parameters:

observer (PglBaseObject) – An observer to attach to this object.

Returns:

True or False depending on the success of the operation.

Return type:

bool

Example:

myboard = Board()
screen = Game.instance().screen
# screen will be notified of all changes in myboard
myboard.attach(screen)
property bg_color

The bg_color attribute sets the background color. It needs to be a Color.

New in version 1.3.0.

When the background color is changed, the observers are notified of the change with the pygamelib.base.Text.bg_color:changed event. The new color is passed as the value parameter.

static black(message)

This method works exactly the way green_bright() work with different color.

static black_bright(message)

This method works exactly the way green_bright() work with different color.

static black_dim(message)

This method works exactly the way green_bright() work with different color.

static blue(message)

This method works exactly the way green_bright() work with different color.

static blue_bright(message)

This method works exactly the way green_bright() work with different color.

static blue_dim(message)

This method works exactly the way green_bright() work with different color.

static cyan(message)

This method works exactly the way green_bright() work with different color.

static cyan_bright(message)

This method works exactly the way green_bright() work with different color.

static cyan_dim(message)

This method works exactly the way green_bright() work with different color.

static debug(message)

Print a debug message.

The debug message is a regular message prefixed by INFO in blue on a green background.

Parameters:

message (str) – The message to print.

Example:

base.Text.debug("This is probably going to success, eventually...")
detach(observer)

Detach an observer from this instance. If observer is not in the list this returns False.

Parameters:

observer (PglBaseObject) – An observer to detach from this object.

Returns:

True or False depending on the success of the operation.

Return type:

bool

Example:

# screen will no longer be notified of the changes in myboard.
myboard.detach(screen)
static fatal(message)

Print a fatal message.

The fatal message is a regular message prefixed by FATAL in white on a red background.

Parameters:

message (str) – The message to print.

Example:

base.Text.fatal("|x_x|")
property fg_color

The fg_color attribute sets the foreground color. It needs to be a Color.

New in version 1.3.0.

When the foreground color is changed, the observers are notified of the change with the pygamelib.base.Text.fg_color:changed event. The new color is passed as the value parameter.

static green(message)

This method works exactly the way green_bright() work with different color.

static green_bright(message)

Return a string formatted to be bright green

Parameters:

message (str) – The message to format.

Returns:

The formatted string

Return type:

str

Example:

print( Text.green_bright("This is a formatted message") )
static green_dim(message)

This method works exactly the way green_bright() work with different color.

handle_notification(target, attribute=None, value=None)

A virtual method that needs to be implemented by the observer. By default it does nothing but each observer needs to implement it if something needs to be done when notified.

This method always receive the notifying object as first parameter. The 2 other parameters are optional and can be None.

You can use the attribute and value as you see fit. You are free to consider attribute as an event and value as the event’s value.

Parameters:
  • subject (PglBaseObject) – The object that has changed.

  • attribute (str) – The attribute that has changed, it is usually a “FQDN style” string. This can be None.

  • value (Any) – The new value of the attribute. This can be None.

static info(message)

Print an informative message.

The info is a regular message prefixed by INFO in white on a blue background.

Parameters:

message (str) – The message to print.

Example:

base.Text.info("This is a very informative message.")
property length

Return the true length of the text.

New in version 1.3.0.

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 text.

Note

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

Example:

game.screen.place(my_text, 0, game.screen.width - my_text.length)
classmethod load(data: dict = None)

Load data and create a new Text object out of it.

New in version 1.3.0.

Parameters:

data (dict) – Data to create a new actuator (usually generated by serialize())

Returns:

A new Text object.

Return type:

Text

Example:

title = base.Text.load( previous_title.serialize() )
static magenta(message)

This method works exactly the way green_bright() work with different color.

static magenta_bright(message)

This method works exactly the way green_bright() work with different color.

static magenta_dim(message)

This method works exactly the way green_bright() work with different color.

notify(modifier=None, attribute: str = None, value: Any = None) None

Notify all the observers that a change occurred.

Parameters:
  • modifier (PglBaseObject) – An optional parameter that identify the modifier object to exclude it from the notified objects.

  • attribute (str) – An optional parameter that identify the attribute that has changed.

  • value (Any) – An optional parameter that identify the new value of the attribute.

Example:

# This example is silly, you would usually notify other objects from inside
# an object that changes a value that's important for the observers.
color = Color(255,200,125)
color.attach(some_text_object)
color.notify()
parent

This object’s parent. It needs to be a BoardItem.

print_formatted()

Print the text with the current font activated.

New in version 1.3.0.

If the font is not set, it is strictly equivalent to use Python’s print(text_object).

static print_white_on_red(message)

Print a white message over a red background.

Parameters:

message (str) – The message to print.

Example:

base.Text.print_white_on_red("This is bright!")
static red(message)

This method works exactly the way green_bright() work with different color.

static red_bright(message)

This method works exactly the way green_bright() work with different color.

static red_dim(message)

This method works exactly the way green_bright() work with different color.

render_to_buffer(buffer, row, column, buffer_height, buffer_width)

Render the Text object from the display buffer to the frame buffer.

New in version 1.3.0.

This method is automatically called by pygamelib.engine.Screen.render().

Parameters:
  • buffer (numpy.array) – A screen buffer to render the item into.

  • row (int) – The row to render in.

  • column (int) – The column to render in.

  • height (int) – The total height of the display buffer.

  • width (int) – The total width of the display buffer.

property screen_column: int

A property to get/set the screen column.

Parameters:

value (int) – the screen column

Return type:

int

property screen_row: int

A property to get/set the screen row.

Parameters:

value (int) – the screen row

Return type:

int

serialize()

Return a dictionary with all the attributes of this object.

New in version 1.3.0.

Returns:

A dictionary with all the attributes of this object.

Return type:

dict

store_screen_position(row: int, column: int) bool

Store the screen position of the object.

This method is automatically called by Screen.place().

Parameters:
  • row (int) – The row (or y) coordinate.

  • column (int) – The column (or x) coordinate.

Example:

an_object.store_screen_coordinate(3,8)
style

The style attribute sets the style of the text. It needs to be a str.

property text

The text attribute. It needs to be a str.

New in version 1.3.0.

When the text is changed, the observers are notified of the change with the pygamelib.base.Text.text:changed event. The new text is passed as the value parameter.

static warn(message)

Print a warning message.

The warning is a regular message prefixed by WARNING in black on a yellow background.

Parameters:

message (str) – The message to print.

Example:

base.Text.warn("This is a warning.")
static white(message)

This method works exactly the way green_bright() work with different color.

static white_bright(message)

This method works exactly the way green_bright() work with different color.

static white_dim(message)

This method works exactly the way green_bright() work with different color.

static yellow(message)

This method works exactly the way green_bright() work with different color.

static yellow_bright(message)

This method works exactly the way green_bright() work with different color.

static yellow_dim(message)

This method works exactly the way green_bright() work with different color.