Skip to content

Basic Game Example

This example demonstrates a basic Love2D structure using Luau type annotations.

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
end
end
function love.draw()
love.graphics.draw(logo, position.x, position.y)
end
  1. Place an image at assets/logo.png.
  2. Run kaledis dev.
  • “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.draw is actually drawing something visible.