Beat the Clock
Part of the Game Rules section of Coddy's Godot journey. Lesson 24 of 29.
The countdown does nothing at zero and keeps going to -1, -2. Add the rules:
- When the time reaches 0, the player loses. Show
Time's up!and stop the game. - When the player reaches the flag first, they win. Show
You win!and stop the game.
Stop the game with get_tree().paused = true. The pause stops the clock, so the time stays where it ended. It also stops the player, so nobody wins after the time is up.
if time_left == 0:
$HUD/Message.text = "Time's up!"
get_tree().paused = trueChallenge
Easy- In
_on_clock_timeout(), after the label line, check iftime_leftis 0. If so, showTime's up!on$HUD/Messageand pause the game. - In
_on_goal_body_entered(body), pause the game afterYou win!.
Try it yourself
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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Game Rules
Practice on your own: Godot playground