Score a Pipe
Part of the Projects section of Coddy's Godot journey. Lesson 24 of 33.
Each pipe you pass is a point. The pipe has a child Area2D called Gap, a thin strip behind the opening. The flyer touches it only after it gets through, so that is when you score.
In pipe.gd, connect the child's signal through $Gap and emit scored:
$Gap.body_entered.connect(_on_gap_body_entered)
func _on_gap_body_entered(body):
scored.emit()In main.gd, connect each new pipe's scored. Add one to score and show it in $HUD/Score. str(score) turns the number into text. Show 0 at the start.
Challenge
Medium- In
pipe.gd, addsignal scored. Connect$Gap.body_enteredand emitscoredwhen a body enters the gap. - In
main.gd, addvar score = 0and show0in$HUD/Scoreat the start. - Connect each pipe's
scoredto a function that adds one and shows the new score.
Try it yourself
extends Area2D
signal hit
const SPEED = 160
func _ready():
body_entered.connect(_on_body_entered)
func _physics_process(delta):
position.x -= SPEED * delta
if position.x < -60:
queue_free()
func _on_body_entered(body):
hit.emit()
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Projects
Practice on your own: Godot playground