Menu
CoddyTech

Dodge: Score

Part of the Game Rules section of Coddy's Godot journey. Lesson 26 of 29.

In Dodge, you score one point for every second you survive. The scene has a second timer, ScoreTimer, that runs out every second. The top bar has a Score label.

This works like the clock you built earlier. Use a variable, show it in _ready(), and add a point on every timeout:

func _on_score_timer_timeout():
	score += 1
	$HUD/Score.text = "Score: %d" % score

One scene can have many timers. Each has its own wait_time and its own function. So you can change how often blocks fall without changing the score.

challenge icon

Challenge

Medium
  1. Add var score = 0 at the top, and show Score: 0 on $HUD/Score in _ready().
  2. Connect $ScoreTimer.timeout to _on_score_timer_timeout.
  3. In _on_score_timer_timeout(), add a point and show the new score.

Try it yourself

extends Node2D

var block_scene = preload("res://block.tscn")

func _ready():
	$SpawnTimer.timeout.connect(_on_spawn_timer_timeout)

func _on_spawn_timer_timeout():
	var block = block_scene.instantiate()
	block.position = Vector2(randf_range(40, 600), -30)
	add_child(block)
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