Like2D Logo Like2D
← Back to Docs

Tutorials

Tutorial 1: Hello World

Objective: Create your first Like2D application that displays text on screen.

Step 1: Create Project Structure

HelloWorld/
└── main.luau

Step 2: Write the Code

Create main.luau:

-- main.luau - Hello World Tutorial

function onInit()
    setWindowTitle("Hello World Tutorial")
    setWindowSize(800, 600)
    print("Hello World tutorial started!")
end

function onDraw()
    clearScreen(100, 149, 237)  -- Cornflower blue
    local text = "Hello, Like2D World!"
    local centerX = 400 - (string.len(text) * 12)
    local centerY = 300
    drawTextDirect(text, centerX, centerY, 48, 255, 255, 255, 255)
    drawTextDirect("Press ESC to exit", 320, 350, 24, 255, 255, 100, 255)
end

function onCleanup()
    print("Hello World tutorial ended")
end

Step 3: Run the Application

.\Like2D.exe "HelloWorld"

Expected Result: A blue window with "Hello, Like2D World!" text in white.

Tutorial 2: Basic Movement

Objective: Add keyboard-controlled movement to a simple colored rectangle.

Step 1: Create Project

Movement/
└── main.luau

Step 2: Write the Code

-- main.luau - Basic Movement Tutorial

local player = {
    x = 400, y = 300, width = 50, height = 50,
    speed = 5, color = {r = 255, g = 100, b = 100}
}

function onInit()
    setWindowTitle("Movement Tutorial")
    setWindowSize(800, 600)
end

function onDraw()
    clearScreen(40, 40, 50)
    if isKeyDown("Left") then player.x = player.x - player.speed end
    if isKeyDown("Right") then player.x = player.x + player.speed end
    if isKeyDown("Up") then player.y = player.y - player.speed end
    if isKeyDown("Down") then player.y = player.y + player.speed end
    player.x = math.max(0, math.min(800 - player.width, player.x))
    player.y = math.max(0, math.min(600 - player.height, player.y))
    drawRect(player.x, player.y, player.width, player.height, 
             player.color.r, player.color.g, player.color.b, 255, true, 0)
    drawTextDirect("Use Arrow Keys to Move", 10, 10, 20, 255, 255, 255, 255)
end

Tutorial 3: Image Rendering

Objective: Load and display images in your application.

Step 1: Create Project with Assets

ImageDemo/
├── main.luau
└── assets/
    ├── player.png
    └── background.jpg

Step 2: Write the Code

-- main.luau - Image Rendering Tutorial

local player = { x = 400, y = 300, width = 64, height = 64, speed = 3 }

function onInit()
    setWindowTitle("Image Rendering Tutorial")
    setWindowSize(800, 600)
    loadImage("assets/player.png")
    loadImage("assets/background.jpg")
end

function onDraw()
    renderImage("assets/background.jpg", 0, 0, 800, 600)
    if isKeyDown("Left") then player.x = player.x - player.speed end
    if isKeyDown("Right") then player.x = player.x + player.speed end
    if isKeyDown("Up") then player.y = player.y - player.speed end
    if isKeyDown("Down") then player.y = player.y + player.speed end
    renderImage("assets/player.png", player.x, player.y, player.width, player.height)
    drawTextDirect("Arrow keys to move", 10, 10, 18, 255, 255, 255, 255)
end

Tutorial 4: Multi-file Projects

Objective: Learn how to organize your code across multiple files using dofile().

Step 1: Create Two Files

MyProject/
├── main.luau
└── anim.luau

Step 2: Write the Animation Logic

-- anim.luau
anim_speed = 0.5

function get_blink_alpha()
    return math.sin(getTime() / 1000 * anim_speed) * 0.5 + 0.5
end

Step 3: Include it in Main

-- main.luau
dofile("anim.luau")

function onInit()
    setWindowTitle("Multi-file Tutorial")
    setWindowSize(800, 600)
end

function onDraw()
    clearScreen(30, 30, 30)
    local alpha = get_blink_alpha()
    drawTextDirect("BLINKING TEXT", 300, 300, 32, 255, 255, 255, alpha * 255)
end

Tutorial 5: File Encryption & Save System

Objective: Learn how to encrypt game assets and create secure save files.

Step 1: Create Project

EncryptionDemo/
├── main.luau
└── assets/
    └── hero.png

Step 2: Write the Code

-- main.luau - Encryption & Save System Tutorial

local player = { name = "Hero", score = 0, level = 1, x = 400, y = 300 }

function onInit()
    setWindowTitle("Encryption Demo")
    setWindowSize(800, 600)
    createDirectory("saves")
    loadImage("assets/hero.png.enc")
    
    local saveData = readEncryptedFile("saves/latest.dat")
    if saveData then
        local loaded = json.decode(saveData)
        player.score = loaded.score
        player.level = loaded.level
    end
end

function onUpdate(dt)
    if isKeyDown("Right") then player.x = player.x + 200 * dt end
    if isKeyDown("Left")  then player.x = player.x - 200 * dt end
    if isKeyJustPressed("s") then
        player.score = player.score + 100
        writeEncryptedFile("saves/latest.dat", json.encode({
            score = player.score, level = player.level
        }))
    end
end

function onDraw()
    clearScreen(30, 30, 50)
    renderImage("assets/hero.png.enc", player.x, player.y, 64, 64)
    drawTextDirect("Score: " .. player.score, 10, 35, 20, 255, 255, 0, 255)
end

Next Steps