SHARE
TWEET

ComputerCraft Tutorial: key_test0_3

hevohevo Dec 24th, 2013 271 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ###########################
  2. -- key_test
  3. -- version 0.3
  4. -- http://hevohevo.hatenablog.com/
  5.  
  6.  
  7. -- ###########################
  8. -- config
  9. MENU = {}
  10. MENU[1] = "EASY"
  11. MENU[2] = "NORMAL"
  12. MENU[3] = "DIFFICULT"
  13. MENU[4] = "EXIT"
  14.  
  15. BASE_X = 7
  16. BASE_Y = 7
  17.  
  18.  
  19. -- ###########################
  20. -- functions
  21. function moveSymbol(x,y)
  22.   term.write(" ")
  23.   term.setCursorPos(x,y)
  24.   term.write(">")
  25.   term.setCursorPos(x,y)
  26. end
  27.  
  28. function drowMenu()
  29.   term.clear()
  30.   term.setCursorPos(BASE_X,BASE_Y)
  31.   for i,v in ipairs(MENU) do
  32.     term.write("  "..v)
  33.     term.setCursorPos(BASE_X,BASE_Y+i)
  34.   end
  35.   moveSymbol(BASE_X,BASE_Y)
  36. end
  37.  
  38. function choose(y)
  39.   if y==1 or y==2 or y==3 then
  40.     term.clear()
  41.     term.setCursorPos(1,1)
  42.     print(MENU[y])
  43.   elseif y == 4 then
  44.     --
  45.   end
  46. end
  47.  
  48.  
  49. -- ###########################
  50. -- main
  51. drowMenu()
  52.  
  53. while true do
  54.   local event, key = os.pullEvent("key")
  55.  
  56.   if event == "key" then
  57.     local x, y = term.getCursorPos() -- current position
  58.     local rel_y = y - BASE_Y + 1 -- relative position x
  59.     local rel_x = x - BASE_X + 1 -- relative position y
  60.  
  61.     if key == keys.up and rel_y > 1 then
  62.       moveSymbol(x,y-1)
  63.     elseif key == keys.down and rel_y < #MENU then
  64.       moveSymbol(x,y+1)
  65.     elseif key == keys.space then
  66.       choose(rel_y)
  67.       break
  68.     end
  69.   end
  70.  
  71.   sleep(0)
  72. end
RAW Paste Data
Top