Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Pocket Computer Pattern Lock Screen (26 x 20) with Clock & Day/Night Tracker
- local w, h = term.getSize()
- -- Target correct sequence
- local correctPattern = {3, 6, 5, 8, 9}
- local maxAttempts = 7
- -- Grid positioned in the lower third (rows 12, 15, 18)
- local dots = {
- [1] = {x = 7, y = 12}, [2] = {x = 13, y = 12}, [3] = {x = 19, y = 12},
- [4] = {x = 7, y = 15}, [5] = {x = 13, y = 15}, [6] = {x = 19, y = 15},
- [7] = {x = 7, y = 18}, [8] = {x = 13, y = 18}, [9] = {x = 19, y = 18}
- }
- local enteredPattern = {}
- local totalClicks = 0
- local message = "Draw Pattern"
- local msgColor = colors.white
- -- Calculate Minecraft cycle stats (Matches Clock App logic)
- local function getDayNightStats()
- local timeTicks = os.time("ingame") * 1000 -- Convert to 0..24000 ticks
- local isDay = timeTicks >= 0 and timeTicks < 12000
- local ticksRemaining = 0
- if isDay then
- ticksRemaining = 12000 - timeTicks
- else
- if timeTicks >= 12000 then
- ticksRemaining = 24000 - timeTicks
- else
- ticksRemaining = -timeTicks
- end
- end
- -- 20 ticks = 1 real second
- local realSeconds = math.floor(ticksRemaining / 20)
- local mins = math.floor(realSeconds / 60)
- local secs = realSeconds % 60
- return isDay, mins, secs
- end
- -- Helper to draw connecting line between two dots
- local function drawLine(d1, d2, color)
- local x1, y1 = d1.x, d1.y
- local x2, y2 = d2.x, d2.y
- local dx = math.abs(x2 - x1)
- local dy = math.abs(y2 - y1)
- local sx = (x1 < x2) and 1 or -1
- local sy = (y1 < y2) and 1 or -1
- local err = dx - dy
- term.setBackgroundColor(color)
- while true do
- term.setCursorPos(x1, y1)
- term.write(" ")
- if x1 == x2 and y1 == y2 then break end
- local e2 = 2 * err
- if e2 > -dy then
- err = err - dy
- x1 = x1 + sx
- end
- if e2 < dx then
- err = err + dx
- y1 = y1 + sy
- end
- end
- end
- -- Draw the UI layout
- local function drawUI()
- term.setBackgroundColor(colors.black)
- term.clear()
- -- 1. Upper Section: Time & Cycle Info
- local isDay, mins, secs = getDayNightStats()
- local timeStr = textutils.formatTime(os.time(), true)
- -- Current Time
- term.setCursorPos(math.floor((w - #timeStr) / 2) + 1, 2)
- term.setTextColor(colors.yellow)
- term.setBackgroundColor(colors.black)
- term.write(timeStr)
- -- Day/Night Status Indicator
- local statusStr = isDay and "[ Day ]" or "[ Night ]"
- term.setCursorPos(math.floor((w - #statusStr) / 2) + 1, 3)
- term.setTextColor(isDay and colors.orange or colors.purple)
- term.write(statusStr)
- -- Next Cycle Countdown
- local nextEvent = isDay and "Sunset" or "Sunrise"
- local cycleStr = string.format("%s in %dm %ds", nextEvent, mins, secs)
- term.setCursorPos(math.floor((w - #cycleStr) / 2) + 1, 4)
- term.setTextColor(colors.lightGray)
- term.write(cycleStr)
- -- Status / Hint Message (Between Clock Header and Pattern Grid)
- term.setCursorPos(math.floor((w - #message) / 2) + 1, 7)
- term.setTextColor(msgColor)
- term.setBackgroundColor(colors.black)
- term.write(message)
- -- 2. Lower Third: Connecting Lines
- for i = 1, #enteredPattern - 1 do
- local p1 = dots[enteredPattern[i]]
- local p2 = dots[enteredPattern[i + 1]]
- drawLine(p1, p2, colors.white)
- end
- -- 3. Lower Third: 9 Dots
- for id, dot in ipairs(dots) do
- local isSelected = false
- for _, sel in ipairs(enteredPattern) do
- if sel == id then isSelected = true break end
- end
- term.setBackgroundColor(isSelected and colors.cyan or colors.white)
- term.setCursorPos(dot.x, dot.y)
- term.write(" ")
- end
- end
- -- Validate sequence matches key table
- local function checkPattern()
- if #enteredPattern ~= #correctPattern then return false end
- for i = 1, #correctPattern do
- if enteredPattern[i] ~= correctPattern[i] then return false end
- end
- return true
- end
- -- Main Event Loop with Timer for Real-Time Updates
- local timer = os.startTimer(1)
- while true do
- drawUI()
- local event, p1, p2, p3 = os.pullEvent()
- -- Update Clock & Countdown every second
- if event == "timer" and p1 == timer then
- timer = os.startTimer(1)
- -- Touch / Click Handler
- elseif event == "mouse_click" and p1 == 1 then
- local clickX, clickY = p2, p3
- for id, dot in ipairs(dots) do
- if clickX >= (dot.x - 1) and clickX <= (dot.x + 1) and clickY >= (dot.y - 1) and clickY <= (dot.y + 1) then
- local alreadyClicked = false
- for _, sel in ipairs(enteredPattern) do
- if sel == id then alreadyClicked = true break end
- end
- if not alreadyClicked then
- table.insert(enteredPattern, id)
- totalClicks = totalClicks + 1
- if checkPattern() then
- drawUI()
- message = "Unlocked!"
- msgColor = colors.lime
- drawUI()
- sleep(0.8)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- -- Launch App Launcher
- if fs.exists("appLauncher.lua") then
- shell.run("appLauncher.lua")
- else
- print("Error: appLauncher.lua not found!")
- end
- return
- end
- if totalClicks >= maxAttempts then
- drawUI()
- message = "Wrong Pattern!"
- msgColor = colors.red
- drawUI()
- sleep(1.2)
- enteredPattern = {}
- totalClicks = 0
- message = "Draw Pattern"
- msgColor = colors.white
- end
- end
- break
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment