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 = trueA 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
Hard- In
pipe.gd, addsignal hit. Connectbody_enteredin_readyand emithitwhen a body enters. - In
flyer.gd, addsignal crashedand emit it whenposition.yis over 306. - In
main.gd, connect each pipe'shitand the flyer'scrashedto_on_crash. It showsGame Overand 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)
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