Advertisement
Guest User

code

a guest
Jan 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.11 KB | None | 0 0
  1. --[[
  2.     KeyPad v1.0
  3.     by Fwacer
  4.     May 29th, 2014
  5. ]]--
  6.  
  7. function touch()
  8.   local event, side
  9.   event, side, xPos, yPos = os.pullEvent("monitor_touch") --Gets the coordinates of where the player touched the screen
  10. end
  11. function printKey()
  12.     local mon = peripheral.wrap('top') --Change 'top' to wherever your monitor is located.
  13.     mon.clear()
  14.     mon.setTextColor(colors.white)
  15.     mon.setCursorPos(1,1)
  16.     mon.setTextScale(1.25)
  17.     mon.write(" 1 2 3")
  18.     mon.setCursorPos(1,2.5)
  19.     mon.write(" 4 5 6")
  20.     mon.setCursorPos(1,3.5)
  21.     mon.write(" 7 8 9")
  22.     mon.setCursorPos(4,4.5)
  23.     mon.write("0")
  24.     mon.setCursorPos(1,5)
  25.     mon.setTextColor(colors.red)
  26.     mon.setTextScale(1)
  27.     mon.write("CLR ")
  28.     mon.setCursorPos(4,5)
  29.     mon.setTextColor(colors.purple)
  30. end
  31. function codeConvert()
  32.   local x = xPos
  33.   local y = yPos
  34.   local xVar
  35.   local num
  36.   local isValid
  37.   --Change the xPos into blocks 1,2,3
  38.   if x==1 or x==2 then
  39.     xVar = 1
  40.   elseif x==3 or x==4 or x==5 then
  41.     xVar = 2
  42.   else
  43.     xVar = 3
  44.   end
  45.   if y<4 then
  46.     isValid = true
  47.   elseif y==4 then
  48.     if xVar==2 then
  49.       isValid = true
  50.     else
  51.       isValid = false
  52.     end
  53.   elseif y==5 then --CLS button
  54.     if x>4 then
  55.       mon.clear()
  56.       num = -1
  57.     end
  58.   end
  59. --Now to determine the numbers picked
  60.   if isValid then
  61.     if xVar==1 then
  62.       if y==1 then
  63.         num = 1
  64.       elseif y==2 then
  65.         num = 4
  66.       elseif y==3 then
  67.         num = 7
  68.       end
  69.     elseif xVar==2 then
  70.       if y==1 then
  71.         num = 2
  72.       elseif y==2 then
  73.         num = 5
  74.       elseif y==3 then
  75.         num = 8
  76.       elseif y==4 then
  77.         num = 0
  78.       end
  79.     elseif xVar==3 then
  80.       if y==1 then
  81.         num = 3
  82.       elseif y==2 then
  83.         num = 6
  84.       elseif y==3 then
  85.         num = 9
  86.       end
  87.     end
  88.   end
  89.   if num==null then
  90.     print("num is null") --Failsafe
  91.     return -1
  92.   elseif num>-1 then
  93.     write(num)
  94.   end
  95.   return num
  96. end
  97.  
  98. local code = 1352 --Edit this to change the password
  99. local userCode = 0
  100. local count = 0
  101. local loop = true
  102. local num = 0
  103. mon = peripheral.wrap('top')
  104. printKey()
  105. while loop do
  106.   if count<4 then
  107.     touch()
  108.   end
  109.   num = codeConvert()
  110.   if num==-1 then
  111.     printKey()
  112.     count = 0
  113.     userCode = 0
  114.   elseif count<4 then
  115.     userCode = (userCode*10)+num
  116.     count = count+1
  117.     mon.write(tostring(num))
  118.   else
  119.     print(" ")
  120.     print(userCode)
  121.     if code==userCode then
  122.       printKey()
  123.       mon.setTextColor(colors.lime)
  124.       mon.write(tostring(userCode))
  125.       print("CORRECT")
  126.       redstone.setOutput('bottom', true)
  127.       sleep(10) --How long the redstone stays on
  128.       printKey()
  129.       redstone.setOutput('bottom', false)
  130.       --To run another program if the code is correct,
  131.       --do:  shell.run("myProgram")
  132.       --right here.
  133.       term.clear()
  134.       term.setCursorPos(1,1)
  135.       userCode = 0
  136.       count = 0
  137.     else
  138.       printKey()
  139.       mon.setTextColor(colors.red)
  140.       mon.write(tostring(userCode))
  141.       userCode = 0
  142.       count = 0
  143.       print("DENIED")
  144.       sleep(2)
  145.       term.clear()
  146.       term.setCursorPos(1,1)
  147.       printKey()
  148.     end
  149.   end
  150.  
  151. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement