Menu
CoddyTech

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 icon

Challenge

Medium
  1. In pipe.gd, add signal scored. Connect $Gap.body_entered and emit scored when a body enters the gap.
  2. In main.gd, add var score = 0 and show 0 in $HUD/Score at the start.
  3. Connect each pipe's scored to 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()
quiz iconTest yourself

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