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
EasyMake the countdown a race you can lose.
- In
_on_clock_timeout(), after the label line, whentime_leftis 0, set$HUD/Message.textto"Time's up!"and pause the game. - In
_on_goal_body_entered(body), pause the game after"You win!".
Press Play and wait: at Time: 0 the game stops with Time's up! Then Check.
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