Advertisement
goldfiction

menu tutorial

Jun 21st, 2025
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.42 KB | Gaming | 0 0
  1. local w,h = term.getSize()
  2. local select = 1
  3.  
  4. --Some draw functions to get us started
  5.  
  6. local function printCentered(str, ypos)
  7.   term.setCursorPos(w/2 - #str/2, ypos)
  8.   term.write(str)
  9. end
  10.  
  11. local function printRight(str, ypos)
  12.   term.setCursorPos(w - #str, ypos)
  13.   term.write(str)
  14. end
  15.  
  16. --Add the menus here
  17.  
  18. function drawMain()
  19.   printCentered("Picture 1", 8)
  20.   printCentered("Picture 2", 12)
  21.   printCentered("Quit", h-2)
  22.  
  23.   local ypos = 9
  24.   if select == 2 then ypos = 13
  25.   elseif select == 3 then ypos = h-1 end
  26.   printCentered("---------", ypos)
  27. end
  28.  
  29. function drawPicOne()
  30.   local rookList = {
  31.         "|'-'-'| ",
  32.         " \\   / ",
  33.         "  |   |  ",
  34.         "  |   |  ",
  35.         "  |   |  ",
  36.         "  |   |  ",
  37.         "  |___|  ",
  38.         " /_____\\ ",
  39.         "(_______)"
  40.     }
  41.    
  42.     for i=1,#rookList do
  43.         printCentered(rookList[i], 3 + i)
  44.     end
  45.    
  46.     printCentered("Picture 2", h-4)
  47.     printCentered("Back", h-1)
  48.     local ypos = h-3;
  49.     if select == 2 then ypos = h end
  50.     printCentered("---------", ypos)
  51. end
  52.  
  53. function drawPicTwo()
  54.   local bishopList = {
  55.         "   _<>  ",
  56.         " /\\\\  \\",
  57.         " \\ \\) /",
  58.        "  \\__/  ",
  59.         " (____) ",
  60.         "  |  |  ",
  61.         "  |  |  ",
  62.         "  |  |  ",
  63.         "  |__|  ",
  64.         " /____\\ ",
  65.         "(______)"
  66.     }
  67.    
  68.     for i=1,#bishopList do
  69.         printCentered(bishopList[i], 2 + i)
  70.     end
  71.    
  72.     printCentered("Picture 1", h-4)
  73.     printCentered("Back", h-1)
  74.     local ypos = h-3;
  75.     if select == 2 then ypos = h end
  76.     printCentered("---------", ypos)
  77. end
  78.  
  79. function drawHeader()
  80.   printCentered("PICTURE VIEWER", 1)
  81.   printCentered(string.rep("-", w), 2)  
  82.   printRight("by nitrofingers", h)
  83. end
  84.  
  85. --Menu state
  86.  
  87. local menustate = "main"
  88.  
  89. local mopt = {
  90.   ["main"] = {
  91.     options = {"pic1", "pic2", "quit"},
  92.     draw = drawMain
  93.   },
  94.   ["pic1"] = {
  95.     options = {"pic2", "main"},
  96.     draw = drawPicOne
  97.   },
  98.   ["pic2"] = {
  99.     options = {"pic1", "main"},
  100.     draw = drawPicTwo
  101.   }
  102. }
  103.  
  104. --Run Function
  105.  
  106. function runMenu()
  107.   while true do
  108.     term.clear()
  109.     drawHeader()
  110.     mopt[menustate].draw()
  111.  
  112.     local id, key = os.pullEvent("key")
  113.     --UP = 200, DOWN = 208, ENTER = 28
  114.    
  115.     if key == 200 and select > 1 then select = select-1
  116.     elseif key == 208 and select < #mopt[menustate].options then select = select+1
  117.     elseif key == 28 then
  118.       if mopt[menustate].options[select] == "quit" then break end
  119.       menustate = mopt[menustate].options[select]
  120.     end
  121.   end
  122. end
  123.  
  124. runMenu()
  125. term.clear()
  126. term.setCursorPos(1,1)
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement