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
EasyMake blocks fall from the sky:
- In
_ready(), connect$SpawnTimer.timeoutto_on_spawn_timer_timeout. - In
_on_spawn_timer_timeout(), spawn a block at a random x between 40 and 600, at y = -30.
Try it yourself
extends Node2D
var block_scene = preload("res://block.tscn")
func _ready():
pass
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Game Rules
Practice on your own: Godot playground