A Clock on Screen
Part of the Game Rules section of Coddy's Godot journey. Lesson 10 of 29.
A timer and a label make a clock. This room has a Timer called Clock that runs out every second. It also has a label, HUD/Time, in the top right corner.
Each time the clock runs out, add a second and show it:
func _on_clock_timeout():
seconds += 1
$HUD/Time.text = "Time: %d" % secondsChange the number first, then show it. In the other order, the label shows the number from one second before.
Challenge
EasyMake the clock in the top right count the seconds:
- In
_ready(), connect$Clock.timeoutto_on_clock_timeout. - In
_on_clock_timeout(), add 1 toseconds, then showTime:and the seconds on$HUD/Time.
Try it yourself
extends Node2D
var score = 0
var total = 0
var seconds = 0
func _ready():
total = get_tree().get_nodes_in_group("coins").size()
$HUD/Score.text = "Coins: %d of %d" % [score, total]
$HUD/Time.text = "Time: %d" % seconds
$Player.picked_up.connect(_on_player_picked_up)
func _on_player_picked_up():
score += 1
$HUD/Score.text = "Coins: %d of %d" % [score, total]
func _on_clock_timeout():
pass
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Game Rules
2Score on Screen
A Label on ScreenNumbers into TextFormat with PercentUpdate the LabelA Clock on ScreenPractice on your own: Godot playground