Crash Into Pipes
Part of the Projects section of Coddy's Godot journey. Lesson 26 of 36.
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
HardEnd the game on a crash.
- In
pipe.gd, addsignal hit, connectbody_enteredin_readyand emithitwhen a body enters. - In
flyer.gd, addsignal crashedand emit it whenposition.yis over306. - In
main.gd, connect each pipe'shitand the flyer'scrashedto_on_crash, which showsGame Overand pauses.
Press Play and fall: Game Over shows. Then Check.
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)
All lessons in Projects
3Project: Flappy Flyer
Flyer GravityFlap the FlyerPractice on your own: Godot playground