Menu
CoddyTech

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" % seconds

Change the number first, then show it. In the other order, the label shows the number from one second before.

challenge icon

Challenge

Easy

Make the clock in the top right count the seconds:

  1. In _ready(), connect $Clock.timeout to _on_clock_timeout.
  2. In _on_clock_timeout(), add 1 to seconds, then show Time: 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
quiz iconTest yourself

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