API Reference
Every Lua function at your fingertips. Search, browse, and learn the complete Tiny API.
Iterate over values of a table. - If you want to iterate over keys, use `pairs(table)`. - If you want to iterate over index, use `ipairs(table)`. - If you want to iterate in reverse, use `rpairs(table)`.
Append *all values* from the table `source` to the table `dest`.
function _draw()
gfx.cls()
local src = {1, 2, 3}
local dst = {4, 5}
local result = append(src, dst)
for k,v in ipairs(result) do
print(v, (k + 1) * 8, 8)
end
end
Add *all key/value* from the table `source` to the table `dest`.
function _draw()
gfx.cls()
local src = {x = 1, y = 2, z = 3}
local dst = {a = 4, b = 5}
local result = merge(src, dst)
local index = 1
for k,v in pairs(result) do
print(k..":"..v , index * 4 * 8, 8)
index = index + 1
end
end
Create new instance of a class by creating a new table and setting the metatable. It allow to create kind of Object Oriented Programming.
local Player = {
x = 128,
y = 128,
color = 8
}
function Player:draw()
shape.rectf(self.x, self.y, 10, 10, self.color)
end
function _init()
-- create a new player
player = new(Player)
-- create a new player with default vaules
player2 = new(Player, {x = 200, y = 200, color = 9})
end
function _draw()
gfx.cls()
player:draw() -- call the draw method on the player instance.
player2:draw() -- call the draw method on the player2 instance.
end
Print on the screen a string. Default color is the closest to white (#FFFFFF) in the palette. To ensure visibility, use a color that contrasts with the cls() background color.
function _draw()
gfx.cls()
-- every character is a sprite 4x4 pixels.
print("hello")
print("world", 10, 8)
print("how", 10, 16, 4)
print("are", 28, 16, 5)
print("you", 46, 16, 6)
print("...", 64, 16, math.rnd(10))
end
Iterate over values of a table in reverse order. The iterator return an index and the value. The method is useful to remove elements from a table while iterating on it.
function _draw()
gfx.cls()
local data = {
{ name = "riri" },
{ name = "fifi" },
{ name = "loulou" }
}
local y = 0
for index, key in rpairs(data) do
print(index .. " - " .. key.name, 10, y)
y = y + 10
end
end
Log a message into the console.
function _update()
console.log("hello from the console")
console.log("Log a value: ", tiny.frame)
console.log("Log a table: ", ctrl.touch())
end
function _draw()
gfx.cls()
-- draw the mouse position.
local pos = ctrl.touch()
shape.line(pos.x - 2, pos.y, pos.x + 2, pos.y, 3)
shape.line(pos.x, pos.y - 2, pos.x, pos.y + 2, 3)
end
Return true if the key was pressed during the last frame. When called without argument, return a table of all keys pressed during the last frame (values matching the keys lib), or false if no key was pressed. If you need to check that the key is still pressed, see `ctrl.pressing` instead.
local percent_a = 1
local percent_b = 1
function _update()
percent_a = math.min(percent_a + 0.05, 1)
percent_b = math.min(percent_b + 0.05, 1)
if ctrl.pressed(keys.space) then
percent_a = 0
end
if ctrl.pressing(keys.space) then
percent_b = 0
end
local offset_a = juice.powIn2(0, 8, percent_a)
local offset_b = juice.powIn2(0, 8, percent_b)
gfx.cls()
shape.rectf(64, 128 - 16, 32, 32, 7)
shape.rectf(64, 128 - 32 + offset_a, 32, 32, 8)
shape.rectf(32 + 128, 128 - 16, 32, 32, 7)
shape.rectf(32 + 128, 128 - 32 + offset_b, 32, 32, 8)
print("pressed", 64, 128 + 32)
print("pressing", 32 + 128, 128 + 32)
end
Return true if the key is still pressed.
local percent_a = 1
local percent_b = 1
function _update()
percent_a = math.min(percent_a + 0.05, 1)
percent_b = math.min(percent_b + 0.05, 1)
if ctrl.pressed(keys.space) then
percent_a = 0
end
if ctrl.pressing(keys.space) then
percent_b = 0
end
local offset_a = juice.powIn2(0, 8, percent_a)
local offset_b = juice.powIn2(0, 8, percent_b)
gfx.cls()
shape.rectf(64, 128 - 16, 32, 32, 7)
shape.rectf(64, 128 - 32 + offset_a, 32, 32, 8)
shape.rectf(32 + 128, 128 - 16, 32, 32, 7)
shape.rectf(32 + 128, 128 - 32 + offset_b, 32, 32, 8)
print("pressed", 64, 128 + 32)
print("pressing", 32 + 128, 128 + 32)
end
Get coordinates of the current touch/mouse. If the mouse/touch is out-of the screen, the coordinates will be the last mouse position/touch. The function return those coordinates as a table {x, y}. A sprite can be draw directly on the mouse position by passing the sprite number.
function _draw()
gfx.cls(2)
p = ctrl.touch()
print("coordinates: "..p.x .. "x"..p.y, 1, 1, 4)
shape.rectf(p.x, p.y, 5,5, p.x + p.y)
end
Return the position of the touch (as `{x, y}`)if the screen was touched or the mouse button was pressed during the last frame. `nil` otherwise. The touch can be : - 0: left click or one finger - 1: right click or two fingers - 2: middle click or three fingers If you need to check that the touch/mouse button is still active, see `ctrl.touching` instead.
local circles = {}
function _update()
local pos = ctrl.touched(0)
if pos ~= nil then
table.insert(circles, pos)
end
end
function _draw()
gfx.cls()
local p = ctrl.touch()
shape.circlef(p.x, p.y, 4, 8)
for pos in all(circles) do
shape.circlef(pos.x, pos.y, 4, 9)
print("("..pos.x .. ", "..pos.y..")", pos.x + 3, pos.y + 3)
end
end
Return the position of the touch (as `{x, y}`)if the screen is still touched or the mouse button is still pressed. `nil` otherwise. The touch can be : - 0: left click or one finger - 1: right click or two fingers - 2: middle click or three fingers
function _draw()
gfx.cls()
local p = ctrl.touch()
shape.circlef(p.x, p.y, 4, 8)
local start = ctrl.touching(0)
if start ~= nil then
local pos = ctrl.touch()
shape.line(start.x, start.y, pos.x, pos.y, 1)
print("("..start.x .. ", "..start.y..")", start.x, start.y)
print("("..pos.x .. ", "..pos.y..")", pos.x, pos.y)
end
end
Load and get the content of the file name
Save the content into a local file, on desktop or in the local storage on the web platform.
Move the game camera.
local x = 0
local y = 0
function _update()
if ctrl.pressing(keys.left) then
x = x - 0.5
elseif ctrl.pressing(keys.right) then
x = x + 0.5
end
if ctrl.pressing(keys.up) then
y = y - 0.5
elseif ctrl.pressing(keys.down) then
y = y + 0.5
end
gfx.camera(math.floor(x), math.floor(y))
end
function _draw()
gfx.cls(2)
for x = 0 - 64, 256 + 64, 16 do
for y = 0 - 64, 256 + 64, 16 do
shape.line(x - 2, y, x + 2, y, 9)
shape.line(x, y - 2, x, y + 2, 9)
end
end
print("camera: ("..x..", "..y..")", 6, 6)
shape.rect(0, 0, 256, 256, 1)
endClip the draw surface (ie: limit the drawing area).
function _init()
c = {}
for i=1,100 do
table.insert(c, {x=math.rnd(256), y=math.rnd(256), c = math.rnd(1,12)})
end
end
function _draw()
gfx.cls()
local pos = ctrl.touch()
-- set a clip area to crop circles
gfx.clip(pos.x - 20, pos.y - 20, 40, 40)
for circle in all(c) do
shape.circlef(circle.x, circle.y, 10, circle.c)
end
end
Clear the screen. When called without arguments, clears with the color closest to black (#000000) in the palette. To ensure visibility, always draw with a color index DIFFERENT from the cls() color.
function _draw()
if ctrl.pressed(keys.space) then
gfx.cls()
end
print("Press space to clear the screen")
local pos = ctrl.touch()
shape.circlef(pos.x, pos.y, 4, math.rnd())
endApply a dithering pattern on every new draw call. The pattern is using the bits value of a 2 octet value. The first bits is the one on the far left and represent the pixel of the top left of a 4x4 matrix. The last bit is the pixel from the bottom right of this matrix.
function _draw()
gfx.cls()
gfx.dither()
shape.circlef(30, 30, 30, 2)
gfx.dither(0xA5A5) -- set a dithering pattern
shape.circlef(50, 50, 30, 3)
gfx.dither(0x0842) -- set another dithering pattern
shape.circlef(70, 70, 30, 2)
end
Switch to another draw mode. - 0: default. - 1: drawing with transparent (ie: can erase part of the screen) - 2: drawing a stencil that will be use with the next mode - 3: drawing using a stencil test (ie: drawing only in the stencil) - 4: drawing using a stencil test (ie: drawing everywhere except in the stencil)
local draw_stencil = 2
local draw_inside_stencil = 3
local draw_outside_stencil = 4
local outside = false
function _draw()
if ctrl.pressed(keys.space) then
outside = not outside
end
gfx.cls(3)
gfx.draw_mode(draw_stencil)
-- draw a circle in the middle of the screen
shape.circlef(128, 128, 64, 1)
if outside then
gfx.draw_mode(draw_inside_stencil)
else
gfx.draw_mode(draw_outside_stencil)
end
-- draw the sprite sheet. only in the circle
spr.sdraw()
gfx.draw_mode()
end
Change a color from the palette to another color.
function _draw()
gfx.cls()
print("example", 10, 10, 2) -- print using the color index 2
gfx.pal(2, 3) -- switch the text color to another color
print("example", 10, 20, 2) -- print using the color index 2
gfx.pal() -- reset the palette
print("example", 10, 30, 2) -- print using the color index 2
end
Get the color index at the coordinate (x,y).
function _draw()
gfx.cls()
local index = 0
for x=0, 240, 16 do
for y=0, 240, 16 do
shape.rectf(x, y, 16, 16, index)
index = index + 1
end
end
local pos = ctrl.touch()
local color = gfx.pget(pos.x, pos.y)
if color ~= nil then
shape.rectf(0, 0, 80, 6, 13)
print("color index: "..color)
end
shape.circlef(pos.x - 2, pos.y - 2, 4, 0)
endSet the color index at the coordinate (x,y).
function _draw()
local pos = ctrl.touching(0)
if pos ~= nil then
-- set the pixel with the color 9 when the mouse is pressed
local p = ctrl.touch()
gfx.pset(p.x, p.y, 9)
end
endTransform the current frame buffer into a spritesheeet. - If the index of the spritesheet already exist, the spritesheet will be replaced - If the index of the spritesheet doesn't exist, a new spritesheet at this index will be created - If the index of the spritesheet is negative, a new spritesheet will be created at the last positive index.
function _draw()
gfx.cls(1)
-- draw a transparent circle (like a hole)
shape.circlef(64, 128, 20, 0)
-- keep the result as spritesheet 0
gfx.to_sheet(0)
gfx.cls(1)
-- draw some circles
shape.circlef(64, 108, 20, 8)
shape.circlef(44, 128, 20, 9)
shape.circlef(64, 148, 20, 10)
shape.circlef(84, 128, 20, 11)
-- draw over the circles
-- the mask generated before.
spr.sheet(0)
spr.sdraw()
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128
function _update()
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)
for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end
local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end
Get the flag from a tile, using cell coordinates.
Draw map tiles on the screen.
Table with all entities by type (ie: `map.entities["player"]`). ``` local entities = map.entities() local players = entities["Player"] for entity in all(players) do shape.rectf(entity.x, entity.y, entity.width, entity.height, 8) -- display an entity using a rectangle end [...] entity.fields -- access custom field of the entity ```
Get the flag from a tile, using screen coordinates.
Convert cell coordinates cx, cy into map screen coordinates x, y.
Get the list of layers from the actual level.
Set the current level to use.
Convert screen coordinates x, y into map cell coordinates {cx, cy}. For example, coordinates of the player can be converted to cell coordinates to access the flag of the tile matching the player coordinates.
Calculate the angle in radians between the positive x-axis and the point (x, y).
Clamp the value between 2 values.
function _draw()
gfx.cls()
local pos = ctrl.touch()
local x = math.clamp(60, pos.x, 256 - 60)
gfx.dither(0xA5A5)
shape.line(64, 129, 256 - 60, 129, 9)
gfx.dither()
shape.rect(60, 128, 4, 4, 9)
shape.rect(256 - 60, 128, 4, 4, 9)
shape.circlef(pos.x, pos.y, 2, 9)
shape.circle(x, 129, 2, 8)
end
Compute the distance between two points.
function _draw()
gfx.cls()
local pos = ctrl.touch()
shape.line(pos.x, pos.y, 128, 128, 3)
shape.circlef(128, 128, 2, 9)
shape.circlef(pos.x, pos.y, 2, 9)
-- midle of the line
local x = (pos.x - 128) * 0.5
local y = (pos.y - 128) * 0.5
-- display dst
local dst = math.dst(128, 128, pos.x, pos.y)
print("dst: "..dst, 128 + x, 128 + y)
end
Compute the distance between two points not squared. Use this method to know if an coordinate is closer than another.
local a = {
x = 10 + math.rnd(236),
y = 10 + math.rnd(236)
}
local b = {
x = 10 + math.rnd(236),
y = 10 + math.rnd(236)
}
function _draw()
gfx.cls()
local pos = ctrl.touch()
if math.dst2(pos.x, pos.y, a.x, a.y) > math.dst2(pos.x, pos.y, b.x, b.y) then
-- b is closer
shape.line(pos.x, pos.y, b.x, b.y, 3)
else
-- a is closer
shape.line(pos.x, pos.y, a.x, a.y, 3)
end
shape.circlef(a.x, a.y, 2, 9)
shape.circlef(b.x, b.y, 2, 9)
shape.circlef(pos.x, pos.y, 2, 9)
end
Perlin noise. The random generated value is between 0.0 and 1.0.
function _draw()
gfx.cls(8)
gfx.dither(0x0001)
local x = math.perlin(0.1, 0.2, tiny.frame / 100)
local y = math.perlin(0.4, 0.5, tiny.frame / 100)
shape.circlef(x * 256, y * 256, 64, 7)
end
Generate random values
local value = {}
function _update()
table.insert(value, math.rnd(126))
if(#value > 50) then
table.remove(value, #value)
end
end
function _draw()
gfx.cls()
local y = 0
for v in all(value) do
print("rnd: "..v, 4, y)
y = y + 6
end
end
Check if two (r)ectangles overlaps.
Return the sign of the number: -1 if negative. 1 otherwise.
function _draw()
gfx.cls()
local cos = math.cos(tiny.t)
print("cos: "..cos)
print("sign: "..math.sign(cos), 0, 8)
shape.line(128, 128, 128 + cos * 128, 128, 9)
end
positive infinity value.
value of pi (~3.14)
Get the name of a note regarding the note index (ie: C0 = 0, Cs0 = 1, ...)
Access instrument using its index or its name.
Save the actual music in the current sfx file.
Access musical sequence using its index.
Access sfx using its index or its name.
Draw a circle.
function _draw()
gfx.cls()
-- filled circle
shape.circlef(20, 20, 20, 4)
shape.circlef(30, 30, 20, 5)
shape.circlef(40, 40, 20, 6)
print("filled circle", 20, 10)
-- non filled circle
shape.circle(50, 70, 10, 7)
shape.circle(60, 80, 10, 8)
shape.circle(70, 90, 10, 9)
print("non filled circle", 20, 65)
shape.circle(80, 120, 15, 10)
shape.circle(80, 140, 20, 16)
shape.circle(80, 160, 30, 14)
print("circle with different radius", 20, 115)
end
Draw a filled circle.
function _draw()
gfx.cls()
-- filled circle
shape.circlef(20, 20, 20, 4)
shape.circlef(30, 30, 20, 5)
shape.circlef(40, 40, 20, 6)
print("filled circle", 20, 10)
-- non filled circle
shape.circle(50, 70, 10, 7)
shape.circle(60, 80, 10, 8)
shape.circle(70, 90, 10, 9)
print("non filled circle", 20, 65)
shape.circle(80, 120, 15, 10)
shape.circle(80, 140, 20, 16)
shape.circle(80, 160, 30, 14)
print("circle with different radius", 20, 115)
end
Draw a gradient using dithering, only from color c1 to color c2.
function _draw()
local c1 = 2
local c2 = 3
gfx.cls(c1)
shape.rectf(0, 256 - 16, 256, 16, c2)
for x=0,240,16 do
shape.gradient(x, 16 * math.cos(2 * 3.14 * (x / 256) + tiny.t * 2), 16, 256, c1, c2, false)
end
end
Draw a line.
function _draw()
gfx.cls()
local i =0
for x =16, 240, 16 do
for y =16, 240, 16 do
shape.line(x, y, 256 - x, 256 - y, i)
i = i + 1
end
end
end
Draw a rectangle.
function _draw()
gfx.cls()
-- filled rectangle
print("filled rectangle", 20, 10)
shape.rectf(20, 20, 20, 20, 4)
shape.rectf(30, 30, 20, 20, 5)
shape.rectf(40, 40, 20, 20, 6)
print("non filled rectangle", 20, 65)
-- non filled rectangle
shape.rect(50, 70, 20, 20, 7)
shape.rect(60, 80, 20, 20, 8)
shape.rect(70, 90, 20, 20, 9)
print("rectangle with different width", 20, 115)
shape.rect(20, 120, 30, 20, 10)
shape.rect(20, 140, 40, 20, 12)
shape.rect(20, 160, 60, 20, 13)
end
Draw a filled rectangle.
function _draw()
gfx.cls()
-- filled rectangle
print("filled rectangle", 20, 10)
shape.rectf(20, 20, 20, 20, 4)
shape.rectf(30, 30, 20, 20, 5)
shape.rectf(40, 40, 20, 20, 6)
print("non filled rectangle", 20, 65)
-- non filled rectangle
shape.rect(50, 70, 20, 20, 7)
shape.rect(60, 80, 20, 20, 8)
shape.rect(70, 90, 20, 20, 9)
print("rectangle with different width", 20, 115)
shape.rect(20, 120, 30, 20, 10)
shape.rect(20, 140, 40, 20, 12)
shape.rect(20, 160, 60, 20, 13)
end
Draw a triangle using the coordinates of (x1, y1), (x2, y2) and (x3, y3) and color.
function tri(f, fill)
local x1 = 128 + math.cos(f) * 64
local y1 = 128 + math.sin(f) * 64
local x2 = 128 + math.cos(f + math.pi * 1/3) * 64
local y2 = 128 + math.sin(f + math.pi * 1/3) * 64
local x3 = 128 + math.cos(f+ math.pi * 2/3) * 64
local y3 = 128 + math.sin(f +math.pi * 2/3) * 64
if fill then
shape.trianglef(x3, y3, x2, y2, x1, y1, 8)
else
shape.triangle(x1, y1, x2, y2, x3, y3, 8)
end
end
function _draw()
gfx.cls()
local f = tiny.frame * 0.01
tri(f, false)
tri(f*2, true)
end
Draw a filled triangle using the coordinates of (x1, y1), (x2, y2) and (x3, y3) and color.
function tri(f, fill)
local x1 = 128 + math.cos(f) * 64
local y1 = 128 + math.sin(f) * 64
local x2 = 128 + math.cos(f + math.pi * 1/3) * 64
local y2 = 128 + math.sin(f + math.pi * 1/3) * 64
local x3 = 128 + math.cos(f+ math.pi * 2/3) * 64
local y3 = 128 + math.sin(f +math.pi * 2/3) * 64
if fill then
shape.trianglef(x3, y3, x2, y2, x1, y1, 8)
else
shape.triangle(x1, y1, x2, y2, x3, y3, 8)
end
end
function _draw()
gfx.cls()
local f = tiny.frame * 0.01
tri(f, false)
tri(f*2, true)
end
Play a music
Play a note by an instrument until it's stopped
function _init()
keys = {
{note="E4", x=57, y=100, w=16, h=50},
{note="Fs4", x=75, y=100, w=16, h=50},
{note="Gs4", x=93, y=100, w=16, h=50},
{note="A4", x=111, y=100, w=16, h=50},
{note="B4", x=129, y=100, w=16, h=50},
{note="Cs5", x=147, y=100, w=16, h=50},
{note="Ds5", x=165, y=100, w=16, h=50},
{note="E5", x=183, y=100, w=16, h=50}
}
active_notes = {}
end
function _update()
local touch = ctrl.touching(0)
for i, key in ipairs(keys) do
if in_bounds(touch, key) then
active_notes[i] = active_notes[i] or sound.note(key.note, 1)
else
active_notes[i] = active_notes[i] and active_notes[i].stop()
end
end
end
function _draw()
gfx.cls()
print("E MAJOR SCALE", 57, 84, 15)
print("Click keys to play", 57, 92, 14)
for i, key in ipairs(keys) do
if active_notes[i] then
shape.rectf(key.x, key.y, key.w, key.h, 15)
else
shape.rect(key.x, key.y, key.w, key.h, 14)
end
end
local pos = ctrl.touch()
shape.circlef(pos.x, pos.y, 2, 2)
end
function in_bounds(touch, key)
return touch and touch.x >= key.x and touch.x < key.x + key.w and
touch.y >= key.y and touch.y < key.y + key.h
end
Play a sfx.
Draw a sprite.
function _init()
id = 1
end
function _draw()
if ctrl.pressed(keys.left) then
id = id - 1
elseif ctrl.pressed(keys.right) then
id = id + 1
end
gfx.cls()
print("sprite index "..id.. " (press left or right to change)", 50, 112)
spr.draw(id, 120, 120)
end
Get the color index at the coordinate (x,y) from the current spritesheet.
function _draw()
local pos = ctrl.touch()
gfx.cls()
spr.sdraw()
shape.circlef(pos.x - 4, pos.y - 4, 8, 3)
local color = spr.pget(pos.x, pos.y)
if(color ~= nil) then
print("index color: "..color, 7, 0)
shape.rectf(0, 0, 6, 6, color)
shape.circlef(pos.x - 4, pos.y - 4, 6, color)
end
end
Set the color index at the coordinate (x,y) in the current spritesheet.
function _draw()
local pos = ctrl.touch()
local touching = ctrl.touching(0)
gfx.cls()
spr.sdraw()
if touching ~= nil then
spr.pset(pos.x, pos.y, 9)
end
print("click to alter", 45, 96)
shape.circle(64 + 8, 128 + 8, 32, 1)
shape.circlef(128 + 8, 128 + 8, 32, 1)
spr.draw(100, 128, 128)
shape.circlef(pos.x, pos.y, 2, 3)
end
S(uper) Draw a fragment from the spritesheet.
function _draw()
local pos = ctrl.touch()
gfx.cls()
spr.sdraw()
shape.circlef(pos.x - 4, pos.y - 4, 8, 3)
local color = spr.pget(pos.x, pos.y)
if(color ~= nil) then
print("index color: "..color, 7, 0)
shape.rectf(0, 0, 6, 6, color)
shape.circlef(pos.x - 4, pos.y - 4, 6, color)
end
end
Switch to another spritesheet. The index of the spritesheet is given by it's position in the spritesheets field from the `_tiny.json` file.The first spritesheet is at the index 0. It retuns the previous spritesheet. The spritesheet can also be referenced by its filename.
local current = 0
function _update()
local x = math.perlin((tiny.frame % 100) / 100, (tiny.frame % 100) / 100, (tiny.frame % 100) / 100)
local y = math.perlin((tiny.frame * 0.5 % 100) / 100, (tiny.frame % 100) / 100, (tiny.frame * 0.5 % 100) / 100)
gfx.cls()
shape.circlef(x * 256, y * 256, 10, 8)
gfx.to_sheet("circle.png")
gfx.cls()
shape.rectf(x * 256, y * 256, 20, 20, 8)
gfx.to_sheet("rect.png")
if ctrl.pressed(keys.space) then
current = (current + 1) % 2
end
end
function _draw()
if current == 0 then
spr.sheet("circle.png")
else
spr.sheet("rect.png")
end
spr.sdraw()
end
Select a font to use for text rendering. Call without arguments to reset to the default boot font.
function _draw()
gfx.cls()
-- Use the default boot font
text.font()
text.print("default font", 10, 10)
-- Switch to the first custom font (index 0)
text.font(0)
text.print("custom font", 10, 30)
-- Switch to a custom font by name
text.font("big_font")
text.print("big font", 10, 50)
-- Reset to default
text.font()
text.print("back to default", 10, 70)
end
Print text on the screen using the currently selected font.
function _draw()
gfx.cls()
-- Print with default color (white)
text.print("hello world", 10, 10)
-- Print with a color index
text.print("colored text", 10, 20, 9)
-- Print multiline text
text.print("line 1\nline 2\nline 3", 10, 40)
end
Measure the width in pixels of a string using the currently selected font.
function _draw()
gfx.cls()
local msg = "hello"
local w = text.width(msg)
-- Center the text on screen
local x = (256 - w) / 2
text.print(msg, x, 120)
end
Exit the actual script to switch to another one. The next script to use is identified by it's index. The index of the script is the index of it in the list of scripts from the `_tiny.json` file.The first script is at the index 0.
Delta time between two frame. As Tiny is a fixed frame engine, it's always equal to 1/60
Number of frames elapsed since the start of the game.
Height of the game in pixels.
Current platform: 1 for desktop, 2 for web.
Time elapsed since the start of the game.
Width of the game in pixels.
Add vector2 to another vector2
function _init()
gfx.camera(-128, -128)
local v1 = vec2.create(32, 38)
local v2 = vec2.create(20, 2)
gfx.cls(1)
print("v1", 2, 15, 10)
shape.line(0, 0, v1.x, v1.y, 10)
print("v2", 23, 3, 9)
shape.line(0, 0, v2.x, v2.y, 9)
local v3 = vec2.add(v1, v2)
print("v1 + v2", 30, 15, 11)
gfx.dither(0xAAAA)
shape.line(0, 0, v3.x, v3.y, 11)
gfx.dither()
end
Create a vector 2 as a table { x, y }.
Cross product
Dot product between two vectors
function _init()
angle = 0
end
function _update()
if ctrl.pressing(keys.space) then
angle = angle + 0.1
end
gfx.camera(-128, -128)
local v1 = vec2.create(1, 0)
local v2 = vec2.create(math.cos(angle), math.sin(angle))
local dot = vec2.dot(v1, v2)
local scl = vec2.scl(v1, dot)
gfx.cls(1)
local scaledv1 = vec2.scl(v1, 64)
local scaledv2 = vec2.scl(v2, 64)
local scaledv3 = vec2.scl(scl, 64)
print("v1", 18, 5, 10)
shape.line(0, 0, scaledv1.x, scaledv1.y, 10)
print("v2", scaledv2.x + 5, scaledv2.y + 5, 11)
shape.line(0, 0, scaledv2.x, scaledv2.y, 11)
print("dot", scaledv3.x, 5, 9)
shape.line(0, 0, scaledv3.x, scaledv3.y, 9)
end
Calculate the magnitude (length) of a vector
Normalize a vector
function _update()
gfx.cls(1)
local v0 = vec2.create(43, 64)
local v1 = vec2.nor(v0)
print("vector x: " .. v0.x .. " y: " .. v0.y)
print("normalized x: " .. v1.x .. " y: " .. v1.y, 0, 8)
end
Scale a vector
function _init()
gfx.camera(-128, -128)
local v1 = vec2.create(32, 38)
local v2 = vec2.scl(v1, 2)
gfx.cls(1)
print("v1 scaled", 8, 60, 11)
gfx.dither(0xAAAA)
shape.line(0, 0, v2.x, v2.y, 11)
gfx.dither()
print("v1", 18, 15, 10)
shape.line(0, 0, v1.x, v1.y, 10)
end
Subtract another vector from another vector
function _init()
gfx.camera(-128, -128)
local v1 = vec2.create(32, 38)
local v2 = vec2.create(20, 2)
gfx.cls(1)
print("v1", 18, 15, 10)
shape.line(0, 0, v1.x, v1.y, 10)
print("v2", 23, 3, 9)
shape.line(0, 0, v2.x, v2.y, 9)
local v3 = vec2.sub(v1, v2)
print("v1 - v2", 0, 40, 11)
gfx.dither(0xAAAA)
shape.line(0, 0, v3.x, v3.y, 11)
gfx.dither()
end