Menu
CoddyTech

Score and Victory

Part of the Projects section of Coddy's Godot journey. Lesson 20 of 36.

A big rock is worth 20 points and a small one 50. _on_rock_broken knows the size, so it adds the points and updates $HUD/Score.

You win when no rocks are left. Start the count at 4. Take one off for each broken rock, and add two when a big one splits:

if rock.size == 2:
	rocks_left += 2
rocks_left -= 1
if rocks_left == 0:
	$HUD/Message.text = "You win!"
	get_tree().paused = true

Count the two new rocks here, not in spawn_rock. spawn_rock runs at the end of the frame, so the count would reach zero before the last two small rocks exist.

The rocks, bullets and ship are Pausable. Main is Always.

challenge icon

Challenge

Hard

Score the rocks and win when they are gone.

  1. In main.gd, add var score = 0 and show Score: 0.
  2. A broken big rock adds 20, a small one 50. Show the score.
  3. Add var rocks_left = 4. Take one off per broken rock, add two when a big one splits. At 0, show You win! and pause.

Press Play and clear the rocks: You win! shows. Then Check.

Try it yourself

extends Node2D

const ROCK = preload("res://rock.tscn")

func _ready():
	for i in 4:
		spawn_rock(Vector2(randf_range(0, 640), 0), Vector2(randf_range(-90, 90), randf_range(40, 90)), 2)

func spawn_rock(where, velocity, size):
	var rock = ROCK.instantiate()
	rock.position = where
	rock.velocity = velocity
	rock.size = size
	rock.broken.connect(_on_rock_broken)
	add_child(rock)

func _on_rock_broken(rock):
	if rock.size == 2:
		spawn_rock.call_deferred(rock.position, rock.velocity.rotated(0.8) * 1.5, 1)
		spawn_rock.call_deferred(rock.position, rock.velocity.rotated(-0.8) * 1.5, 1)

All lessons in Projects

Practice on your own: Godot playground