JustDoesGames

game

Apr 25th, 2020
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.71 KB | None | 0 0
  1. -- Just Does Games --
  2.  
  3. if not term.isColor() then return print("Advanced Computer Required.") end
  4. local w,h = term.getSize()
  5. local running = true
  6.  
  7. local px,py = math.ceil(w/2), math.ceil(h/2)
  8. local prevx,prevy = px,py
  9. local cx,cy = px,py
  10.  
  11. local player, background = colors.cyan, colors.black
  12.  
  13. function animate_player()
  14.     local limit, loop, update = 1, 0, true
  15.     while running do
  16.         if prevx ~= px or prevy ~= py then
  17.             if prevx ~= px then
  18.                 if px > prevx then prevx = prevx + 1 else prevx = prevx - 1 end
  19.                 update = true
  20.             end
  21.             if prevy ~= py then
  22.                 if py > prevy then prevy = prevy + 1 else prevy = prevy - 1 end
  23.                 update = true
  24.             end
  25.             if update then
  26.                 term.setCursorPos(prevx,prevy)
  27.                 term.setBackgroundColor(background)
  28.                 print(" ")
  29.                 term.setCursorPos(prevx,prevy)
  30.                 term.setBackgroundColor(player)
  31.                 print(" ")
  32.             end
  33.             if loop == limit then sleep(0.1) loop = 0 else loop = loop + 1 end
  34.         else
  35.             sleep(.07) -- ANY LOWER AND IT STOPS WORKING... WHY>?
  36.         end
  37.     end
  38. end
  39.  
  40. function controller()
  41.     while running do
  42.         a,i,x,y = os.pullEvent()
  43.         if a == "key" then
  44.             if i == keys.q then
  45.                 running = false
  46.             else
  47.                
  48.             end
  49.         elseif a == "mouse_click" or a == "mouse_drag" then
  50.             term.setBackgroundColor(background)
  51.             term.setCursorPos(cx,cy) write(" ")
  52.             cx,cy = x,y
  53.             term.setCursorPos(cx,cy) write("x")
  54.             px,py = x,y
  55.         end
  56.         sleep(.01)
  57.     end
  58. end
  59.  
  60. function game()
  61.     parallel.waitForAny(controller, animate_player)
  62. end
  63.  
  64. term.setBackgroundColor(background)
  65. term.clear()
  66.  
  67. term.setCursorPos(prevx,prevy)
  68. term.setBackgroundColor(player)
  69. print(" ")
  70. term.setBackgroundColor(background)
  71.  
  72. game()
  73. term.clear() term.setCursorPos(1,1) print("Exited.") sleep(.3)
Advertisement
Add Comment
Please, Sign In to add comment