have you written code before?
this determines where we start
you just played a game. about 20 lines of Lua.
let's build it. line by line.
01 / 12
line 1. the title.
print() writes text to the screen.
click run to see what happens
nice. that's your game's first line. try changing the text inside the quotes.
02 / 12
variables. store things.
local creates a variable. the value is whatever comes after =.
local name = io.read("your name? ")
your name?
variables
that's variables. name holds whatever they typed.
03 / 12
strings. .. joins them together.
Lua uses .. to combine text. click the blocks to see how:
drag to reorder, click to change:
"brave soul "
..
name
..
"."
result:
brave soul player.
print("brave soul " .. name .. ".")
three pieces joined into one string.
04 / 12
if/else. the cave choice.
if the player types 'i', run one thing. otherwise, run another.
go inside or leave?
local choice = io.read("[i]nside or [l]eave? ")
if choice == "i" then
print("you venture inside...")
else
print("you go home safely.")
end
05 / 12
loops. do it 5 times.
a for loop counts and runs code each time.
fori=5,1,-1do
^^^^^
keywordvariablestartstopstep
for i = 5, 1, -1 do
print(i)
end
print("GO!")
i = —
output will appear here...
loop finished. ran 5 times. ✓
try it yourself:
click run
06 / 12
randomness. flip the coin.
math.random(1, 2) returns 1 or 2. the cave game uses this to decide treasure or trap.
?
math.random(1, 2)
== 1: treasure!
== 2: bat swoops!
if math.random(1, 2) == 1 then
print("treasure!")
else
print("bat swoops!")
end
07 / 12
special powers.
the arcade gives your game things browsers can't normally do. click each one:
the quick brown fox
activate 0 / 5 powers to continue
08 / 12
faster controls.
io.getkey() reads a single keypress - no Enter needed. good for action games.
press any key:
waiting for keypress...
use WASD or arrow keys to move. reach the star:
09 / 12
tables. keep a list.
a table holds multiple values. like a backpack for your game.
backpack
items: 0
local backpack = {}
functions. reuse code.
wrap code in a function to call it by name. edit the code below and hit run:
click run
10 / 12
the arcade has a database.
your game can save scores that last forever. two lines.
net.rank(name, score)
local top = net.top(5)
add a score
query the board
net.top(
)
the score is whatever your game decides. net.rank(name, score) handles the rest.
11 / 12
two last things.
no accounts. when you submit a game, you choose your own edit code. this is how you edit or delete your game later.
my-secret-code
pick something you'll remember. minimum 4 characters.
the editor has a docs panel on the right side. every API with examples, plus ready-to-paste snippets:
snippets
reference
hello world · number guesser · story game · countdown · leaderboard · bouncing ball
click any snippet to insert code at cursor. reference tab has every API.
12 / 12
you built it.
every line you learned. one game.
the cave, complete
now make your own. pick a starting point:
done.
pick a starter.
choose a starting point. you can change everything in the editor.
arcade APIs
what the arcade adds
games run in a Lua sandbox. beyond standard Lua, you get these. click each to see what it does:
two ways to get input. use whichever fits your game:
io.read("prompt")
shows the prompt inline in the terminal, waits for Enter. returns the typed text as a string. use for names, answers, commands.
name: player
hello, player!
hello, player!
io.getkey()
waits for a single keypress. no Enter needed. returns the key name: "a", "space", "up", "down", "left", "right". use for real-time controls, movement, menus.
pressed: up
you also get these utilities:
sleep(ms) / clear()
pause execution for N milliseconds (async, doesn't freeze the browser). clear() wipes the terminal output.
3... 2... 1... GO!
colored(text, color.red)
ANSI color output. colors: red, green, yellow, blue, cyan, purple, bold.
sound.win() / sound.lose()
web audio sounds. also: click, coin, blip, buzz, beep(freq, dur).
♪
net.rank(name, score) / net.get/set
persistent leaderboard + key-value storage, scoped to your game. net.top(n) gets top entries.
1. player 99
2. guest 42
2. guest 42
the editor has a docs button in the top bar. click it to open a panel with snippets you can insert and a full Lua reference. it pulses when you first open the editor.
vim keybindings are available too. toggle with the vim button. :w saves, :wq saves and exits.
my-secret-code
when you submit a game you choose your own edit code. pick something you'll remember.
launching editor...