Score a Pipe
Part of the Projects section of Coddy's Godot journey. Lesson 27 of 36.
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
MediumScore a point for every pipe you pass.
- 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 score.
Press Play and fly through a gap: the score shows 1. Then Check.
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()
All lessons in Projects
3Project: Flappy Flyer
Flyer GravityFlap the FlyerPractice on your own: Godot playground