Menu
CoddyTech

Dodge: Play Again

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

The last step lets the player press E after Game Over to play again.

You did this in the spike room. Set Main's process_mode to Node.PROCESS_MODE_ALWAYS, so its _process runs while the game is paused. The player, both timers and every block are set to Pausable in their scenes, so they still stop. On the key, unpause and reload:

func _process(delta):
	if lives == 0 and Input.is_action_just_pressed("action"):
		get_tree().paused = false
		get_tree().reload_current_scene()

The reload clears every block, the score and the lives. Also show the Hint label at Game Over, so the player knows which key to press.

challenge icon

Challenge

Hard
  1. In _ready(), set this node's process_mode to Node.PROCESS_MODE_ALWAYS.
  2. At Game Over, also show $HUD/Hint.
  3. Add _process(). When lives is 0 and action was just pressed, unpause the game and reload the scene.

Try it yourself

extends Node2D

var block_scene = preload("res://block.tscn")
var score = 0
var lives = 3

func _ready():
	$HUD/Score.text = "Score: %d" % score
	$HUD/Lives.text = "Lives: %d" % lives
	$SpawnTimer.timeout.connect(_on_spawn_timer_timeout)
	$ScoreTimer.timeout.connect(_on_score_timer_timeout)
	$Player.hurt.connect(_on_player_hurt)

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

func _on_player_hurt():
	lives -= 1
	$HUD/Lives.text = "Lives: %d" % lives
	if lives == 0:
		$HUD/GameOver.visible = true
		get_tree().paused = true
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