Advertisement
DOGGYWOOF

Turtle CTRL panel

Oct 6th, 2024 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. -- Clear the terminal screen
  2. local function clear()
  3. term.clear()
  4. term.setCursorPos(1, 1)
  5. end
  6.  
  7. -- Draw the main GUI border and header
  8. local function drawBorder()
  9. term.setBackgroundColor(colors.black)
  10. term.setTextColor(colors.white)
  11. term.setCursorPos(1, 1)
  12. term.write(string.rep("=", 30)) -- Top border
  13. term.setCursorPos(1, 2)
  14. term.write("|" .. string.rep(" ", 28) .. "|") -- Empty space for header
  15. term.setCursorPos(1, 3)
  16. term.write("| Turtle Control Panel |") -- Title
  17. term.setCursorPos(1, 4)
  18. term.write("|" .. string.rep(" ", 28) .. "|") -- Empty space
  19. for i = 5, 8 do
  20. term.setCursorPos(1, i)
  21. term.write("|" .. string.rep(" ", 28) .. "|") -- Side borders
  22. end
  23. term.setCursorPos(1, 9)
  24. term.write(string.rep("=", 30)) -- Bottom border
  25. end
  26.  
  27. -- Display the fuel level
  28. local function displayFuelLevel()
  29. local fuelLevel = turtle.getFuelLevel()
  30. term.setCursorPos(3, 5)
  31. term.setTextColor(fuelLevel < 10 and colors.red or colors.green)
  32. term.write("Fuel Level: " .. fuelLevel .. " units")
  33. term.setTextColor(colors.white) -- Reset to default color
  34. end
  35.  
  36. -- Display the direction the turtle is facing
  37. local function displayDirection(direction)
  38. term.setCursorPos(3, 6)
  39. term.write("Direction: " .. ({"North", "East", "South", "West"})[direction + 1])
  40. end
  41.  
  42. -- Display the amount of backup fuel available
  43. local function displayBackupFuel()
  44. local coalCount = turtle.getItemCount(1) -- Assuming coal is in slot 1
  45. term.setCursorPos(3, 7)
  46. term.write("Backup Fuel (Coal): " .. coalCount)
  47. end
  48.  
  49. -- Load fuel into the turtle
  50. local function loadFuel()
  51. if turtle.getItemCount(1) > 0 then
  52. turtle.select(1)
  53. turtle.refuel()
  54. displayFuelLevel()
  55. else
  56. term.setCursorPos(3, 8)
  57. term.setTextColor(colors.red)
  58. term.write("No fuel in slot 1.")
  59. term.setTextColor(colors.white) -- Reset to default color
  60. end
  61. end
  62.  
  63. -- Move the turtle forward
  64. local function moveForward()
  65. if turtle.forward() then
  66. displayFuelLevel()
  67. else
  68. term.setCursorPos(3, 8)
  69. term.setTextColor(colors.red)
  70. term.write("Move failed!")
  71. term.setTextColor(colors.white) -- Reset to default color
  72. end
  73. end
  74.  
  75. -- Move the turtle backward
  76. local function moveBackward()
  77. if turtle.back() then
  78. displayFuelLevel()
  79. else
  80. term.setCursorPos(3, 8)
  81. term.setTextColor(colors.red)
  82. term.write("Move failed!")
  83. term.setTextColor(colors.white) -- Reset to default color
  84. end
  85. end
  86.  
  87. -- Turn the turtle left and update direction
  88. local function turnLeft(direction)
  89. turtle.turnLeft()
  90. direction = (direction - 1) % 4
  91. return direction
  92. end
  93.  
  94. -- Turn the turtle right and update direction
  95. local function turnRight(direction)
  96. turtle.turnRight()
  97. direction = (direction + 1) % 4
  98. return direction
  99. end
  100.  
  101. -- Initialize direction
  102. local direction = 0 -- 0: North, 1: East, 2: South, 3: West
  103.  
  104. -- Start the GUI
  105. clear()
  106. drawBorder()
  107. displayFuelLevel()
  108. displayDirection(direction)
  109. displayBackupFuel()
  110.  
  111. -- Main loop to handle user input
  112. while true do
  113. local event, key = os.pullEvent("key")
  114.  
  115. if key == keys.w then
  116. moveForward()
  117. elseif key == keys.s then
  118. moveBackward()
  119. elseif key == keys.a then
  120. direction = turnLeft(direction)
  121. elseif key == keys.d then
  122. direction = turnRight(direction)
  123. elseif key == keys.l then
  124. loadFuel()
  125. elseif key == keys.q then
  126. break -- Exit the program
  127. end
  128.  
  129. -- Refresh the display
  130. clear()
  131. drawBorder()
  132. displayFuelLevel()
  133. displayDirection(direction)
  134. displayBackupFuel()
  135. end
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement