-- title: Xúc xắc -- blurb: Gieo một hoặc hai con xúc xắc -- Dice: draws the pips with gfx.circle. Tap anywhere above the buttons to roll. local n = 2 local faces = {1, 1} -- pip positions of each face on a 3x3 grid (column, row) local PIPS = { {{2, 2}}, {{1, 1}, {3, 3}}, {{1, 1}, {2, 2}, {3, 3}}, {{1, 1}, {3, 1}, {1, 3}, {3, 3}}, {{1, 1}, {3, 1}, {2, 2}, {1, 3}, {3, 3}}, {{1, 1}, {3, 1}, {1, 2}, {3, 2}, {1, 3}, {3, 3}}, } local function die(x, y, size, face) gfx.rect(x, y, size, size, 5) local step = size // 4 for _, p in ipairs(PIPS[face]) do gfx.circle(x + p[1] * step, y + p[2] * step, size // 11, 0) end end local function roll() for i = 1, n do faces[i] = math.random(1, 6) end end function open() roll() end function draw() if n == 1 then die(229, 250, 300, faces[1]) else die(54, 300, 300, faces[1]) die(404, 300, 300, faces[2]) gfx.text(24, 660, "Tổng: " .. (faces[1] + faces[2]), 36, true) end gfx.button(24, 760, 340, 90, "1 con", 30, n == 1) gfx.button(394, 760, 340, 90, "2 con", 30, n == 2) end function tap(x, y) if y >= 760 and y < 850 then n = x < 380 and 1 or 2 end roll() return true end function key(k) if k == "select" then roll(); return true end return false end