Learn Defold - 10 Code Examples & CST Typing Practice Test
Defold is a free, cross-platform 2D game engine developed by King, designed for rapid development of high-performance 2D games. It features a lightweight editor, Lua scripting, a component-based architecture, and efficient deployment across multiple platforms.
View all 10 Defold code examples →
Learn DEFOLD with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
Defold Lua Script Example - Simple Player Movement
-- player.script
function init(self)
self.speed = 200
end
function update(self, dt)
local move = vmath.vector3()
if go.get_action("left").pressed then
move.x = move.x - 1
end
if go.get_action("right").pressed then
move.x = move.x + 1
end
go.set_position(go.get_position() + move * self.speed * dt)
end
A basic Defold script in Lua to move a player left and right using arrow keys.
Defold Lua Script Example - Player Jump
-- player_jump.script
function init(self)
self.speed = 200
self.vspeed = 0
self.gravity = 500
end
function update(self, dt)
local move = vmath.vector3()
if go.get_action("left").pressed then move.x = move.x - 1 end
if go.get_action("right").pressed then move.x = move.x + 1 end
if go.get_action("jump").pressed and self.vspeed == 0 then self.vspeed = -300 end
self.vspeed = self.vspeed + self.gravity * dt
move.y = self.vspeed * dt
go.set_position(go.get_position() + move * self.speed * dt)
end
Adds jumping to the player with simple gravity.
Defold Lua Script Example - Simple Shooting
-- player_shoot.script
function init(self)
self.speed = 200
end
function update(self, dt)
if go.get_action("shoot").pressed then
factory.create("#bullet_factory", go.get_position())
end
end
Player shoots a bullet when pressing space.
Defold Lua Script Example - Enemy Follow Player
-- enemy.script
function update(self, dt)
local player_pos = go.get_position("/player")
local pos = go.get_position()
local dir = vmath.normalize(player_pos - pos)
go.set_position(pos + dir * 100 * dt)
end
Enemy moves towards the player.
Defold Lua Script Example - Timer Example
-- timer.script
function init(self)
self.timer = 0
end
function update(self, dt)
self.timer = self.timer + dt
if self.timer >= 1 then
print("Timer triggered")
self.timer = 0
end
end
Runs a repeating timer action.
Defold Lua Script Example - Mouse Click Detection
-- mouse.script
function update(self, dt)
if mouse.is_button_pressed(mouse.BUTTON_LEFT) then
print("Left click")
end
if mouse.is_button_pressed(mouse.BUTTON_RIGHT) then
print("Right click")
end
end
Detects mouse clicks in the game window.
Defold Lua Script Example - Display FPS
-- fps.script
function update(self, dt)
local fps = 1 / dt
print(string.format("FPS: %.2f", fps))
end
Prints FPS each frame.
Defold Lua Script Example - Random Movement
-- random_move.script
function update(self, dt)
local pos = go.get_position()
pos.x = pos.x + math.random(-1,1) * 50 * dt
pos.y = pos.y + math.random(-1,1) * 50 * dt
go.set_position(pos)
end
Moves an object randomly each frame.
Defold Lua Script Example - Simple Animation
-- animation.script
function update(self, dt)
self.time = (self.time or 0) + dt
if self.time >= 0.1 then
sprite.play_flipbook(go.get_id(), hash("anim"))
self.time = 0
end
end
Cycles sprite animation frames automatically.
Defold Lua Script Example - Collision Detection
-- collision.script
function on_message(self, message_id, message, sender)
if message_id == hash("contact_point_response") then
print("Collision detected with", sender)
end
end
Checks collision with another game object.
Frequently Asked Questions about Defold
What is Defold?
Defold is a free, cross-platform 2D game engine developed by King, designed for rapid development of high-performance 2D games. It features a lightweight editor, Lua scripting, a component-based architecture, and efficient deployment across multiple platforms.
What are the primary use cases for Defold?
2D mobile games. Rapid prototyping and game jams. Educational games. Casual and puzzle games. Performance-sensitive 2D games
What are the strengths of Defold?
Lightweight and fast. Easy cross-platform deployment. Free and open-source. Good performance on mobile devices. Component-based architecture for modular design
What are the limitations of Defold?
Primarily 2D-focused, limited 3D support. Smaller community than Unity or Godot. Less asset marketplace support. Requires familiarity with Lua scripting. GUI system is basic compared to full-feature engines
How can I practice Defold typing speed?
CodeSpeedTest offers 10+ real Defold code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.