spiral_snake.lua
local sys = require "system"
print [[
This example will draw a snake like spiral on the screen. Showing ANSI escape
codes for moving the cursor around.
]]
sys.autotermrestore()
sys.setconsoleflags(io.stdout, sys.getconsoleflags(io.stdout) + sys.COF_VIRTUAL_TERMINAL_PROCESSING)
local x, y = 1, 1 local dx, dy = 1, 0 local wx, wy = 30, 30 local mx, my = 1, 1
local move_left = "\27[1D"
local move_right = "\27[1C"
local move_up = "\27[1A"
local move_down = "\27[1B"
print(("\n"):rep(wy))
local move = move_right
while wx > 0 and wy > 0 do
sys.sleep(0.01) io.write("*" .. move_left .. move )
io.flush()
x = x + dx
y = y + dy
if x > wx and move == move_right then
dx = 0
dy = 1
move = move_up
wy = wy - 1
my = my + 1
elseif y > wy and move == move_up then
dx = -1
dy = 0
move = move_left
wx = wx - 1
mx = mx + 1
elseif x < mx and move == move_left then
dx = 0
dy = -1
move = move_down
wy = wy - 1
my = my + 1
elseif y < my and move == move_down then
dx = 1
dy = 0
move = move_right
wx = wx - 1
mx = mx + 1
end
end
io.write(move_down:rep(15))
print("\nDone!")