十秒捡金币、十秒躲避、十秒把箱子推到位。时间盒让范围锁死,适合练场景、信号和 UI。
局内状态
gdscript
extends Node2D
enum Phase { READY, PLAY, OVER }
var phase := Phase.READY
var score := 0
func _ready() -> void:
$Timer.timeout.connect(_on_timeup)
$UI/Start.pressed.connect(_start)
func _start() -> void:
phase = Phase.PLAY
score = 0
$Timer.start(10.0)
$UI/Start.hide()
func add_score(n: int) -> void:
if phase != Phase.PLAY:
return
score += n
$UI/Score.text = str(score)
func _on_timeup() -> void:
phase = Phase.OVER
Save.save_high(score)
$UI/Result.text = "分数 %d / 最高 %d" % [score, Save.high_score]
$UI/Start.text = "再来"
$UI/Start.show()金币的 collected 只在 PLAY 时加分,避免结算后还能刷分。
开局重置
再来一局不要只 reload_current_scene() 也行,但若用了 Autoload 存分,重载场景更干净:地图、金币、玩家都回到编辑器里的样子。
Timer 的 one_shot 勾上。autostart 关掉,等点开始。
一局十秒,你能在一晚上做完「移动 + 收集 + 分数 + 存档」。这比空的 RPG 框架更接近能发给别人玩的东西。
