pygamelib.base.Text

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

Bases: pygamelib.base.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 formating 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 (~pygamelib.gfx.core.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.
be_notified(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]) 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 into a display buffer (not a screen 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 bg_color attribute sets the foreground color.
length Return the true length of the text.
text The text attribute.
attach(observer)

Attach an observer to this instance. It means that until it is detached, it will be notified everytime 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)
be_notified(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 paramters 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. This can be None.
  • value (Any) – The new value of the attribute. This can be None.
bg_color

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

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|")
fg_color

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

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.

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.")
length

Return the true length of the text.

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.

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. Two important points:

  1. The “change” that occurred is not specified (but the notifying object is passed as parameter)
  2. No parameters are passed to the be_notified() method except the notifying object.
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 = None

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

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 into a display buffer (not a screen buffer).

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.
serialize()

Return a dictionary with all the attributes of this object.

Returns:A dictionary with all the attributes of this object.
Return type:dict
style = None

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

text

The text attribute. It needs to be a str.

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.