ly/res/example.lua
2026-05-20 15:40:00 -04:00

49 lines
1.7 KiB
Lua

-- [[
-- This is an example of using LuaJIT to create a custom animation in Ly, in this case a
-- bouncing square that changes colors.
--
-- You are given the following `ly` table:
-- {
-- height: number -- The height of the terminal
-- width: number -- The width of the terminal
-- frame_delay: number -- The amount of frames to delay an update to the animation
-- putCell(byte, fg, bg, x, y) -- Put the cell a buffer. This buffer will be drawn
-- on screen after draw() is called. It will not clear
-- itself automatically, see `ly.clear()`.
-- All arguments to this function are numbers, and
-- must be in the unsigned 32-bit integer range: 0 to 2^32-1.
-- clear() -- Clear the buffer.
-- }
-- ]]
local SQUARE_WIDTH = 10
local SQUARE_HEIGHT = 5
local SQUARE_X = 0
local SQUARE_Y = 0
local SQUARE_VX = 1
local SQUARE_VY = 1
local timer = os.clock()
local color = math.random(0xFFFFFF)
function draw()
-- Rather than progressing the animation by frame, do it based on
-- seconds, like from os.clock().
if timer + 0.025 < os.clock() then
ly.clear()
SQUARE_X = SQUARE_X + SQUARE_VX
SQUARE_Y = SQUARE_Y + SQUARE_VY
for x = SQUARE_X, SQUARE_X + SQUARE_WIDTH do
for y = SQUARE_Y, SQUARE_Y + SQUARE_HEIGHT do
ly.putCell(string.byte(' '), 0, color, x, y)
end
end
if SQUARE_X <= 0 then SQUARE_VX = 1; color = math.random(0xFFFFFF) end
if SQUARE_X + SQUARE_WIDTH >= ly.width-1 then SQUARE_VX = -1; color = math.random(0xFFFFFF) end
if SQUARE_Y <= 0 then SQUARE_VY = 1; color = math.random(0xFFFFFF) end
if SQUARE_Y + SQUARE_HEIGHT >= ly.height-1 then SQUARE_VY = -1; color = math.random(0xFFFFFF) end
timer = os.clock()
end
end