Actor > Text

Contents

Actor > Text#

API Reference#

class miniworlds.actors.texts.text.Text(position=(0, 0), text='', **kwargs)[source]

Actor that displays text.

Set size, font_size, or max_width when the complete text should fit into a fixed area.

Parameters:
  • position – Top-left position of the text.

  • text – Initial text.

Examples

score_text = Text((10, 10), "Score: 0")
score_text.font_size = 24
__init__(position=(0, 0), text='', **kwargs)[source]

Create a text actor.

Parameters:
  • position – Top-left position as (x, y).

  • text – Initial text.

Examples

score_text = Text((10, 10), "Score: 0")

@world.register
def act(self):
    score_text.text = "Score: " + str(player.score)
font_by_size(width=None, height=None)[source]

Choose a font size that fits into a target width or height.

This is useful when the text should stay inside a fixed box.

Parameters:
  • width – Maximum width the text should fit into.

  • height – Maximum height the text should fit into.

Examples

headline = Text((20, 20), "Miniworlds")
headline.font_by_size(width=200)
property font_size

Font size in pixels.

Examples

text.font_size = 18
Type:

float

classmethod from_topleft(position=(0, 0), text='', **kwargs)[source]

Create a text actor positioned by its top-left corner.

Parameters:
  • position – Top-left position of the text actor.

  • text – Initial text to display.

Returns:

A new Text actor.

Examples

label = Text.from_topleft((10, 10), "Ready")
get_costume_class()[source]

Return the costume class used by Text actors.

Return type:

type[text_costume.TextCostume]

Returns:

The TextCostume class.

get_text()[source]

Return the displayed text.

Returns:

Current text.

get_text_width()[source]

Return the rendered text width.

Returns:

Width of the text in pixels.

Examples

if title.get_text_width() > 200:
    title.font_size = 18
property max_width

Maximum width used for text rendering and wrapping.

A value of 0 means no limit.

Examples

text.max_width = 200
Type:

float

new_costume()[source]

Create the text-specific costume.

Returns:

A TextCostume instance for this actor.

on_shape_change()[source]

Updates the text layout after the actor shape has changed.

This hook is called internally when size-related properties change.

set_text(text)[source]

Set the displayed text and redraw the actor.

Parameters:

text – The new text to show.

Examples

message.set_text("Press space to start")
property text

Displayed text string.

Examples

if player.lives == 0:
    label.text = "Game Over"
Type:

str

property value

Alias for text.

This property is useful when a text actor should behave like a simple value display, for example a score or timer.

Examples

score.value = "Score: " + str(player.score)
Type:

str