pyxen.Text
在 Entity 上使用位图字体渲染文本字符串。
Text 实体使用字体图集进行渲染——每个字符都从图集纹理中绘制为一个四边形。
构造函数
Text(
content,
*,
font="_default",
color=None,
align="left"
)
参数
| 参数 | 类型 | 描述 |
|---|---|---|
content | str | 要显示的文本字符串 |
font | str | 字体资源名称(默认:内置字体) |
color | (r, g, b, a) | 着色颜色(默认:白色) |
align | str | 文本对齐方式:"left"、"center" 或 "right" |
content(必填)
要渲染的文本字符串。
Text("Hello World")
font(可选)
要使用的字体资源名称。如果省略,则使用内置默认字体。
Text("Score: 0", font="myfont")
字体资源由一个 .font.json 描述文件和一个 .font.png 图集图像组成。
color(可选)
为渲染的文本着色。
Text("Warning!", color=(1, 0, 0, 1))
默认值:(1.0, 1.0, 1.0, 1.0)(白色,完全不透明)。
align(可选)
控制文本相对于实体位置的水平对齐方式。
Text("Centered", align="center")
| 值 | 描述 |
|---|---|
"left" | 文本从实体位置开始(默认) |
"center" | 文本以实体位置为中心 |
"right" | 文本在实体位置处结束 |
赋值 Text
你可以在生成时赋值:
label = world.spawn(
text=Text("Score: 0"),
x=10, y=10, layer=200
)
或者之后赋值:
label.text = Text("Game Over", color=(1, 0, 0, 1))
运行时更新 Text
通过实体访问 text 组件来更新属性:
label.text.content = "Score: 100"
label.text.color = (1, 1, 0, 1)
label.text.align = "center"
移除 Text
将 text 设置为 None 即可移除:
label.text = None
示例
from pyxen import *
score = 0
label = world.spawn(
text=Text("Score: 0", color=(1, 1, 1, 1)),
x=10, y=10, layer=200
)
def update():
global score
score += 1
label.text.content = f"Score: {score}"