zamkamec

Lockscreen

Sep 24th, 2026
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. -- Pocket Computer Pattern Lock Screen (26 x 20) with Clock & Day/Night Tracker
  2. local w, h = term.getSize()
  3.  
  4. -- Target correct sequence
  5. local correctPattern = {3, 6, 5, 8, 9}
  6. local maxAttempts = 7
  7.  
  8. -- Grid positioned in the lower third (rows 12, 15, 18)
  9. local dots = {
  10. [1] = {x = 7, y = 12}, [2] = {x = 13, y = 12}, [3] = {x = 19, y = 12},
  11. [4] = {x = 7, y = 15}, [5] = {x = 13, y = 15}, [6] = {x = 19, y = 15},
  12. [7] = {x = 7, y = 18}, [8] = {x = 13, y = 18}, [9] = {x = 19, y = 18}
  13. }
  14.  
  15. local enteredPattern = {}
  16. local totalClicks = 0
  17. local message = "Draw Pattern"
  18. local msgColor = colors.white
  19.  
  20. -- Calculate Minecraft cycle stats (Matches Clock App logic)
  21. local function getDayNightStats()
  22. local timeTicks = os.time("ingame") * 1000 -- Convert to 0..24000 ticks
  23. local isDay = timeTicks >= 0 and timeTicks < 12000
  24.  
  25. local ticksRemaining = 0
  26. if isDay then
  27. ticksRemaining = 12000 - timeTicks
  28. else
  29. if timeTicks >= 12000 then
  30. ticksRemaining = 24000 - timeTicks
  31. else
  32. ticksRemaining = -timeTicks
  33. end
  34. end
  35.  
  36. -- 20 ticks = 1 real second
  37. local realSeconds = math.floor(ticksRemaining / 20)
  38. local mins = math.floor(realSeconds / 60)
  39. local secs = realSeconds % 60
  40.  
  41. return isDay, mins, secs
  42. end
  43.  
  44. -- Helper to draw connecting line between two dots
  45. local function drawLine(d1, d2, color)
  46. local x1, y1 = d1.x, d1.y
  47. local x2, y2 = d2.x, d2.y
  48.  
  49. local dx = math.abs(x2 - x1)
  50. local dy = math.abs(y2 - y1)
  51. local sx = (x1 < x2) and 1 or -1
  52. local sy = (y1 < y2) and 1 or -1
  53. local err = dx - dy
  54.  
  55. term.setBackgroundColor(color)
  56. while true do
  57. term.setCursorPos(x1, y1)
  58. term.write(" ")
  59. if x1 == x2 and y1 == y2 then break end
  60. local e2 = 2 * err
  61. if e2 > -dy then
  62. err = err - dy
  63. x1 = x1 + sx
  64. end
  65. if e2 < dx then
  66. err = err + dx
  67. y1 = y1 + sy
  68. end
  69. end
  70. end
  71.  
  72. -- Draw the UI layout
  73. local function drawUI()
  74. term.setBackgroundColor(colors.black)
  75. term.clear()
  76.  
  77. -- 1. Upper Section: Time & Cycle Info
  78. local isDay, mins, secs = getDayNightStats()
  79. local timeStr = textutils.formatTime(os.time(), true)
  80.  
  81. -- Current Time
  82. term.setCursorPos(math.floor((w - #timeStr) / 2) + 1, 2)
  83. term.setTextColor(colors.yellow)
  84. term.setBackgroundColor(colors.black)
  85. term.write(timeStr)
  86.  
  87. -- Day/Night Status Indicator
  88. local statusStr = isDay and "[ Day ]" or "[ Night ]"
  89. term.setCursorPos(math.floor((w - #statusStr) / 2) + 1, 3)
  90. term.setTextColor(isDay and colors.orange or colors.purple)
  91. term.write(statusStr)
  92.  
  93. -- Next Cycle Countdown
  94. local nextEvent = isDay and "Sunset" or "Sunrise"
  95. local cycleStr = string.format("%s in %dm %ds", nextEvent, mins, secs)
  96. term.setCursorPos(math.floor((w - #cycleStr) / 2) + 1, 4)
  97. term.setTextColor(colors.lightGray)
  98. term.write(cycleStr)
  99.  
  100. -- Status / Hint Message (Between Clock Header and Pattern Grid)
  101. term.setCursorPos(math.floor((w - #message) / 2) + 1, 7)
  102. term.setTextColor(msgColor)
  103. term.setBackgroundColor(colors.black)
  104. term.write(message)
  105.  
  106. -- 2. Lower Third: Connecting Lines
  107. for i = 1, #enteredPattern - 1 do
  108. local p1 = dots[enteredPattern[i]]
  109. local p2 = dots[enteredPattern[i + 1]]
  110. drawLine(p1, p2, colors.white)
  111. end
  112.  
  113. -- 3. Lower Third: 9 Dots
  114. for id, dot in ipairs(dots) do
  115. local isSelected = false
  116. for _, sel in ipairs(enteredPattern) do
  117. if sel == id then isSelected = true break end
  118. end
  119.  
  120. term.setBackgroundColor(isSelected and colors.cyan or colors.white)
  121. term.setCursorPos(dot.x, dot.y)
  122. term.write(" ")
  123. end
  124. end
  125.  
  126. -- Validate sequence matches key table
  127. local function checkPattern()
  128. if #enteredPattern ~= #correctPattern then return false end
  129. for i = 1, #correctPattern do
  130. if enteredPattern[i] ~= correctPattern[i] then return false end
  131. end
  132. return true
  133. end
  134.  
  135. -- Main Event Loop with Timer for Real-Time Updates
  136. local timer = os.startTimer(1)
  137.  
  138. while true do
  139. drawUI()
  140.  
  141. local event, p1, p2, p3 = os.pullEvent()
  142.  
  143. -- Update Clock & Countdown every second
  144. if event == "timer" and p1 == timer then
  145. timer = os.startTimer(1)
  146.  
  147. -- Touch / Click Handler
  148. elseif event == "mouse_click" and p1 == 1 then
  149. local clickX, clickY = p2, p3
  150.  
  151. for id, dot in ipairs(dots) do
  152. if clickX >= (dot.x - 1) and clickX <= (dot.x + 1) and clickY >= (dot.y - 1) and clickY <= (dot.y + 1) then
  153.  
  154. local alreadyClicked = false
  155. for _, sel in ipairs(enteredPattern) do
  156. if sel == id then alreadyClicked = true break end
  157. end
  158.  
  159. if not alreadyClicked then
  160. table.insert(enteredPattern, id)
  161. totalClicks = totalClicks + 1
  162.  
  163. if checkPattern() then
  164. drawUI()
  165. message = "Unlocked!"
  166. msgColor = colors.lime
  167. drawUI()
  168. sleep(0.8)
  169. term.setBackgroundColor(colors.black)
  170. term.clear()
  171. term.setCursorPos(1, 1)
  172.  
  173. -- Launch App Launcher
  174. if fs.exists("appLauncher.lua") then
  175. shell.run("appLauncher.lua")
  176. else
  177. print("Error: appLauncher.lua not found!")
  178. end
  179. return
  180. end
  181.  
  182. if totalClicks >= maxAttempts then
  183. drawUI()
  184. message = "Wrong Pattern!"
  185. msgColor = colors.red
  186. drawUI()
  187. sleep(1.2)
  188.  
  189. enteredPattern = {}
  190. totalClicks = 0
  191. message = "Draw Pattern"
  192. msgColor = colors.white
  193. end
  194. end
  195. break
  196. end
  197. end
  198. end
  199. end
Advertisement
Add Comment
Please, Sign In to add comment