⚠️ Beta State

PyBevy is in an early and experimental stage. The API is incomplete, subject to breaking changes without notice, and you should expect bugs. Many features are still under development.

Text 2D

Render and animate text in 2D world space.

Introduction

Text can be rendered in 2D world space using Text2d. This example shows three text entities with different animations: translation, rotation, and scale.

import math
from pybevy.prelude import *
from pybevy.text import Text2d, TextFont, TextColor

Marker Components

Each text entity gets a marker component to control its animation type.

@component
class AnimateTranslation(Component):
    pass
 
 
@component
class AnimateRotation(Component):
    pass
 
 
@component
class AnimateScale(Component):
    pass

Setup

Spawn three text entities with different colors and animations.

def setup(commands: Commands) -> None:
    commands.spawn(Camera2d())
 
    commands.spawn(
        Text2d("Translation"),
        TextFont.from_font_size(50.0),
        TextColor(Color.srgb(1.0, 1.0, 0.0)),
        Transform.from_xyz(0.0, 200.0, 0.0),
        AnimateTranslation(),
    )
 
    commands.spawn(
        Text2d("Rotation"),
        TextFont.from_font_size(50.0),
        TextColor(Color.srgb(0.0, 1.0, 1.0)),
        AnimateRotation(),
    )
 
    commands.spawn(
        Text2d("Scale"),
        TextFont.from_font_size(50.0),
        TextColor(Color.srgb(1.0, 0.5, 1.0)),
        Transform.from_xyz(0.0, -200.0, 0.0),
        AnimateScale(),
    )

Animation Systems

Three systems animate translation, rotation, and scale independently using sine-wave patterns.

def animate_translation(query: Query[Mut[Transform], With[AnimateTranslation]], time: Res[Time]) -> None:
    for transform in query:
        transform.translation.x = 100.0 * math.sin(time.elapsed_secs())
 
 
def animate_rotation(query: Query[Mut[Transform], With[AnimateRotation]], time: Res[Time]) -> None:
    for transform in query:
        transform.rotation = Quat.from_rotation_z(time.elapsed_secs())
 
 
def animate_scale(query: Query[Mut[Transform], With[AnimateScale]], time: Res[Time]) -> None:
    for transform in query:
        scale = 1.0 + 0.5 * math.sin(time.elapsed_secs() * 2.0)
        transform.scale = Vec3.splat(scale)

Running the App

@entrypoint
def main(app: App) -> App:
    return (
        app
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .add_systems(Update, (animate_translation, animate_rotation, animate_scale))
    )
 
if __name__ == "__main__":
    main().run()

Running this example

Use PyBevy's hot reload feature to run and develop this example. If you don't have PyBevy installed, check out the Quick Start guide.

$pybevy watch text2d.py

The code will reload automatically when you make changes to the file.


From Python to Rust

Notice how the core concepts in the code—Commands, Assets, App, and Systems—are identical to the original Bevy example?

This is the power of pybevy! It lets you learn Bevy's powerful, data-driven architecture in friendly Python.

When your project grows and you're ready for maximum, native performance, you'll already know the concepts to start writing systems in Bevy Engine with Rust.