-- title: Thẻ từ -- blurb: Ôn từ vựng: chạm thẻ để lật, Biết / Chưa biết -- Flashcards. Edit CARDS below to make your own deck. Progress (which cards you know) is saved. local CARDS = { {"apple", "quả táo"}, {"library", "thư viện"}, {"borrow", "mượn"}, {"journey", "hành trình"}, {"patience", "sự kiên nhẫn"}, {"whisper", "thì thầm"}, } local known = {} -- known[i] = true local cur = 1 local flipped = false local function load_known() local s = data.load() or "" for i in s:gmatch("%d+") do known[tonumber(i)] = true end end local function save_known() local t = {} for i = 1, #CARDS do if known[i] then t[#t + 1] = tostring(i) end end data.save(table.concat(t, " ")) end local function count_known() local n = 0 for i = 1, #CARDS do if known[i] then n = n + 1 end end return n end -- the next card not known yet, after cur (wraps); nil when every card is known local function next_card() for step = 1, #CARDS do local i = (cur + step - 1) % #CARDS + 1 if not known[i] then return i end end return nil end function open() load_known() cur = #CARDS cur = next_card() or 1 -- the first card not known yet end function draw() gfx.text(24, gfx.top + 10, "Đã thuộc " .. count_known() .. " / " .. #CARDS, 24) if count_known() == #CARDS then gfx.text(24, 400, "Thuộc hết rồi!", 48, true) gfx.button(24, 700, 710, 90, "Học lại từ đầu", 32) return end gfx.rect(24, 220, 710, 380, 3) local word = flipped and CARDS[cur][2] or CARDS[cur][1] gfx.text(60, 370, word, 56, true) gfx.text(60, 540, flipped and "(nghĩa)" or "chạm thẻ để xem nghĩa", 22) gfx.button(24, 700, 340, 90, "Chưa biết", 32) gfx.button(394, 700, 340, 90, "Biết", 32, true) end function tap(x, y) if count_known() == #CARDS then if y >= 700 and y < 790 then known = {}; save_known(); cur = 1; flipped = false; return true end return false end if y >= 220 and y < 600 then flipped = not flipped; return true end if y >= 700 and y < 790 then if x >= 394 then known[cur] = true; save_known() end cur = next_card() or cur flipped = false return true end return false end function key(k) if k == "select" then flipped = not flipped; return true end return false end