Learn LOVE2D with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
1
Love2D Lua Example - Simple Counter
-- main.lua
local count = 0
function love.load()
font = love.graphics.newFont(40)
end
function love.update(dt)
end
function love.draw()
love.graphics.setFont(font)
love.graphics.print("Count: " .. count, 100, 100)
love.graphics.print("Press UP to +, DOWN to -", 100, 200)
end
function love.keypressed(key)
if key == "up" then
count = count + 1
elseif key == "down" then
count = count - 1
end
end
A simple Love2D Lua script that increments and decrements a counter displayed on screen.
2
Love2D Lua Example - Moving Rectangle
-- main.lua
local x, y = 100, 100
local speed = 200
function love.update(dt)
if love.keyboard.isDown("up") then y = y - speed * dt end
if love.keyboard.isDown("down") then y = y + speed * dt end
if love.keyboard.isDown("left") then x = x - speed * dt end
if love.keyboard.isDown("right") then x = x + speed * dt end
end
function love.draw()
love.graphics.rectangle("fill", x, y, 50, 50)
end
Move a rectangle using arrow keys.
3
Love2D Lua Example - Circle Bounce
-- main.lua
local x, y = 100, 100
local dx, dy = 200, 150
local radius = 20
function love.update(dt)
x = x + dx * dt
y = y + dy * dt
if x - radius < 0 or x + radius > love.graphics.getWidth() then dx = -dx end
if y - radius < 0 or y + radius > love.graphics.getHeight() then dy = -dy end
end
function love.draw()
love.graphics.circle("fill", x, y, radius)
end
A bouncing circle within the window boundaries.
4
Love2D Lua Example - Keyboard Controlled Player
-- main.lua
local player = {x=200, y=200, speed=150}
function love.update(dt)
if love.keyboard.isDown("w") then player.y = player.y - player.speed * dt end
if love.keyboard.isDown("s") then player.y = player.y + player.speed * dt end
if love.keyboard.isDown("a") then player.x = player.x - player.speed * dt end
if love.keyboard.isDown("d") then player.x = player.x + player.speed * dt end
end
function love.draw()
love.graphics.rectangle("fill", player.x, player.y, 40, 40)
end
Player controlled with WASD keys.
5
Love2D Lua Example - Timer Countdown
-- main.lua
local timer = 10
function love.update(dt)
timer = timer - dt
if timer < 0 then timer = 0 end
end
function love.draw()
love.graphics.print("Time left: " .. math.floor(timer), 100, 100)
end
Countdown timer displayed on screen.
6
Love2D Lua Example - Player Jump
-- main.lua
local player = {x=200, y=300, vy=0, onGround=true}
local gravity = 500
function love.update(dt)
player.vy = player.vy + gravity * dt
player.y = player.y + player.vy * dt
if player.y >= 300 then player.y = 300; player.vy = 0; player.onGround = true end
end
function love.keypressed(key)
if key == "space" and player.onGround then
player.vy = -300
player.onGround = false
end
end
function love.draw()
love.graphics.rectangle("fill", player.x, player.y, 50, 50)
end
Simple jump mechanics with gravity.
7
Love2D Lua Example - Moving Background
-- main.lua
local bgX = 0
local speed = 100
function love.update(dt)
bgX = bgX - speed * dt
if bgX <= -800 then bgX = 0 end
end
function love.draw()
love.graphics.rectangle("fill", bgX, 0, 800, 600)
love.graphics.rectangle("fill", bgX + 800, 0, 800, 600)
end
Scrolling background effect.
8
Love2D Lua Example - Simple Enemy AI
-- main.lua
local player = {x=400, y=300}
local enemy = {x=100, y=100, speed=100}
function love.update(dt)
local dx = player.x - enemy.x
local dy = player.y - enemy.y
local dist = math.sqrt(dx*dx + dy*dy)
if dist > 0 then
enemy.x = enemy.x + dx/dist * enemy.speed * dt
enemy.y = enemy.y + dy/dist * enemy.speed * dt
end
end
function love.draw()
love.graphics.rectangle("fill", player.x, player.y, 50, 50)
love.graphics.rectangle("fill", enemy.x, enemy.y, 50, 50)
end
Enemy moves towards player automatically.
9
Love2D Lua Example - Collectibles
-- main.lua
local player = {x=200, y=200}
local items = {{x=400, y=200}, {x=500, y=300}}
local score = 0
function love.update(dt)
for i=#items,1,-1 do
local item = items[i]
if math.abs(player.x - item.x) < 25 and math.abs(player.y - item.y) < 25 then
table.remove(items, i)
score = score + 10
end
end
end
function love.draw()
love.graphics.rectangle("fill", player.x, player.y, 50, 50)
for _, item in ipairs(items) do
love.graphics.circle("fill", item.x, item.y, 20)
end
love.graphics.print("Score: " .. score, 100, 50)
end
Player collects items to increase score.
10
Love2D Lua Example - Particle System
-- main.lua
local ps
function love.load()
ps = love.graphics.newParticleSystem(love.graphics.newImage("particle.png"), 100)
ps:setParticleLifetime(1, 2)
ps:setEmissionRate(50)
ps:setSpeed(100, 200)
end
function love.update(dt)
ps:update(dt)
end
function love.draw()
love.graphics.draw(ps, 400, 300)
end
Simple particle system example.