Dodge: Lives
Part of the Game Rules section of Coddy's Godot journey. Lesson 27 of 29.
Blocks now break on the player and nothing happens. Give the player three lives.
When a block lands on the player, it emits the player's hurt signal and breaks. The player flashes red. Connect $Player.hurt and take a life away each time:
func _on_player_hurt():
lives -= 1
$HUD/Lives.text = "Lives: %d" % livesThe Lives label is on the right of the top bar.
Challenge
Medium- Add
var lives = 3at the top, and showLives: 3on$HUD/Livesin_ready(). - Connect
$Player.hurtto_on_player_hurt. - In
_on_player_hurt(), take a life away and show it.
Try it yourself
extends Node2D
var block_scene = preload("res://block.tscn")
var score = 0
func _ready():
$HUD/Score.text = "Score: %d" % score
$SpawnTimer.timeout.connect(_on_spawn_timer_timeout)
$ScoreTimer.timeout.connect(_on_score_timer_timeout)
func _on_spawn_timer_timeout():
var block = block_scene.instantiate()
block.position = Vector2(randf_range(40, 600), -30)
add_child(block)
func _on_score_timer_timeout():
score += 1
$HUD/Score.text = "Score: %d" % score
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