争分夺秒
本课属于 Coddy Godot 学习旅程的“游戏规则”单元。第 24 课,共 29 课。
倒计时到 0 时什么也没发生,还会继续变成 -1、-2。添加以下规则:
- 时间到达 0 时,玩家输掉游戏。显示
Time's up!并停止游戏。 - 玩家先到达旗帜时,就赢得游戏。显示
You win!并停止游戏。
使用 get_tree().paused = true 停止游戏。暂停会让时钟停止,因此时间会停留在结束时的数值。它也会让玩家停止,因此时间耗尽后就不会有人获胜。
if time_left == 0:
$HUD/Message.text = "Time's up!"
get_tree().paused = true挑战
简单让倒计时变成一场你可能会输的比赛。
- 在
_on_clock_timeout()中,在标签语句之后,当time_left为 0 时,将$HUD/Message.text设为"Time's up!",并暂停游戏。 - 在
_on_goal_body_entered(body)中,在显示"You win!"后暂停游戏。
按“开始”并等待:当显示 Time: 0 时,游戏会停止,并显示 Time's up! 然后点击“检查”。
亲手试试
extends Node2D
var time_left = 10
func _ready():
$HUD/Time.text = "Time: %d" % time_left
$Goal.body_entered.connect(_on_goal_body_entered)
$Clock.timeout.connect(_on_clock_timeout)
func _on_goal_body_entered(body):
if body.is_in_group("player"):
$HUD/Message.text = "You win!"
func _on_clock_timeout():
time_left -= 1
$HUD/Time.text = "Time: %d" % time_left
这节课包含一个小测验。开始学习即可作答,并记录你的进度。
游戏规则中的所有课
自己练习: Godot 在线编辑器