Every Love2D game is the same skeleton. Learn it once, reuse forever:
function love.load() -- runs once: load images, set up state
end
function love.update(dt) -- runs 60 times per second: move things, check rules
end
function love.draw() -- runs 60 times per second: draw everything
endConfig
conf.lua sits next to main.lua:
function love.conf(t)
t.title = "My First Game"
t.console = true -- enable print to console
t.window.width = 800
t.window.height = 600
endDrawing
Origin (0,0) is top-left. x goes right, y goes down.
function love.draw()
love.graphics.rectangle("fill", 100, 100, 50, 50) -- x, y, w, h
love.graphics.circle("fill", 300, 200, 25) -- x, y, radius
love.graphics.print("Score: 10", 10, 10)
endLoad images once in love.load, draw them in love.draw:
local ship
function love.load()
ship = love.graphics.newImage("assets/ship.png")
end
function love.draw()
love.graphics.draw(ship, 100, 100)
endInput
function love.update(dt)
-- held keys (smooth movement)
if love.keyboard.isDown("left") then player.x = player.x - 300 * dt end
if love.keyboard.isDown("right") then player.x = player.x + 300 * dt end
end
function love.keypressed(key)
-- single presses (jump, pause, quit)
if key == "space" then jump() end
if key == "escape" then love.event.quit() end
endAudio (2 lines)
local music
function love.load()
music = love.audio.newSource("assets/theme.mp3", "stream")
love.audio.play(music)
endShort sound effects: use "static" instead of "stream". More in Audio recipe.
Try it (10 min)
Make a circle follow your arrow keys:
local x, y = 400, 300
function love.update(dt)
local speed = 300
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.circle("fill", x, y, 25)
endNext step
Continue with Your First Game — turn this into Pong, start to finish.