paramus

Calculator

Feb 11th, 2025 (edited)
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.41 KB | Gaming | 0 0
  1. local Total, Memory, Operando = 0, 0, ""
  2.  
  3. function Gui()
  4.     term.setBackgroundColor(colors.black)
  5.     term.clear()
  6.    
  7.     if (Memory ~= 0) then
  8.         term.setCursorPos(1,1)
  9.         term.write(Memory)
  10.     end
  11.     term.setCursorPos(1,2)
  12.     term.write(Operando)
  13.     term.setCursorPos(1,3)
  14.     term.write(Total)
  15.    
  16.     --remove when done
  17.     paintutils.drawLine(27,1,27,20,colors.white)
  18.    
  19.     --Number buttons
  20.     paintutils.drawFilledBox(1,4,5,7,colors.yellow)
  21.     term.setCursorPos(3,6)
  22.     term.write("7")
  23.     paintutils.drawFilledBox(6,4,10,7,colors.orange)
  24.     term.setCursorPos(8,6)
  25.     term.write("8")
  26.     paintutils.drawFilledBox(11,4,15,7,colors.yellow)
  27.     term.setCursorPos(13,6)
  28.     term.write("9")
  29.     paintutils.drawFilledBox(1,8,5,11,colors.orange)
  30.     term.setCursorPos(3,10)
  31.     term.write("4")
  32.     paintutils.drawFilledBox(6,8,10,11,colors.yellow)
  33.     term.setCursorPos(8,10)
  34.     term.write("5")
  35.     paintutils.drawFilledBox(11,8,15,11,colors.orange)
  36.     term.setCursorPos(13,10)
  37.     term.write("6")
  38.     paintutils.drawFilledBox(1,12,5,15,colors.yellow)
  39.     term.setCursorPos(3,14)
  40.     term.write("1")
  41.     paintutils.drawFilledBox(6,12,10,15,colors.orange)
  42.     term.setCursorPos(8,14)
  43.     term.write("2")
  44.     paintutils.drawFilledBox(11,12,15,15,colors.yellow)
  45.     term.setCursorPos(13,14)
  46.     term.write("3")
  47.     paintutils.drawFilledBox(1,16,5,19,colors.orange)
  48.     term.setCursorPos(3,18)
  49.     term.write("")
  50.     paintutils.drawFilledBox(6,16,10,19,colors.yellow)
  51.     term.setCursorPos(8,18)
  52.     term.write("0")
  53.     paintutils.drawFilledBox(11,16,15,19,colors.orange)
  54.     term.setCursorPos(13,18)
  55.     term.write(",")
  56.    
  57.     --Operators Buttons
  58.     paintutils.drawFilledBox(16,4,20,7,colors.gray)
  59.     term.setCursorPos(18,6)
  60.     term.write("/")
  61.     paintutils.drawFilledBox(21,4,26,7,colors.lightGray)
  62.     term.setCursorPos(23,6)
  63.     term.write("<<")
  64.     paintutils.drawFilledBox(16,8,20,11,colors.lightGray)
  65.     term.setCursorPos(18,10)
  66.     term.write("*")
  67.     paintutils.drawFilledBox(21,8,26,11,colors.gray)
  68.     term.setCursorPos(23,10)
  69.     term.write("CE")
  70.     paintutils.drawFilledBox(16,12,20,15,colors.gray)
  71.     term.setCursorPos(18,14)
  72.     term.write("-")
  73.     paintutils.drawFilledBox(21,12,26,15,colors.lightGray)
  74.     term.setCursorPos(23,14)
  75.     term.write("+")
  76.     paintutils.drawFilledBox(16,16,20,19,colors.lightGray)
  77.     term.setCursorPos(18,18)
  78.     term.write("%")
  79.     paintutils.drawFilledBox(21,16,26,19,colors.gray)
  80.     term.setCursorPos(23,18)
  81.     term.write("=")
  82. end
  83.  
  84. function KeyPress(key)
  85.     key = keys.getName(key)
  86.            
  87.     if (key == "+") then
  88.         operando = "+"
  89.     elseif (key == "-") then
  90.         operando = "-"
  91.     elseif (key == "*") then
  92.         operando = "*"
  93.     elseif (key == "/") then
  94.         operando = "/"
  95.     else
  96.         if (key == "zero") then key = "0"
  97.         elseif (key == "one") then key = "1"
  98.         elseif (key == "two") then key = "2"
  99.         elseif (key == "three") then key = "3"
  100.         elseif (key == "four") then key = "4"
  101.         elseif (key == "five") then key = "5"
  102.         elseif (key == "six") then key = "6"
  103.         elseif (key == "seven") then key = "7"
  104.         elseif (key == "eight") then key = "8"
  105.         elseif (key == "nine") then key = "9"
  106.         end
  107.  
  108.         if (Operando == "") then
  109.             if (Total == 0) then
  110.                 Total = tonumber(key)
  111.             else
  112.                 Total = Total .. tonumber(key)
  113.             end
  114.         elseif (Operando == "+") then
  115.             Total = Total + tonumber(key)
  116.         elseif (Operando == "-") then
  117.             Total = Total - tonumber(key)
  118.         elseif (Operando == "*") then
  119.             Total = Total * tonumber(key)
  120.         elseif (Operando == "/") then
  121.             Total = Total / tonumber(key)
  122.         end
  123.     end
  124. end
  125.  
  126. function Click(x,y)
  127.     -- Numbers
  128.     if (x>=1 and x<=5 and y>=4 and y<=7) then
  129.         if (Total == 0) then
  130.             Total = 7
  131.         else
  132.             Total = Total .. 7
  133.         end
  134.     elseif (x>=6 and x<=10 and y>=4 and y<=7) then
  135.         if (Total == 0) then
  136.             Total = 8
  137.         else
  138.             Total = Total .. 8
  139.         end
  140.     elseif (x>=11 and x<=15 and y>=4 and y<=7) then
  141.         if (Total == 0) then
  142.             Total = 9
  143.         else
  144.             Total = Total .. 9
  145.         end
  146.     elseif (x>=1 and x<=5 and y>=8 and y<=11) then
  147.         if (Total == 0) then
  148.             Total = 4
  149.         else
  150.             Total = Total .. 4
  151.         end
  152.     elseif (x>=6 and x<=10 and y>=8 and y<=11) then
  153.         if (Total == 0) then
  154.             Total = 5
  155.         else
  156.             Total = Total .. 5
  157.         end
  158.     elseif (x>=11 and x<=15 and y>=8 and y<=11) then
  159.         if (Total == 0) then
  160.             Total = 6
  161.         else
  162.             Total = Total .. 6
  163.         end
  164.     elseif (x>=1 and x<=5 and y>=12 and y<=15) then
  165.         if (Total == 0) then
  166.             Total = 1
  167.         else
  168.             Total = Total .. 1
  169.         end
  170.     elseif (x>=6 and x<=10 and y>=12 and y<=15) then
  171.         if (Total == 0) then
  172.             Total = 2
  173.         else
  174.             Total = Total .. 2
  175.         end
  176.     elseif (x>=11 and x<=15 and y>=12 and y<=15) then
  177.         if (Total == 0) then
  178.             Total = 3
  179.         else
  180.             Total = Total .. 3
  181.         end
  182.     elseif (x>=6 and x<=10 and y>=16 and y<=19) then
  183.         if (Total == 0) then
  184.             Total = 0
  185.         else
  186.             Total = Total .. 0
  187.         end
  188.     end
  189.    
  190.     --Symbols
  191.     if (x>=16 and x<=20 and y>=4 and y<=7) then
  192.         Memory = Total
  193.         Total = 0
  194.         Operando = "/"
  195.     elseif (x>=21 and x<=26 and y>=4 and y<=7) then
  196.         local sTotal = tostring(Total)
  197.         Total = tonumber(string.sub(sTotal,1,string.len(sTotal)-1))
  198.         if (Total == nil) then
  199.             Total=0
  200.         end
  201.     elseif (x>=16 and x<=20 and y>=8 and y<=11) then
  202.         Memory = Total
  203.         Total = 0
  204.         Operando = "*"
  205.     elseif (x>=21 and x<=26 and y>=8 and y<=11) then
  206.         Memory = 0
  207.         Total = 0
  208.         Operando = ""
  209.     elseif (x>=16 and x<=20 and y>=12 and y<=15) then
  210.         Memory = Total
  211.         Total = 0
  212.         Operando = "-"
  213.     elseif (x>=21 and x<=26 and y>=12 and y<=15) then
  214.         Memory = Total
  215.         Total = 0
  216.         Operando = "+"
  217.     elseif (x>=16 and x<=20 and y>=16 and y<=19) then
  218.         Memory = Total
  219.         Total = 0
  220.         Operando = "%"
  221.     elseif (x>=21 and x<=26 and y>=16 and y<=19) then
  222.         if (Operando == "+") then
  223.             Total = Memory + Total
  224.             Memory = 0
  225.             Operando = ""
  226.         elseif (Operando == "-") then
  227.             Total = Memory - Total
  228.             Memory = 0
  229.             Operando = ""
  230.         elseif (Operando == "*") then
  231.             Total = Memory * Total
  232.             Memory = 0
  233.             Operando = ""
  234.         elseif (Operando == "/") then
  235.             Total = Memory / Total
  236.             Memory = 0
  237.             Operando = ""
  238.         elseif (Operando == "%") then
  239.             Total = Memory % Total
  240.             Memory = 0
  241.             Operando = ""
  242.         end
  243.     end
  244. end
  245.  
  246. -- Main --
  247. while (true) do
  248.     Gui()
  249.     term.setCursorPos(1,1)
  250.     parallel.waitForAny(function()
  251.             local event, key, is_held = os.pullEvent("key")
  252.             --KeyPress(key)
  253.         end,
  254.         function()
  255.             local event, button, x, y = os.pullEvent("mouse_click")
  256.             Click(x,y)
  257.         end)
  258. end
Advertisement
Add Comment
Please, Sign In to add comment