extends CanvasLayer ## HUD:分数、生命(图标)、提示。接 Main 的方法直接更新。 ## 命数图标用 ColorRect 排一排,比纯数字直观。 const LIFE_ICON_SIZE: Vector2 = Vector2(24, 8) const LIFE_ICON_SPACING: float = 4.0 const LIFE_ICON_COLOR: Color = Color(0.4, 0.6, 1.0) @onready var score_label: Label = $ScoreLabel @onready var lives_label: Label = $LivesLabel @onready var message_label: Label = $MessageLabel @onready var lives_container: HBoxContainer = $LivesContainer func _ready() -> void: show_message("按 空格 发球") func update_score(score: int) -> void: score_label.text = "Score: %d" % score func update_lives(lives: int) -> void: lives_label.text = "Lives: %d" % lives _refresh_life_icons(lives) func show_message(text: String) -> void: message_label.text = text func _refresh_life_icons(lives: int) -> void: # 清空旧图标 for child in lives_container.get_children(): child.queue_free() # 重新生成 lives 个 for i in range(lives): var icon := ColorRect.new() icon.custom_minimum_size = LIFE_ICON_SIZE icon.color = LIFE_ICON_COLOR lives_container.add_child(icon)