Actor > Text

Inhalt

Actor > Text#

API Reference#

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

Ein Text-Actor ist ein Actor, der einen Text enthält.

Sie müssen die Größe des Actors mit self.size() manuell einstellen, damit der vollständige Text sichtbar ist.

Parameter:
  • position – Oben-links Position des Textes.

  • text – Der ursprüngliche Text

Beispiele

Erstellen Sie neue Texte::

self.text = TextActor((1,1), "Hello World")
__init__(position=(0, 0), text='', **kwargs)[Quellcode]

Creates a text actor.

Parameter:
  • position – Top-left position of the text actor, e.g. (100, 50).

  • text – The initial text to display.

Beispiele

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)[Quellcode]

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

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

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

  • height – Maximum height the text should fit into.

Beispiele

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.

Beispiele

Sets the font size to 10:

text.font_size = 10
Rückgabe:

The current font size.

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

Creates a text actor whose origin is interpreted as the top-left corner.

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

  • text – Initial text to display.

Rückgabe:

A new Text actor.

get_costume_class()[Quellcode]

Returns the costume class used by Text actors.

Rückgabetyp:

type[text_costume.TextCostume]

Rückgabe:

The TextCostume class.

get_text()[Quellcode]

Ruft den aktuell angezeigten Text ab

Rückgabe:

Der aktuell angezeigte Text

get_text_width()[Quellcode]

Returns the width of the currently rendered text in pixels.

Rückgabe:

Width of the text in pixels.

Beispiele

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.

Rückgabe:

The current maximum width in pixels. 0 means no limit.

new_costume()[Quellcode]

Creates the text-specific costume used to draw the string.

Rückgabe:

A TextCostume instance for this actor.

on_shape_change()[Quellcode]

Updates the text layout after the actor shape has changed.

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

set_text(text)[Quellcode]

Sets the displayed text and redraws the actor.

Parameter:

text – The new text to show.

Beispiele

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.

Beispiele

score.value = "Score: " + str(points)