Menu
CoddyTech

Crash Into Pipes

Part of the Projects section of Coddy's Godot journey. Lesson 23 of 33.

Hitting a pipe or the ground should end the game. Both lead to one function in main.gd:

func _on_crash():
	$HUD/Message.text = "Game Over"
	get_tree().paused = true

A pipe. The pipe is an Area2D with two shapes, the top and bottom halves. The gap has no shape. In pipe.gd, connect body_entered and emit a hit signal. main.gd connects each new pipe's hit before adding it.

The ground. Its top is at y = 318. The flyer's edge is 12 pixels from its middle, so it touches the ground when position.y is over 306. Then flyer.gd emits crashed.

The flyer, pipes and timer are Pausable, so the pause freezes them all.

challenge icon

Challenge

Hard
  1. In pipe.gd, add signal hit. Connect body_entered in _ready and emit hit when a body enters.
  2. In flyer.gd, add signal crashed and emit it when position.y is over 306.
  3. In main.gd, connect each pipe's hit and the flyer's crashed to _on_crash. It shows Game Over and pauses the game.

Try it yourself

extends Node2D

const PIPE = preload("res://pipe.tscn")

func _ready():
	$PipeTimer.timeout.connect(_on_pipe_timer_timeout)

func _on_pipe_timer_timeout():
	var pipe = PIPE.instantiate()
	pipe.position = Vector2(700, randf_range(100, 250))
	$Pipes.add_child(pipe)
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