pyxen.Text

Renders a text string on an Entity using a bitmap font.

Text entities use a font atlas for rendering — each character is drawn as a quad from the atlas texture.


Constructor

Text(
    content,
    *,
    font="_default",
    color=None,
    align="left"
)

Parameters

ParameterTypeDescription
contentstrThe text string to display
fontstrName of the font asset (default: built-in font)
color(r, g, b, a)Tint color (default: white)
alignstrText alignment: "left", "center", or "right"

content (Required)

The text string to render.

Text("Hello World")

font (Optional)

Name of the font asset to use. If omitted, the built-in default font is used.

Text("Score: 0", font="myfont")

A font asset consists of a .font.json descriptor and a .font.png atlas image.


color (Optional)

Tints the rendered text.

Text("Warning!", color=(1, 0, 0, 1))

Default: (1.0, 1.0, 1.0, 1.0) (white, fully opaque).


align (Optional)

Controls horizontal text alignment relative to the entity position.

Text("Centered", align="center")
ValueDescription
"left"Text starts at the entity position (default)
"center"Text is centered on the entity position
"right"Text ends at the entity position

Assigning Text

You can assign during spawn:

label = world.spawn(
    text=Text("Score: 0"),
    x=10, y=10, layer=200
)

Or later:

label.text = Text("Game Over", color=(1, 0, 0, 1))

Updating Text at Runtime

Access the text component through the entity to update properties:

label.text.content = "Score: 100"
label.text.color = (1, 1, 0, 1)
label.text.align = "center"

Fading and Tinting

A text entity’s alpha, r, g and b tween like a sprite’s (see entity.tween()), so a score pop fades without setting text.color every frame:

label.tween("alpha", to=0, duration=0.4, delay=0.3)  # a score pop fades, after a moment
label.tween("b", to=0, duration=0.2)                 # white turns yellow

Removing Text

Set the text to None to remove it:

label.text = None

On the Screen or in the World

A text entity sits in the world, like a sprite: the camera moves it and the camera’s zoom scales it. For a HUD that stays on the screen (a score, lives, a message), spawn the text as a child of the camera: its x and y are then measured from the center of the screen, and it stays put while the camera moves.

cam = world.spawn(camera=Camera())
label = world.spawn(parent=cam, x=-150, y=-80, layer=200, text=Text("Score: 0"))

Example

The example keeps the score in a component on the label, not in a module variable.

from pyxen import *

def start():
    cam = world.spawn(camera=Camera())
    label = world.spawn(
        parent=cam, x=-150, y=-80, layer=200,
        text=Text("Score: 0", color=(1, 1, 1, 1))
    )
    label.score = {"value": 0}

def update():
    for label in world.all("score"):
        label.score.value += 1
        label.text.content = "Score: " + str(label.score.value)

Snippets

In Pyxen’s home, under Snippets:

  • Text() — Render text using Pyxen’s built-in font.
  • Text(align=…) — Left, center, or right alignment.
  • text.content — Update what the entity shows each frame.