daily pastebin goal
48%
SHARE
TWEET

ComputerCraft Tutorial: key_test0_1

hevohevo Dec 23rd, 2013 500 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ###########################
  2. -- key_test
  3. -- version 0.1
  4. -- http://hevohevo.hatenablog.com/
  5.  
  6. -- ###########################
  7. -- functions
  8. function moveSymbol(x,y)
  9.   term.setCursorPos(x,y)
  10.   term.write("@")
  11.   term.setCursorPos(x,y)
  12. end
  13.  
  14. function moveSymbol2(x,y)
  15.   term.write(" ")
  16.   term.setCursorPos(x,y)
  17.   term.write("@")
  18.   term.setCursorPos(x,y)
  19. end
  20.  
  21. -- ###########################
  22. -- main
  23. MAX_X, MAX_Y = term.getSize()
  24.  
  25. term.clear()
  26. moveSymbol(1,1)
  27.  
  28. while true do
  29.   local ename, keycode = os.pullEvent("key")
  30.  
  31.   if ename == "key" then
  32.     local x, y = term.getCursorPos()
  33.  
  34.     if keycode == keys.up and y > 1 then
  35.       moveSymbol(x,y-1)
  36.     elseif keycode == keys.down and y < MAX_Y then
  37.       moveSymbol(x,y+1)
  38.     elseif keycode == keys.left and x > 1 then
  39.       moveSymbol(x-1,y)
  40.     elseif keycode == keys.right and x < MAX_X then
  41.       moveSymbol(x+1,y)
  42.     elseif keycode == keys.q then
  43.       break -- terminated
  44.     else
  45.       -- do nothing
  46.     end
  47.   end
  48.  
  49.   sleep(0)
  50. end
RAW Paste Data
Top