info = { type = "image", title = "Road race", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Three race cars overtake along a scenic road", api = 33, } local tr = ({ sk = { ["Speed"] = "Rýchlosť", ["Cars"] = "Autá", ["Back car colour"] = "Farba zadného auta" }, cs = { ["Speed"] = "Rychlost", ["Cars"] = "Auta", ["Back car colour"] = "Barva zadního auta" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Speed"), type = "int", min = 1, max = 3, default = 2 }, { key = "cars", label = T("Cars"), type = "int", min = 1, max = 3, default = 3 }, { key = "paint", label = T("Back car colour"), type = "color", default = "#D62C2C" }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt, abs, min = math.floor, math.sin, math.sqrt, math.abs, math.min local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local SKY, SKY_LO = { 146, 204, 246 }, { 180, 228, 250 } local SUN, SUN_HALO = { 255, 236, 130 }, { 206, 236, 252 } local MTN, MTN_D = { 122, 150, 210 }, { 96, 122, 188 } local SNOW = { 246, 250, 255 } local PINE, PINE_L = { 30, 106, 62 }, { 54, 144, 80 } local TRUNK = { 100, 66, 40 } local VERGE, VERGE_D = { 86, 178, 74 }, { 62, 146, 60 } local ROAD, ROAD_D = { 98, 102, 116 }, { 80, 84, 98 } local LINE = { 236, 236, 228 } local CURB = { 222, 52, 52 } local POST = { 244, 240, 228 } local KEY = { 255, 0, 255 } local TIRE, HUB = { 28, 28, 34 }, { 206, 206, 216 } local SKIN = { 244, 190, 150 } local DARK = { 40, 34, 40 } local function c3(c) return c[1], c[2], c[3] end local function disc(cx, cy, r, c) for dy = -r, r do local dx = floor(sqrt(r * r - dy * dy) + 0.5) rect(cx - dx, cy + dy, 2 * dx + 1, 1, c3(c)) end end local function soft(x, y, w, h, c) local r, g, b = c[1], c[2], c[3] local x0 = floor(x) local f = x - x0 if f < 0.05 then rect(x0, y, w, h, r, g, b) return end if f > 0.95 then rect(x0 + 1, y, w, h, r, g, b) return end if w > 1 then rect(x0 + 1, y, w - 1, h, r, g, b) end blend_rect(x0, y, 1, h, r, g, b, 1 - f) blend_rect(x0 + w, y, 1, h, r, g, b, f) end local BG, TREES, GROUND = nil, nil, nil local TREES_Y, GROUND_Y = 24, 34 local function mtn_top(x) return floor(34 - (8 + 12 * abs(sin(x * 0.11 + 0.4)) + 3 * abs(sin(x * 0.37))) + 0.5) end local function build_bg() rect(0, 0, W, H, c3(SKY)) rect(0, 24, W, 14, c3(SKY_LO)) for x = 0, W - 1, 2 do px(x, 23, c3(SKY_LO)) end disc(52, 8, 7, SUN_HALO) disc(52, 8, 4, SUN) for x = 0, W - 1 do local top = mtn_top(x) local c = mtn_top(x + 1) > top and MTN_D or MTN rect(x, top, 1, 36 - top, c3(c)) if top < 18 then rect(x, top, 1, min(3, 18 - top), c3(SNOW)) end end BG = gfx.snapshot(0, 0, W, H) end local TR = { 4, 14, 14, 12, 26, 16, 38, 12, 48, 15, 58, 11 } local function build_trees() rect(0, TREES_Y, W, 16, c3(KEY)) for k = 1, #TR, 2 do for o = -W, W, W do local x, h = TR[k] + o, TR[k + 1] local top = 39 - h for r = 0, h - 3 do local half = r * 4 // (h - 2) + (r > 0 and 1 or 0) rect(x - half, top + r, 2 * half + 1, 1, c3(PINE)) end rect(x, top + 2, 1, h // 2, c3(PINE_L)) rect(x, top + h - 2, 1, 2, c3(TRUNK)) end end TREES = gfx.snapshot(0, TREES_Y, W, 16) end local function build_ground() rect(0, GROUND_Y, W, H - GROUND_Y, c3(KEY)) rect(0, 38, W, 6, c3(VERGE)) for i = 0, 7 do rect(i * 9 % W, 40 + i % 3, 3, 1, c3(VERGE_D)) end rect(0, 44, W, 20, c3(ROAD)) rect(0, 44, W, 1, c3(LINE)) for x = 0, W - 1, 8 do rect(x, 44, 4, 1, c3(CURB)) end for y = 54, 60, 6 do for x = 0, W - 1, 16 do rect(x, y, 8, 1, c3(LINE)) end end for i = 0, 4 do rect(i * 14 % W, 47 + i * 3, 6, 1, c3(ROAD_D)) end for x = 6, 38, 32 do rect(x, 35, 2, 9, c3(POST)); rect(x, 35, 2, 2, c3(CURB)) end GROUND = gfx.snapshot(0, GROUND_Y, W, H - GROUND_Y) end local CARS = { { c = { 214, 44, 44 }, cl = { 246, 108, 96 }, cd = { 150, 24, 32 }, helm = { 246, 246, 250 }, suit = { 40, 60, 140 }, scarf = { 255, 220, 60 }, yb = 51, x0 = 22, a = 22, p = 11.0, ph = 0.0 }, { c = { 250, 204, 40 }, cl = { 255, 238, 130 }, cd = { 190, 140, 20 }, helm = { 40, 70, 190 }, suit = { 40, 40, 48 }, scarf = { 240, 80, 60 }, yb = 57, x0 = 20, a = 24, p = 8.5, ph = 2.1 }, { c = { 36, 148, 214 }, cl = { 130, 206, 252 }, cd = { 20, 92, 150 }, helm = { 255, 150, 40 }, suit = { 250, 250, 250 }, scarf = { 255, 255, 255 }, yb = 63, x0 = 22, a = 20, p = 14.0, ph = 4.0 }, } local cx, cy = 0, 0 local function P(ox, oy, w, h, c) soft(cx + ox, cy + oy, w, h, c) end local SPOKE = { 1, 0, 0, 1, -1, 0, 0, -1 } local function draw_car(car, x, t, k) cx, cy = x, car.yb local yb = cy local off = floor(0.5 + 0.5 * sin(t * 0.32 + k * 2)) local C = car.c blend_rect(floor(x) + 1, yb - 1, 17, 1, 20, 24, 30, 0.35) P(0, -6 + off, 18, 2, C) P(0, -4 + off, 18, 1, car.cd) P(10, -7 + off, 7, 1, car.cl) P(0, -8 + off, 3, 2, DARK) P(5, -8 + off, 5, 2, car.suit) P(6, -10 + off, 4, 2, car.helm) P(10, -9 + off, 1, 1, SKIN) local wag = (t // 3 + k) % 2 P(2 - wag, -9 + off - wag, 4 + wag, 1, car.scarf) local s = (t // 2) % 4 for w = 0, 1 do local ox = 1 + w * 12 P(ox + 1, -5, 3, 1, TIRE); P(ox, -4, 5, 3, TIRE); P(ox + 1, -1, 3, 1, TIRE) P(ox + 2 + SPOKE[s * 2 + 1], -3 + SPOKE[s * 2 + 2], 1, 1, HUB) end for q = 0, 2 do local u = (t / (0.5 * STEP_HZ) + q / 3) % 1 blend_rect(floor(x - 2 - u * 9), floor(yb - 5 - u * 3 + off), 2, 2, 236, 236, 240, 0.55 * (1 - u)) end end function on_settings() local c = cfg.paint or { r = 214, g = 44, b = 44 } local car = CARS[1] car.c[1], car.c[2], car.c[3] = c.r, c.g, c.b car.cl[1], car.cl[2], car.cl[3] = min(255, c.r + 50), min(255, c.g + 50), min(255, c.b + 50) car.cd[1], car.cd[2], car.cd[3] = c.r * 7 // 10, c.g * 7 // 10, c.b * 7 // 10 end local CLOUDS = { { 5, 16, 2.0, 12 }, { 13, 12, -1.2, 44 } } function render() if not BG then build_bg(); build_trees(); build_ground() end local t = frame * STEP_HZ // FPS local sp = cfg.speed or 2 gfx.blit_rgb(BG, 0, 0, W, H) for i = 1, #CLOUDS do local c = CLOUDS[i] local y, L = c[1], c[2] local x = floor((c[4] + t * c[3] / STEP_HZ) % (W + L) - L + 0.5) blend_rect(x + 3, y, L - 6, 1, 255, 255, 255, 0.85) blend_rect(x, y + 1, L, 2, 255, 255, 255, 0.85) blend_rect(x + 2, y + 3, L - 6, 1, 255, 255, 255, 0.6) end local o = (t * ((sp + 1) // 2)) % W gfx.blit_rgb(TREES, -o, TREES_Y, W, 16, KEY[1], KEY[2], KEY[3]) gfx.blit_rgb(TREES, W - o, TREES_Y, W, 16, KEY[1], KEY[2], KEY[3]) o = (t * (sp + 2)) % W gfx.blit_rgb(GROUND, -o, GROUND_Y, W, H - GROUND_Y, KEY[1], KEY[2], KEY[3]) gfx.blit_rgb(GROUND, W - o, GROUND_Y, W, H - GROUND_Y, KEY[1], KEY[2], KEY[3]) for k = 1, cfg.cars or 3 do local car = CARS[k] local x = car.x0 + car.a * sin(TAU * t / (car.p * STEP_HZ) + car.ph) - 3 draw_car(car, x, t, k) end end