Basic Game Example
This example demonstrates a basic Love2D structure using Luau type annotations.
main.luau
Section titled “main.luau”type Vector2 = { x: number, y: number }
local position: Vector2 = { x = 400, y = 300 }local velocity: Vector2 = { x = 200, y = 150 }local logo: love.Image
function love.load() logo = love.graphics.newImage("assets/logo.png")end
function love.update(dt: number) position.x += velocity.x * dt position.y += velocity.y * dt
local width = love.graphics.getWidth() local height = love.graphics.getHeight()
if position.x < 0 or position.x + logo:getWidth() > width then velocity.x = -velocity.x end
if position.y < 0 or position.y + logo:getHeight() > height then velocity.y = -velocity.y endend
function love.draw() love.graphics.draw(logo, position.x, position.y)endRunning
Section titled “Running”- Place an image at
assets/logo.png. - Run
kaledis dev.
Common Troubles
Section titled “Common Troubles”- “Attempt to call method ‘getWidth’ (a nil value)”: Did you forget to load the image in
love.load? In Love2D, assets must be loaded before they are used. - Black Screen: Ensure
love.drawis actually drawing something visible.