Coin Collector: You Win
Part of the Signals and Collisions section of Coddy's Godot journey. Lesson 27 of 27.
You win when no coins are left. _on_coin_collected already counts down, so check the count right after it changes:
if coins_left == 0:
print("You collected every coin!")The scene has a hidden Label, WinBanner, with the words You collected every coin! on it. Show it the same way you showed the lamps.
Challenge
MediumWin the game when every coin is taken.
- At the end of
_on_coin_collected(), add anifforcoins_leftequal to0. - Inside it, print
You collected every coin!and show$WinBanner.
Press Play and take all six coins: the win banner shows. Then Check.
Try it yourself
extends Node2D
var coins_left = 0
func _ready():
coins_left = get_tree().get_nodes_in_group("coins").size()
for coin in get_tree().get_nodes_in_group("coins"):
coin.collected.connect(_on_coin_collected)
func _on_coin_collected():
coins_left -= 1
print("Coins left: ", coins_left)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Signals and Collisions
6Project: Coin Collector
Coin Collector: WalkCoin Collector: CoinsPractice on your own: Godot playground