info = { type = "image", title = "Stick runner", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A stick figure runs and hops over hurdles", api = 33, } local tr = ({ sk = { ["Headband and shoes"] = "Čelenka a topánky", ["Obstacles"] = "Prekážky" }, cs = { ["Headband and shoes"] = "Čelenka a boty", ["Obstacles"] = "Překážky" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "band", label = T("Headband and shoes"), type = "color", default = "#E83C3C" }, { key = "jumps", label = T("Obstacles"), type = "bool", default = true }, } local rect, px = gfx.rect, gfx.px local floor, sin, cos = math.floor, math.sin, math.cos local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local GROUND = 50 local RX = 22 local CYC = 14 local INK, FARI = { 30, 32, 66 }, { 104, 112, 156 } local BAND = { 232, 60, 60 } local BG, HILLS, ROAD = nil, nil, nil local KEY = { 255, 0, 255 } local function build_bg() rect(0, 0, W, H, 150, 204, 236) rect(0, 22, W, 14, 186, 222, 238) for x = 0, W - 1, 2 do px(x, 21, 186, 222, 238) end for y = -5, 5 do for x = -5, 5 do local d = x * x + y * y if d <= 26 then px(48 + x, 9 + y, 255, 232, 130) elseif d <= 40 then px(48 + x, 9 + y, 255, 246, 196) end end end rect(6, 6, 12, 2, 240, 246, 252); rect(8, 5, 6, 1, 240, 246, 252) rect(24, 12, 9, 2, 240, 246, 252); rect(26, 11, 4, 1, 240, 246, 252) for x = 0, W - 1 do local top = floor(30 - 6 * math.abs(((x + 6) % 26) / 13 - 1) - 2 * math.abs(((x + 17) % 11) / 5.5 - 1) + 0.5) rect(x, top, 1, 40 - top, 122, 130, 192) if top < 26 then rect(x, top, 1, 26 - top, 236, 240, 252) end end BG = gfx.snapshot(0, 0, W, H) rect(0, 28, W, 22, KEY[1], KEY[2], KEY[3]) for x = 0, W - 1 do local top = floor(40 - 3 * sin(x * TAU / 64) - 1.5 * sin(x * TAU / 32 + 1) + 0.5) rect(x, top, 1, GROUND - top, 84, 160, 88) px(x, top, 116, 188, 104) end for i = 0, 3 do local x = i * 16 + 5 + umod(hash32(i * 0x9E3779B1), 6) local top = floor(40 - 3 * sin(x * TAU / 64) - 1.5 * sin(x * TAU / 32 + 1) + 0.5) for k = 0, 4 do rect(x - k // 2, top - 9 + k * 2, 1 + (k // 2) * 2, 2, 46, 116, 62) end rect(x, top - 1, 1, 2, 96, 64, 40) end HILLS = gfx.snapshot(0, 28, W, 22) rect(0, GROUND, W, 2, 70, 150, 60) rect(0, GROUND + 2, W, 12, 198, 152, 98) rect(0, GROUND + 10, W, 4, 172, 128, 80) for x = 0, W - 1, 2 do px(x, GROUND + 9, 172, 128, 80) end for x = 0, W - 1, 16 do rect(x + 3, GROUND + 6, 6, 1, 226, 190, 132) end for i = 0, 9 do local h = hash32(i * 0x85EBCA6B + 11) px(umod(h, W), GROUND + 3 + umod(h >> 8, 9), 150, 108, 70) end for i = 0, 5 do local h = hash32(i * 0xC2B2AE35 + 5) local x = umod(h, W) px(x, GROUND - 1, 70, 150, 60); px(x + 1, GROUND - 2, 70, 150, 60) end ROAD = gfx.snapshot(0, GROUND - 2, W, H - GROUND + 2) end local function line(x0, y0, x1, y1, c) x0, y0, x1, y1 = floor(x0 + 0.5), floor(y0 + 0.5), floor(x1 + 0.5), floor(y1 + 0.5) local dx, dy = x1 > x0 and x1 - x0 or x0 - x1, y1 > y0 and y0 - y1 or y1 - y0 local sx, sy = x0 < x1 and 1 or -1, y0 < y1 and 1 or -1 local err = dx + dy while true do px(x0, y0, c[1], c[2], c[3]) if x0 == x1 and y0 == y1 then return end local e2 = 2 * err if e2 >= dy then err = err + dy; x0 = x0 + sx end if e2 <= dx then err = err + dx; y0 = y0 + sy end end end local LEG, ARM = 6, 4 local J = {} local function joints(a, air) local th1, sh1, th2, sh2, ua1, ua2 if air then th1, sh1, th2, sh2 = 1.25, 0.95, -0.7, -2.0 ua1, ua2 = -1.1, 1.3 else local s1, c1 = sin(a), cos(a) th1 = 0.6 * s1; sh1 = th1 - 0.25 - 1.1 * (c1 > 0 and c1 or 0) th2 = -0.6 * s1; sh2 = th2 - 0.25 - 1.1 * (c1 < 0 and -c1 or 0) ua1, ua2 = -0.9 * s1, 0.9 * s1 end J.k1x, J.k1y = LEG * sin(th1), LEG * cos(th1) J.f1x, J.f1y = J.k1x + LEG * sin(sh1), J.k1y + LEG * cos(sh1) J.k2x, J.k2y = LEG * sin(th2), LEG * cos(th2) J.f2x, J.f2y = J.k2x + LEG * sin(sh2), J.k2y + LEG * cos(sh2) J.ua1, J.ua2 = ua1, ua2 end local function draw_runner(t, lift, air) joints(t * TAU / CYC, air) local low = J.f1y > J.f2y and J.f1y or J.f2y local hx, hy = RX, GROUND - 0.6 - low - lift local sx, sy = hx + 2, hy - 9 line(hx, hy, hx + J.k2x, hy + J.k2y, FARI); line(hx + J.k2x, hy + J.k2y, hx + J.f2x, hy + J.f2y, FARI) local ex, ey = sx + ARM * sin(J.ua2), sy + 1 + ARM * cos(J.ua2) line(sx, sy + 1, ex, ey, FARI); line(ex, ey, ex + ARM * sin(J.ua2 + 1.6), ey + ARM * cos(J.ua2 + 1.6), FARI) rect(floor(hx + J.f2x + 0.5), floor(hy + J.f2y + 0.5), 2, 1, BAND[1] * 3 // 4, BAND[2] * 3 // 4, BAND[3] * 3 // 4) line(hx, hy, sx, sy, INK) line(hx, hy, hx + J.k1x, hy + J.k1y, INK); line(hx + J.k1x, hy + J.k1y, hx + J.f1x, hy + J.f1y, INK) rect(floor(hx + J.f1x + 0.5), floor(hy + J.f1y + 0.5), 2, 1, BAND[1], BAND[2], BAND[3]) ex, ey = sx + ARM * sin(J.ua1), sy + 1 + ARM * cos(J.ua1) line(sx, sy + 1, ex, ey, INK); line(ex, ey, ex + ARM * sin(J.ua1 + 1.6), ey + ARM * cos(J.ua1 + 1.6), INK) local cx, cy = floor(sx + 1.5), floor(sy - 3.5) rect(cx - 1, cy - 3, 3, 1, INK[1], INK[2], INK[3]) rect(cx - 2, cy - 2, 5, 1, BAND[1], BAND[2], BAND[3]) rect(cx - 3, cy - 1, 7, 3, INK[1], INK[2], INK[3]) rect(cx - 2, cy + 2, 5, 1, INK[1], INK[2], INK[3]) rect(cx - 1, cy + 3, 3, 1, INK[1], INK[2], INK[3]) px(cx + 2, cy, 250, 250, 250) local fl = (t // 3) % 2 px(cx - 3, cy - 2, BAND[1], BAND[2], BAND[3]) px(cx - 4, cy - 2 + fl, BAND[1], BAND[2], BAND[3]) px(cx - 5, cy - 1 - fl, BAND[1], BAND[2], BAND[3]) end local GAP = 180 local function obstacle(n) local h = hash32(n * 0x9E3779B1 + 77) return n * GAP + umod(h, 60), umod(h >> 8, 2) end local function draw_obstacle(x, kind) if kind == 0 then rect(x, GROUND - 7, 1, 8, 90, 90, 100) rect(x + 6, GROUND - 7, 1, 8, 90, 90, 100) rect(x, GROUND - 8, 7, 2, 240, 240, 240) rect(x + 2, GROUND - 8, 2, 2, 220, 50, 50) rect(x + 6, GROUND - 8, 1, 2, 220, 50, 50) else rect(x, GROUND - 6, 7, 7, 168, 112, 60) rect(x, GROUND - 6, 7, 1, 204, 150, 90) rect(x, GROUND - 6, 1, 7, 120, 78, 40); rect(x + 6, GROUND - 6, 1, 7, 120, 78, 40) rect(x + 1, GROUND - 3, 5, 1, 120, 78, 40) end end function on_settings() local c = cfg.band or { r = 232, g = 60, b = 60 } BAND = { c.r, c.g, c.b } end function render() if not BG then build_bg() end local t = frame * STEP_HZ // FPS gfx.blit_rgb(BG, 0, 0, W, H) local hs = t % W gfx.blit_rgb(HILLS, -hs, 28, W, 22, KEY[1], KEY[2], KEY[3]) gfx.blit_rgb(HILLS, W - hs, 28, W, 22, KEY[1], KEY[2], KEY[3]) local G = 2 * t local rs = G % W gfx.blit_rgb(ROAD, -rs, GROUND - 2, W, H - GROUND + 2) gfx.blit_rgb(ROAD, W - rs, GROUND - 2, W, H - GROUND + 2) local lift, air = 0, false if cfg.jumps ~= false then local n0 = (G - 40) // GAP for n = n0, n0 + 2 do if n >= 1 then local wx, kind = obstacle(n) local x = wx - G if x > -8 and x < W then draw_obstacle(x, kind) end local d = x + 3 - RX if d > -14 and d < 14 then local k = d / 14 lift = 11 * (1 - k * k); air = lift > 2 end end end end draw_runner(t, lift, air) end