Lesson 7 of 8

Your First Game with LÖVE

Build a small game with LÖVE, a free 2D game framework. You write three (load, update and draw) and LÖVE handles the window, graphics and input.

Lua
-- main.lua — put in a folder, then run: love that-folder/
local player = { x = 100, y = 100, speed = 200 }

function love.load()
  love.window.setTitle("My First Game")
end

function love.update(dt)
  -- dt = seconds since last frame; multiply for smooth motion
  if love.keyboard.isDown("right") then
    player.x = player.x + player.speed * dt
  end
  if love.keyboard.isDown("left") then
    player.x = player.x - player.speed * dt
  end
  if love.keyboard.isDown("up") then
    player.y = player.y - player.speed * dt
  end
  if love.keyboard.isDown("down") then
    player.y = player.y + player.speed * dt
  end
end

function love.draw()
  love.graphics.setColor(0.2, 0.8, 1)
  love.graphics.rectangle("fill", player.x, player.y, 32, 32)
  love.graphics.setColor(1, 1, 1)
  love.graphics.print("Arrow keys to move", 10, 10)
end

That's the whole game pattern used by every engine: update() advances the world based on elapsed time, draw() renders it, ~60 times per second. Multiplying movement by dt keeps speed the same whether the game runs at 30 or 240 frames per second.

Lua
-- Add gravity and a jump — platformer physics in 15 lines:
local GRAVITY = 800

function love.update(dt)
  player.vy = (player.vy or 0) + GRAVITY * dt
  player.y = player.y + player.vy * dt

  local floor = 400
  if player.y > floor then
    player.y = floor
    player.vy = 0
    player.grounded = true
  end
end

function love.keypressed(key)
  if key == "space" and player.grounded then
    player.vy = -400
    player.grounded = false
  end
end
Finished reading?Mark it done to track your progress.

Tip: use the ← and → keys to move between lessons.