Score and Victory
Part of the Projects section of Coddy's Godot journey. Lesson 17 of 33.
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 = trueCount 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
HardIn main.gd:
- Add
var score = 0and showScore: 0at the start. - A broken big rock adds 20 and a small one 50. Show the new score.
- Add
var rocks_left = 4and count it as shown. At zero, showYou Win!and pause the game.
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)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Projects
2Project: Asteroid Shooter
Turn the ShipFire the EnginePractice on your own: Godot playground