Learn GODOT with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
Godot Simple 2D Todo Game Prototype
# TodoItem.gd
extends Node2D
var task_name = ""
func _on_TodoItem_pressed():
print("Task completed: %s" % task_name)
# Attach this script to a Button or Node representing a task in a 2D scene.
Demonstrates a simple Godot 2D prototype where tasks are represented as nodes, using GDScript for logic and interaction.
Godot Basic Player Movement
# Player.gd
extends CharacterBody2D
@export var speed = 200
func _physics_process(delta):
var direction = Vector2.ZERO
if Input.is_action_pressed("ui_right"):
direction.x += 1
if Input.is_action_pressed("ui_left"):
direction.x -= 1
if Input.is_action_pressed("ui_down"):
direction.y += 1
if Input.is_action_pressed("ui_up"):
direction.y -= 1
velocity = direction.normalized() * speed
move_and_slide()
Implements simple player movement using input actions in GDScript for a 2D scene.
Godot Object Spawner
# Spawner.gd
extends Node2D
@export var object_scene: PackedScene
func _process(delta):
if Input.is_action_just_pressed("spawn_object"):
var obj = object_scene.instantiate()
add_child(obj)
obj.position = Vector2(randf() * 400, randf() * 400)
Spawns new objects dynamically in the scene using GDScript on key press.
Godot Collectible Coins
# Coin.gd
extends Area2D
signal coin_collected
func _on_body_entered(body):
if body.name == "Player":
emit_signal("coin_collected")
queue_free()
# Connect 'coin_collected' signal to update score in UI.
Implements collectible coins that increase a score counter when the player overlaps them.
Godot Simple Timer
# TimerController.gd
extends Node
var time_left = 10
func _ready():
$Timer.start()
func _on_Timer_timeout():
time_left -= 1
$Label.text = str(time_left)
if time_left <= 0:
$Timer.stop()
Shows a countdown timer that updates a label and stops when time reaches zero.
Godot Simple Door Interaction
# Door.gd
extends Node2D
var is_open = false
func toggle_door():
is_open = !is_open
$AnimationPlayer.play(is_open ? "open" : "close")
print(is_open ? "Door opened" : "Door closed")
Demonstrates a door that opens and closes when interacted with by the player.
Godot Simple Projectile
# Projectile.gd
extends Area2D
@export var speed = 400
func _process(delta):
position.x += speed * delta
func _on_body_entered(body):
queue_free()
Spawns and moves a projectile forward with a fixed velocity, destroying it on collision.
Godot Simple Enemy AI
# Enemy.gd
extends CharacterBody2D
@export var speed = 100
var player
func _ready():
player = get_parent().get_node("Player")
func _physics_process(delta):
if player:
var dir = (player.position - position).normalized()
velocity = dir * speed
move_and_slide()
Demonstrates a simple enemy that follows the player using position tracking.
Godot Simple HUD UI
# HUD.gd
extends CanvasLayer
var score = 0
func update_score(amount):
score += amount
$ScoreLabel.text = "Score: %d" % score
Displays score or health using UI elements with GDScript updates.
Godot Scene Switcher
# SceneSwitcher.gd
extends Node
func switch_scene(scene_path: String):
get_tree().change_scene_to_file(scene_path)
Switches between different scenes in a Godot project using GDScript.