Menu
CoddyTech

Random Gaps

Part of the Projects section of Coddy's Godot journey. Lesson 25 of 36.

Every gap is at the same height, so you could stay at one height and never crash. Put each gap somewhere new. A pipe's position.y is the middle of its gap:

pipe.position = Vector2(700, randf_range(100, 250))

From 100 to 250, the gap is never too close to the top or the ground, so every pipe can be passed.

challenge icon

Challenge

Easy

Put each gap at its own height.

  1. In main.gd, keep each new pipe at x = 700.
  2. Give it a random y between 100 and 250.

Press Play: the gaps come high and low. 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, 180)
	$Pipes.add_child(pipe)

All lessons in Projects

Practice on your own: Godot playground