LuLua · Lesson 7 of 8

Your First Game with LÖVE

LÖVE (love2d) is a free 2D game framework: you write three callback functions — load, update, draw — and it handles the window, graphics, and input. A moving-square 'game' is 20 lines.

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 loop pattern used by every engine: update() advances the world based on elapsed time, draw() renders it, ~60 times per second. Multiplying movement by dt makes speed identical on a 30 FPS laptop and a 240 Hz gaming rig.

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
✦ Tip
From here: LÖVE's wiki (love2d.org/wiki) documents every function with examples. For Roblox instead, everything from the OOP lesson applies — Roblox Studio scripts are Luau, a typed Lua dialect.