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" % scoreOne 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
Medium- Add
var score = 0at the top, and showScore: 0on$HUD/Scorein_ready(). - Connect
$ScoreTimer.timeoutto_on_score_timer_timeout. - 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)
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