info = { type = "app", title = "Tetris", author = "BRRRO", license = "MIT", desc = "Tilt the frame like a tray and play", category = "game", api = 32, } permissions = { "tilt" } txt = { score = "SCORE", level = "LEVEL", next = "NEXT", over = "GAME OVER", start = "TILT TO PLAY", speed = "Speed", speed_h = "random = changes by itself", speed_o = { "random", "slow", "medium", "fast" }, colors = "Colours", colors_h = "original = classic piece colours", colors_o = { "original", "muted", "random", "lava", "turquoise", "pink", "green", "purple", "blue", "gold", "red" }, panel = "Side panel", panel_h = "next piece and score" } for k, v in pairs(({ sk = { score = "SKÓRE", level = "ÚROVEŇ", next = "ĎALŠÍ", over = "KONIEC HRY", start = "NAKLOŇ A HRAJ", speed = "Rýchlosť", speed_h = "náhodná = mení sa sama", speed_o = { "náhodná", "pomalá", "stredná", "rýchla" }, colors = "Farby", colors_h = "pôvodné = klasické farby kameňov", colors_o = { "pôvodné", "tlmené", "náhodné", "lávové", "tyrkysové", "ružové", "zelené", "fialové", "modré", "zlaté", "červené" }, panel = "Bočný panel", panel_h = "ďalší kameň a skóre" }, cs = { score = "SKÓRE", level = "ÚROVEŇ", next = "DALŠÍ", over = "KONEC HRY", start = "NAKLOŇ A HREJ", speed = "Rychlost", speed_h = "náhodná = mění se sama", speed_o = { "náhodná", "pomalá", "střední", "rychlá" }, colors = "Barvy", colors_h = "původní = klasické barvy kostek", colors_o = { "původní", "tlumené", "náhodné", "lávové", "tyrkysové", "růžové", "zelené", "fialové", "modré", "zlaté", "červené" }, panel = "Boční panel", panel_h = "další kostka a skóre" }, })[lang] or {}) do txt[k] = v end settings = { { key = "speed", type = "int", default = "medium", label = txt.speed, hint = txt.speed_h, opts = { keys = { "random", "slow", "medium", "fast" }, table.unpack(txt.speed_o) } }, { key = "colors", type = "int", default = "original", label = txt.colors, hint = txt.colors_h, opts = { keys = { "original", "muted", "random", "lava", "turquoise", "pink", "green", "purple", "blue", "gold", "red" }, table.unpack(txt.colors_o) } }, { key = "panel", type = "bool", default = true, label = txt.panel, hint = txt.panel_h }, } local px, rect, blit = gfx.px, gfx.rect, gfx.blit_indexed local floor, abs, char, concat, unpack = math.floor, math.abs, string.char, table.concat, table.unpack local rnd = math.random local W, H = gfx.W, gfx.H local FPS = gfx.FPS local CELL, ROWS = 3, 20 local COLS_PANEL, COLS_FULL = 10, 20 local cols = COLS_PANEL local BX, BY = 1, (H - ROWS * CELL) // 2 local PANX = 1 + COLS_PANEL * CELL + 2 local PANW = W - PANX local O_LBL = { font = "3x5", w = PANW, align = "center", pad = 0 } local O_NUM = { w = PANW, align = "center", fit = "4x6" } local TL = { move = 0.20, move_rl = 0.12, soft = 0.33, soft_rl = 0.20, rot = 0.30, rot_rl = 0.18, } local Y_ARM = floor(FPS * 0.20 + 0.5) local DAS = floor(FPS * 0.25 + 0.5) local ARR = floor(FPS * 0.10 + 0.5) local SOFT_IV = floor(FPS * 0.067 + 0.5) local LOCK_F = floor(FPS * 0.35 + 0.5) local LOCK_MAX = 6 local CLEAR_F = floor(FPS * 0.33 + 0.5) local OVER_F = floor(FPS * 4.50 + 0.5) local AI_NOISE = 0.35 local AI_SLIP = 7 local WAKE_D = 0.08 local SETTLE_F = floor(FPS * 1.2 + 0.5) local SETTLE_A = 0.15 local REF_A = 1.0 / (2.5 * FPS) local REST_A = 0.6 / FPS local DROP_S = { 0.50, 0.33, 0.20 } local MOVE_S = { 0.23, 0.17, 0.10 } local LINE_VAL = { 100, 300, 500, 800 } local OWN = { { 40, 225, 255 }, { 255, 215, 50 }, { 190, 85, 255 }, { 60, 230, 95 }, { 255, 70, 70 }, { 60, 120, 255 }, { 255, 150, 30 }, } local MUTED = {} for i, c in ipairs(OWN) do local l = 0.30 * c[1] + 0.59 * c[2] + 0.11 * c[3] MUTED[i] = { floor((c[1] + l) / 2), floor((c[2] + l) / 2), floor((c[3] + l) / 2) } end local PALS = { { 255, 105, 25, 255, 205, 70 }, { 40, 215, 255, 150, 255, 235 }, { 255, 85, 180, 255, 175, 230 }, { 60, 235, 90, 180, 255, 140 }, { 150, 80, 255, 215, 170, 255 }, { 45, 120, 255, 130, 205, 255 }, { 255, 170, 20, 255, 235, 120 }, { 255, 55, 45, 255, 150, 90 }, } local PAL_N = #PALS local PAL_AT = { lava = 1, turquoise = 2, pink = 3, green = 4, purple = 5, blue = 6, gold = 7, red = 8 } local EDGE = { 78, 88, 138 } local LBL = { 120, 135, 175 } local NUM = { 225, 235, 255 } local DEAD = { 64, 72, 96 } local BASE = { 0x0F00, 0x0660, 0x04E0, 0x06C0, 0x0C60, 0x08E0, 0x02E0 } local SHP, PCL, PRW, LIM, UNI = {}, {}, {}, {}, {} local function build_shapes() for p = 1, 7 do SHP[p], PCL[p], PRW[p], LIM[p], UNI[p] = {}, {}, {}, {}, {} local m, seen = BASE[p], {} for r = 0, 3 do UNI[p][r] = not seen[m] seen[m] = true local s, colt, rowt = {}, {}, {} local minx, maxx, miny = 9, -9, 9 for y = 0, 3 do for x = 0, 3 do if (m >> (15 - (y * 4 + x))) & 1 == 1 then s[#s + 1] = x; s[#s + 1] = y if x < minx then minx = x end if x > maxx then maxx = x end if y < miny then miny = y end local c = colt[x] if c then if y < c[1] then c[1] = y end if y > c[2] then c[2] = y end c[3] = c[3] + 1 else colt[x] = { y, y, 1 } end rowt[y] = (rowt[y] or 0) + 1 end end end SHP[p][r] = s LIM[p][r] = { minx, maxx, miny } local pc = {} for x = 0, 3 do local c = colt[x] if c then pc[#pc + 1] = x; pc[#pc + 1] = c[1]; pc[#pc + 1] = c[2]; pc[#pc + 1] = c[3] end end PCL[p][r] = pc local pr = {} for y = 0, 3 do if rowt[y] then pr[#pr + 1] = y; pr[#pr + 1] = rowt[y] end end PRW[p][r] = pr local o = 0 for y = 0, 3 do for x = 0, 3 do if (m >> (15 - ((3 - x) * 4 + y))) & 1 == 1 then o = o | (1 << (15 - (y * 4 + x))) end end end m = o end end end build_shapes() local grid, gstr = {}, "" local gdirty = true local p_cur, p_nxt, p_rot, p_x, p_y = 1, 1, 0, 0, 0 local ghost_y = 0 local score, lines, level = 0, 0, 1 local phase = "play" local mode = "ai" local fall_t, lock_t, lock_n = 0, 0, 0 local clear_t, clear_rows = 0, {} local over_t = 0 local tgt_x, tgt_rot, ai_t = 0, 0, 0 local pop_t, pop_s, pop_y = 99999, "", 0 local tet_t, lvl_t = 99999, 99999 local ref_x, ref_y = 0, 0 local rest_x, rest_y = 0, 0 local seeded = false local settle_t = 0 local move_dir, move_t = 0, 0 local rot_hold, rot_t, rot_done = false, 0, false local soft, soft_t = false, 0 local dx_now, dy_now = 0, 0 local PC = { {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} } local PAL = {} for i = 1, 27 do PAL[i] = 0 end local p_from, p_to, p_t, p_hold = 1, 1, 1.0, 0 local A_FADE = 1 / (5 * FPS) local A_HOLD_MIN, A_HOLD_SPAN = FPS * 30, FPS * 60 local a_cur, a_tgt, a_hold = 1.0, 1.0, 0 local SP_HOLD_MIN, SP_HOLD_SPAN = FPS * 25, FPS * 60 local SP_A = 1 / (10 * FPS) local LVL = { random = 0, slow = 1, medium = 2, fast = 3 } local function auto_level(opt, n) if opt > 0 and opt <= n then a_cur, a_tgt, a_hold = opt - 1, opt - 1, 0; return a_cur end if a_hold == 0 then local sp = n - 1 for i = 1, 8 do local t = rnd() * sp local d = t - a_tgt if d < 0 then d = -d end if d >= sp * 0.30 then a_tgt = t; break end if i == 8 then a_tgt = sp - a_tgt end end a_hold = SP_HOLD_MIN + rnd(0, SP_HOLD_SPAN - 1) end a_hold = a_hold - 1 a_cur = a_cur + (a_tgt - a_cur) * SP_A return a_cur end local function amb_lerp(v, n, t) if t <= 0 then return v[1] end if t >= n - 1 then return v[n] end local i = floor(t) return v[i + 1] + (v[i + 2] - v[i + 1]) * (t - i) end local function shades(lo_r, lo_g, lo_b, hi_r, hi_g, hi_b) for i = 1, 7 do local k = (i - 1) / 6.0 local c = PC[i] c[1] = lo_r + (hi_r - lo_r) * k c[2] = lo_g + (hi_g - lo_g) * k c[3] = lo_b + (hi_b - lo_b) * k end end local function apply_pal(dead) for i = 1, 7 do local c = PC[i] local o = i * 3 if dead then PAL[o + 1] = floor(c[1] * 0.42 + DEAD[1] * 0.58) PAL[o + 2] = floor(c[2] * 0.42 + DEAD[2] * 0.58) PAL[o + 3] = floor(c[3] * 0.42 + DEAD[3] * 0.58) else PAL[o + 1] = floor(c[1]); PAL[o + 2] = floor(c[2]); PAL[o + 3] = floor(c[3]) end end end local function step_colors() local opt = cfg.colors or "original" if opt == "original" or opt == "muted" then local src = opt == "muted" and MUTED or OWN for i = 1, 7 do local c, o = PC[i], src[i] c[1] = o[1]; c[2] = o[2]; c[3] = o[3] end else local fixed = PAL_AT[opt] if fixed then local p = PALS[fixed] shades(p[1], p[2], p[3], p[4], p[5], p[6]) else if p_t >= 1.0 then if p_hold > 0 then p_hold = p_hold - 1 else p_from = p_to local n repeat n = rnd(PAL_N) until n ~= p_from p_to, p_t = n, 0.0 end end if p_t < 1.0 then p_t = p_t + A_FADE if p_t > 1.0 then p_t = 1.0; p_hold = A_HOLD_MIN + rnd(0, A_HOLD_SPAN - 1) end end local f, t = PALS[p_from], PALS[p_to] shades(f[1] + (t[1] - f[1]) * p_t, f[2] + (t[2] - f[2]) * p_t, f[3] + (t[3] - f[3]) * p_t, f[4] + (t[4] - f[4]) * p_t, f[5] + (t[5] - f[5]) * p_t, f[6] + (t[6] - f[6]) * p_t) end end apply_pal(phase == "over") end local function rebuild_gstr() local rows, row = {}, {} for y = 0, ROWS - 1 do local b = y * cols for x = 1, cols do row[x] = grid[b + x] end rows[y + 1] = char(unpack(row, 1, cols)) end gstr = concat(rows) gdirty = false end local function collide(p, r, ox, oy) local s = SHP[p][r] for i = 1, #s, 2 do local gx, gy = ox + s[i], oy + s[i + 1] if gx < 0 or gx >= cols or gy >= ROWS then return true end if gy >= 0 and grid[gy * cols + gx + 1] ~= 0 then return true end end return false end local function calc_ghost() local g = p_y while not collide(p_cur, p_rot, p_x, g + 1) do g = g + 1 end ghost_y = g end local TR, HG, CN, RC, WH = {}, {}, {}, {}, {} local function scan_board() for c = 0, cols - 1 do TR[c] = ROWS; CN[c] = 0 end for y = 0, ROWS - 1 do local n, b = 0, y * cols for c = 0, cols - 1 do if grid[b + c + 1] ~= 0 then n = n + 1 if TR[c] == ROWS then TR[c] = y end CN[c] = CN[c] + 1 end end RC[y] = n end local holes = 0 for c = 0, cols - 1 do local h = ROWS - TR[c] HG[c] = h holes = holes + h - CN[c] end return holes end local function ai_plan() local holes0 = scan_board() local p = p_cur local best, bx, br = -1e9, p_x, p_rot local sec, sx, sr = -1e9, p_x, p_rot for r = 0, 3 do if UNI[p][r] then local pc, lim = PCL[p][r], LIM[p][r] for ox = -lim[1], cols - 1 - lim[2] do local oy = ROWS for i = 1, #pc, 4 do local t = TR[ox + pc[i]] - 1 - pc[i + 2] if t < oy then oy = t end end if oy + lim[3] >= 0 then for c = 0, cols - 1 do WH[c] = HG[c] end local hadd = 0 for i = 1, #pc, 4 do local c = ox + pc[i] hadd = hadd + TR[c] - 1 - (oy + pc[i + 2]) local nh = ROWS - (oy + pc[i + 1]) if nh > WH[c] then WH[c] = nh end end local full, pr = 0, PRW[p][r] for i = 1, #pr, 2 do local ry = oy + pr[i] if ry >= 0 and RC[ry] + pr[i + 1] == cols then full = full + 1 end end local agg, bump, prev = 0, 0, nil for c = 0, cols - 1 do local h = WH[c] agg = agg + h if prev then local d = h - prev if d < 0 then d = -d end bump = bump + d end prev = h end agg = agg - full * cols local s = -0.51 * agg + 0.76 * full - 0.36 * (holes0 + hadd) - 0.18 * bump + (2 * rnd() - 1) * AI_NOISE if s > best then sec, sx, sr = best, bx, br best, bx, br = s, ox, r elseif s > sec then sec, sx, sr = s, ox, r end end end end end if sec > -1e8 and rnd(100) <= AI_SLIP then bx, br = sx, sr end tgt_x, tgt_rot, ai_t = bx, br, 0 end local function try_move(d) if collide(p_cur, p_rot, p_x + d, p_y) then return false end p_x = p_x + d return true end local KICK = { 0, -1, 1, -2, 2 } local function try_rotate() local nr = (p_rot + 1) & 3 for i = 1, 5 do local k = KICK[i] if not collide(p_cur, nr, p_x + k, p_y) then p_x, p_rot = p_x + k, nr return true end end return false end local spawn local function new_game() for i = 1, ROWS * cols do grid[i] = 0 end gdirty = true score, lines, level = 0, 0, 1 phase, over_t = "play", 0 pop_t, tet_t, lvl_t = 99999, 99999, 99999 clear_t, fall_t, lock_t, lock_n = 0, 0, 0, 0 move_dir, move_t, rot_hold = 0, 0, false soft, soft_t = false, 0 p_nxt = rnd(7) spawn() end spawn = function() p_cur, p_nxt = p_nxt, rnd(7) p_rot = 0 p_x = cols // 2 - 2 p_y = 0 fall_t, lock_t, lock_n = 0, 0, 0 if collide(p_cur, p_rot, p_x, p_y) then phase, over_t = "over", 0 return end calc_ghost() if mode == "ai" then ai_plan() end end local function lock_piece() local s = SHP[p_cur][p_rot] for i = 1, #s, 2 do grid[(p_y + s[i + 1]) * cols + p_x + s[i] + 1] = p_cur end gdirty = true local n = 0 for y = 0, ROWS - 1 do local b, fullr = y * cols, true for x = 1, cols do if grid[b + x] == 0 then fullr = false; break end end if fullr then n = n + 1; clear_rows[n] = y end end for i = n + 1, 4 do clear_rows[i] = nil end if n > 0 then local v = LINE_VAL[n] * level score = score + v lines = lines + n if 1 + lines // 10 > level then lvl_t = 0 end level = 1 + lines // 10 phase, clear_t = "clear", 0 pop_t, pop_s = 0, "+" .. v pop_y = BY + (clear_rows[1] + clear_rows[n]) * CELL // 2 if n == 4 then tet_t = 0 end else spawn() end end local function step_clear() clear_t = clear_t + 1 if clear_t < CLEAR_F then return end for i = 1, #clear_rows do local cy = clear_rows[i] for y = cy, 1, -1 do local d, s2 = y * cols, (y - 1) * cols for x = 1, cols do grid[d + x] = grid[s2 + x] end end for x = 1, cols do grid[x] = 0 end end gdirty = true phase = "play" spawn() end local function step_hand() local t = data.tilt if not t or not t.ok or frame == 1 then if mode ~= "ai" then mode = "ai" if phase == "play" then ai_plan() end end settle_t, seeded = 0, false dx_now, dy_now = 0, 0 return "ai" end local tx, ty = t.tray_x or 0, t.tray_y or 0 if not seeded then seeded = true ref_x, ref_y, rest_x, rest_y = tx, ty, tx, ty end if mode == "ai" then if t.still then rest_x = rest_x + (tx - rest_x) * REST_A rest_y = rest_y + (ty - rest_y) * REST_A end if abs(tx - rest_x) + abs(ty - rest_y) > WAKE_D then mode, settle_t = "human", SETTLE_F ref_x, ref_y = tx, ty move_dir, move_t, rot_hold = 0, 0, false soft, soft_t = false, 0 end else if t.still then mode, settle_t = "ai", 0 rest_x, rest_y = tx, ty if phase == "play" then ai_plan() end elseif settle_t > 0 then settle_t = settle_t - 1 ref_x = ref_x + (tx - ref_x) * SETTLE_A ref_y = ref_y + (ty - ref_y) * SETTLE_A else if move_dir == 0 then ref_x = ref_x + (tx - ref_x) * REF_A end if not soft and not rot_hold then ref_y = ref_y + (ty - ref_y) * REF_A end end end dx_now, dy_now = tx - ref_x, ty - ref_y if mode == "ai" then return "ai" end return settle_t > 0 and "wait" or "human" end local function step_input() local dx, dy = dx_now, dy_now local acted = false local d = 0 if move_dir ~= 0 and ((move_dir > 0 and dx > TL.move_rl) or (move_dir < 0 and dx < -TL.move_rl)) then d = move_dir elseif dx > TL.move then d = 1 elseif dx < -TL.move then d = -1 end if d ~= move_dir then move_dir = d if d ~= 0 then acted = try_move(d); move_t = DAS end elseif d ~= 0 then move_t = move_t - 1 if move_t <= 0 then acted = try_move(d); move_t = ARR end end if (rot_hold and dy < -TL.rot_rl) or dy < -TL.rot then if not rot_hold then rot_hold, rot_t, rot_done = true, Y_ARM, false end if not rot_done then rot_t = rot_t - 1 if rot_t <= 0 then acted = try_rotate() or acted; rot_done = true end end else rot_hold = false end if soft then soft = dy > TL.soft_rl else soft_t = dy > TL.soft and soft_t + 1 or 0 if soft_t >= Y_ARM then soft = true end end if acted and lock_t > 0 and lock_n < LOCK_MAX then lock_t, lock_n = 0, lock_n + 1 end end local function step_ai(mv) ai_t = ai_t - 1 if ai_t > 0 then return end ai_t = mv if p_rot ~= tgt_rot then if not try_rotate() then tgt_rot = p_rot end elseif p_x ~= tgt_x then if not try_move(tgt_x > p_x and 1 or -1) then tgt_x = p_x end end end local function step_play(ctl, iv, mv) local human = ctl == "human" if human then step_input() elseif ctl == "ai" then step_ai(mv) end if collide(p_cur, p_rot, p_x, p_y + 1) then lock_t = lock_t + 1 local lim = (ctl == "ai" or soft) and 1 or LOCK_F if lock_t >= lim then lock_piece() end else lock_t = 0 fall_t = fall_t + 1 local step = iv if human then if soft then step = SOFT_IV end elseif ctl == "ai" and p_rot == tgt_rot and p_x == tgt_x then step = iv // 2 + 1 end if fall_t >= step then fall_t = 0 p_y = p_y + 1 if human and soft then score = score + 1 end end end if phase == "play" then calc_ghost() end end local function step_over(ctl) over_t = over_t + 1 if over_t == 1 then move_dir, move_t, rot_hold = 0, 0, false soft, soft_t = false, 0 end if over_t <= OVER_F then return end if ctl == "wait" then return end if ctl == "ai" then new_game(); return end if abs(dx_now) > TL.move or abs(dy_now) > TL.soft then new_game() end end function update() local lv = auto_level(LVL[cfg.speed] or 2, 3) local iv = floor(amb_lerp(DROP_S, 3, lv) * FPS * (0.88 ^ (level - 1)) + 0.5) if iv < 2 then iv = 2 end local mv = floor(amb_lerp(MOVE_S, 3, lv) * FPS + 0.5) if mv < 1 then mv = 1 end step_colors() pop_t, tet_t, lvl_t = pop_t + 1, tet_t + 1, lvl_t + 1 local ctl = step_hand() if phase == "over" then step_over(ctl) elseif phase == "clear" then step_clear() else step_play(ctl, iv, mv) end if gdirty then rebuild_gstr() end end local GOLD = { 255, 200, 60 } local POP_F, TET_F, LVL_F = FPS, floor(FPS * 0.8), floor(FPS * 1.2) local O_POP = { font = "4x6", w = COLS_PANEL * CELL, align = "center", outline = { 0, 0, 0 }, alpha = 1 } local function fade(c, t, len) if t >= len then return c[1], c[2], c[3] end local k = t / len return floor(GOLD[1] + (c[1] - GOLD[1]) * k), floor(GOLD[2] + (c[2] - GOLD[2]) * k), floor(GOLD[3] + (c[3] - GOLD[3]) * k) end local function draw_well() local w, h = cols * CELL, ROWS * CELL local r, g, b = fade(EDGE, tet_t, TET_F) rect(BX - 1, 0, 1, BY + h + 1, r, g, b) rect(BX + w, 0, 1, BY + h + 1, r, g, b) rect(BX - 1, BY + h, w + 2, 1, r, g, b) end local function draw_pop() if pop_t >= POP_F then return end local k = pop_t / POP_F O_POP.alpha = 1 - k * k gfx.text(BX, pop_y - 3 - floor(10 * k), pop_s, GOLD[1], GOLD[2], GOLD[3], O_POP) end local function draw_ghost() if ghost_y <= p_y then return end local s, c = SHP[p_cur][p_rot], PC[p_cur] local r, g, b = c[1], c[2], c[3] for i = 1, #s, 2 do local x, y = BX + (p_x + s[i]) * CELL, BY + (ghost_y + s[i + 1]) * CELL px(x, y, r, g, b) px(x + 2, y, r, g, b) px(x, y + 2, r, g, b) px(x + 2, y + 2, r, g, b) end end local function draw_piece() local c = PC[p_cur] local r, g, b = c[1], c[2], c[3] local hr, hg, hb = r + (255 - r) * 0.45, g + (255 - g) * 0.45, b + (255 - b) * 0.45 local s = SHP[p_cur][p_rot] for i = 1, #s, 2 do local x, y = BX + (p_x + s[i]) * CELL, BY + (p_y + s[i + 1]) * CELL rect(x, y, CELL, CELL, r, g, b) rect(x, y, CELL, 1, hr, hg, hb) end end local function draw_clear() local k = clear_t / CLEAR_F local hgt = CELL - floor(k * CELL) if hgt < 1 then hgt = 1 end local r = 255 - 120 * k local g = 255 - 110 * k local b = 255 - 55 * k local w = cols * CELL for i = 1, #clear_rows do local y = BY + clear_rows[i] * CELL rect(BX, y, w, CELL, 0, 0, 0) rect(BX, y + (CELL - hgt) // 2, w, hgt, r, g, b) end end local function draw_panel() local t = txt gfx.text(PANX, 1, t.score, LBL[1], LBL[2], LBL[3], O_LBL) gfx.text(PANX, 7, tostring(score), NUM[1], NUM[2], NUM[3], O_NUM) gfx.text(PANX, 17, t.level, LBL[1], LBL[2], LBL[3], O_LBL) local lr, lg, lb = fade(NUM, lvl_t, LVL_F) gfx.text(PANX, 23, tostring(level), lr, lg, lb, O_NUM) gfx.text(PANX, 33, t.next, LBL[1], LBL[2], LBL[3], O_LBL) local s, c, lm = SHP[p_nxt][0], PC[p_nxt], LIM[p_nxt][0] local x0 = PANX + (PANW - (lm[2] - lm[1] + 1) * CELL) // 2 - lm[1] * CELL for i = 1, #s, 2 do rect(x0 + s[i] * CELL, 40 + (s[i + 1] - 1) * CELL, CELL, CELL, c[1], c[2], c[3]) end local by = H - 9 if mode == "ai" then gfx.text(PANX, by + 1, "AI", LBL[1], LBL[2], LBL[3], O_LBL) else local bx = PANX + (PANW - 13) // 2 rect(bx, by, 13, 1, EDGE[1], EDGE[2], EDGE[3]) rect(bx, by + 8, 13, 1, EDGE[1], EDGE[2], EDGE[3]) rect(bx, by, 1, 9, EDGE[1], EDGE[2], EDGE[3]) rect(bx + 12, by, 1, 9, EDGE[1], EDGE[2], EDGE[3]) local gx = dx_now * 18 local gy = dy_now * 12 if gx < -5 then gx = -5 elseif gx > 5 then gx = 5 end if gy < -3 then gy = -3 elseif gy > 3 then gy = 3 end px(bx + 6 + floor(gx + 0.5), by + 4 + floor(gy + 0.5), NUM[1], NUM[2], NUM[3]) end end local O_OVER = { font = "6x10", fit = "4x6" } local O_SCORE = { font = "9x15b", fit = "6x10" } local O_HINT = { font = "4x6", fit = "3x5" } local OV_Y, OV_H = 15, 31 local OV_T, OV_S do local t0, t1 = gfx.text_ink(txt.over, O_OVER) local s0, s1 = gfx.text_ink("0", O_SCORE) local top = OV_Y + 1 + (OV_H - 2 - (t1 - t0 + 1) - 4 - (s1 - s0 + 1)) // 2 OV_T = top - t0 OV_S = top + (t1 - t0 + 1) + 4 - s0 end local function draw_over() rect(0, OV_Y, W, OV_H, 0, 0, 0) rect(0, OV_Y, W, 1, EDGE[1], EDGE[2], EDGE[3]) rect(0, OV_Y + OV_H - 1, W, 1, EDGE[1], EDGE[2], EDGE[3]) if over_t % (FPS * 2) < FPS * 1.5 then gfx.text_center(OV_T, txt.over, LBL[1], LBL[2], LBL[3], O_OVER) end gfx.text_center(OV_S, tostring(score), NUM[1], NUM[2], NUM[3], O_SCORE) if over_t > OVER_F and mode == "human" then rect(0, OV_Y + OV_H + 1, W, 8, 0, 0, 0) gfx.text_center(OV_Y + OV_H + 3, txt.start, LBL[1], LBL[2], LBL[3], O_HINT) end end function render() gfx.clear(0, 0, 0) draw_well() blit(gstr, PAL, BX, BY, cols * CELL, ROWS * CELL, cols, ROWS, true) if phase == "clear" then draw_clear() end if phase == "play" then draw_ghost(); draw_piece() end if phase ~= "over" then draw_pop() end if cfg.panel then draw_panel() end if phase == "over" then draw_over() end end function on_settings() local nc = cfg.panel and COLS_PANEL or COLS_FULL local w = nc * CELL BX = (nc == COLS_PANEL) and 1 or (W - w) // 2 O_POP.w = w if nc ~= cols or gstr == "" then cols = nc step_colors() new_game() rebuild_gstr() end end