Menu
CoddyTech

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

The Lives label is on the right of the top bar.

challenge icon

Challenge

Medium
  1. Add var lives = 3 at the top, and show Lives: 3 on $HUD/Lives in _ready().
  2. Connect $Player.hurt to _on_player_hurt.
  3. 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
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