Actor > Text#
API Reference#
- class miniworlds.actors.texts.text.Text(position=(0, 0), text='', **kwargs)[source]
A Text-Actor is a actor which contains a Text.
You have to set the size of the actor with self.size() manually so that the complete text can be seen.
- Parameters:
position – Top-Left position of Text.
text – The initial text
Examples
Create a new texts:
self.text = TextActor((1,1), "Hello World")
- __init__(position=(0, 0), text='', **kwargs)[source]
Creates a text actor.
- Parameters:
position – Top-left position of the text actor, e.g.
(100, 50).text – The initial text to display.
Examples
Show the current score in the top-left corner:
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]
Chooses 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
Fit a headline into a 200 pixel wide box:
headline = Text((20, 20), "Miniworlds") headline.font_by_size(width=200)
- property font_size
Gets or sets the font size in pixels.
Examples
Sets the font size to
10:text.font_size = 10
- Returns:
The current font size.
- classmethod from_topleft(position=(0, 0), text='', **kwargs)[source]
Creates a text actor whose origin is interpreted as the top-left corner.
- Parameters:
position – Top-left position of the text actor.
text – Initial text to display.
- Returns:
A new
Textactor.
- get_costume_class()[source]
Returns the costume class used by
Textactors.- Return type:
type[text_costume.TextCostume]- Returns:
The
TextCostumeclass.
- get_text()[source]
Gets the currently displayed text
- Returns:
The currently displayed text
- get_text_width()[source]
Returns the width of the currently rendered text in pixels.
- 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 logic.
Set this value when the text should stay within a fixed width.
- Returns:
The current maximum width in pixels.
0means no limit.
- new_costume()[source]
Creates the text-specific costume used to draw the string.
- Returns:
A
TextCostumeinstance 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]
Sets the displayed text and redraws the actor.
- Parameters:
text – The new text to show.
Examples
message.set_text("Press space to start")
- property text
The displayed text string.
Set this property to update what is shown:
label.text = "Game Over"
- 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(points)