Advertisement
Guest User

keypad

a guest
Jul 1st, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.25 KB | None | 0 0
  1. monitor = peripheral.wrap("monitor_3")
  2. glass = peripheral.wrap("openperipheral_bridge_1")
  3.  
  4. correct = 0
  5. required = 5
  6.  
  7.  
  8. while true do
  9.   function RandKey()
  10.   rand = math.random(0,9)
  11.   return rand
  12. end
  13.  
  14.   digit = RandKey()
  15.   displayDigit = tostring(digit)
  16.   dCorrect = tostring(correct)
  17.  
  18.   glass.clear()
  19.   glass.addText(225,120,displayDigit,0xffffff)
  20.  -- glass.addText(225,150,correct,0x00f000)
  21.   glass.sync()
  22.  
  23.  
  24.     t = {
  25.         {text = "1", x = 1, y = 1, txtCol = colours.white, bgCol = colours.blue},
  26.         {text = "2", x = 3, y = 1, txtCol = colours.white, bgCol = colours.blue},
  27.         {text = "3", x = 5, y = 1, txtCol = colours.white, bgCol = colours.blue},
  28.         {text = "4", x = 1, y = 3, txtCol = colours.white, bgCol = colours.blue},
  29.         {text = "5", x = 3, y = 3, txtCol = colours.white, bgCol = colours.blue},
  30.         {text = "6", x = 5, y = 3, txtCol = colours.white, bgCol = colours.blue},
  31.         {text = "7", x = 1, y = 5, txtCol = colours.white, bgCol = colours.blue},
  32.         {text = "8", x = 3, y = 5, txtCol = colours.white, bgCol = colours.blue},
  33.         {text = "9", x = 5, y = 5, txtCol = colours.white, bgCol = colours.blue},
  34.         {text = "0", x = 7, y = 1, txtCol = colours.white, bgCol = colours.blue},
  35.   --{text = tostring(required-correct), x = 7, y = 3, txtCol = colors.white, bgCol = colors.blue}
  36.     }
  37.  
  38.     function writeButtons(_table)
  39.         for i, v in pairs(_table) do
  40.             monitor.setCursorPos(v.x, v.y)
  41.             monitor.setTextColour(v.txtCol)
  42.             monitor.setBackgroundColour(v.bgCol)
  43.          monitor.write(v.text)
  44.         end
  45.     end
  46.  
  47.     function isValidClick(_table, mx, my)
  48.  
  49.         for i, v in pairs(_table) do
  50.             if mx >= v.x and mx <= (v.x + #v.text) and my == v.y then
  51.                  return true, v.text
  52.             end
  53.         end
  54.         return false, nil
  55.     end
  56.  
  57.     writeButtons(t)
  58.  
  59.    
  60.         _, but, x, y = os.pullEvent("monitor_touch")
  61.         bClick, option = isValidClick(t, x, y)
  62.         if bClick then
  63.         if tonumber(option) == digit then correct = correct + 1
  64.   else if tonumber(option) ~= digit then correct = 0
  65.     end
  66. end
  67. end
  68.  
  69. monitor.setCursorPos(7,5)
  70. monitor.setBackgroundColor(colors.green)
  71. monitor.setTextColor(colors.white)
  72. monitor.write(tostring(required-correct))
  73.  
  74. if correct >= required then
  75.   redstone.setBundledOutput("back",colors.red)
  76.   sleep(2)
  77.   redstone.setBundledOutput("back",0)
  78.   correct = 0
  79.   end
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement