Menu
CoddyTech

Dodge: Falling Blocks

Part of the Game Rules section of Coddy's Godot journey. Lesson 25 of 29.

Dodge puts this section together in one game. Blocks fall from the sky, and you move left and right to avoid them. The score counts how long you last. Three hits and the game is over.

The player already moves with the arrow keys. block.tscn already falls and breaks on the grass. You write main.gd, the rules, one step per lesson.

The scene has a SpawnTimer that runs out every second. Each time, spawn a block at a random x along the top:

var block = block_scene.instantiate()
block.position = Vector2(randf_range(40, 600), -30)
add_child(block)
challenge icon

Challenge

Easy

Make blocks fall from the sky.

  1. In _ready(), connect $SpawnTimer.timeout to _on_spawn_timer_timeout.
  2. In _on_spawn_timer_timeout(), spawn a block at a random x between 40 and 600, at y = -30.

Press Play: a block falls somewhere new every second. Then Check.

Try it yourself

extends Node2D

var block_scene = preload("res://block.tscn")

func _ready():
	pass

All lessons in Game Rules

Practice on your own: Godot playground