Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Clear the terminal screen
- local function clear()
- term.clear()
- term.setCursorPos(1, 1)
- end
- -- Draw the main GUI border and header
- local function drawBorder()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
- term.write(string.rep("=", 30)) -- Top border
- term.setCursorPos(1, 2)
- term.write("|" .. string.rep(" ", 28) .. "|") -- Empty space for header
- term.setCursorPos(1, 3)
- term.write("| Turtle Control Panel |") -- Title
- term.setCursorPos(1, 4)
- term.write("|" .. string.rep(" ", 28) .. "|") -- Empty space
- for i = 5, 8 do
- term.setCursorPos(1, i)
- term.write("|" .. string.rep(" ", 28) .. "|") -- Side borders
- end
- term.setCursorPos(1, 9)
- term.write(string.rep("=", 30)) -- Bottom border
- end
- -- Display the fuel level
- local function displayFuelLevel()
- local fuelLevel = turtle.getFuelLevel()
- term.setCursorPos(3, 5)
- term.setTextColor(fuelLevel < 10 and colors.red or colors.green)
- term.write("Fuel Level: " .. fuelLevel .. " units")
- term.setTextColor(colors.white) -- Reset to default color
- end
- -- Display the direction the turtle is facing
- local function displayDirection(direction)
- term.setCursorPos(3, 6)
- term.write("Direction: " .. ({"North", "East", "South", "West"})[direction + 1])
- end
- -- Display the amount of backup fuel available
- local function displayBackupFuel()
- local coalCount = turtle.getItemCount(1) -- Assuming coal is in slot 1
- term.setCursorPos(3, 7)
- term.write("Backup Fuel (Coal): " .. coalCount)
- end
- -- Load fuel into the turtle
- local function loadFuel()
- if turtle.getItemCount(1) > 0 then
- turtle.select(1)
- turtle.refuel()
- displayFuelLevel()
- else
- term.setCursorPos(3, 8)
- term.setTextColor(colors.red)
- term.write("No fuel in slot 1.")
- term.setTextColor(colors.white) -- Reset to default color
- end
- end
- -- Unload all fuel (like coal) from the turtle's inventory to the ground
- local function unloadFuel()
- for i = 1, 16 do
- turtle.select(i)
- local itemCount = turtle.getItemCount(i)
- if itemCount > 0 and turtle.getItemDetail(i) and turtle.getItemDetail(i).name == "minecraft:coal" then
- turtle.drop(itemCount) -- Drop all coal items in the selected slot
- term.setCursorPos(3, 8)
- term.setTextColor(colors.green)
- term.write("Unloaded " .. itemCount .. " coal from slot " .. i .. ".")
- term.setTextColor(colors.white) -- Reset to default color
- end
- end
- end
- -- Move the turtle forward
- local function moveForward()
- if turtle.forward() then
- displayFuelLevel()
- else
- term.setCursorPos(3, 8)
- term.setTextColor(colors.red)
- term.write("Move failed!")
- term.setTextColor(colors.white) -- Reset to default color
- end
- end
- -- Move the turtle backward
- local function moveBackward()
- if turtle.back() then
- displayFuelLevel()
- else
- term.setCursorPos(3, 8)
- term.setTextColor(colors.red)
- term.write("Move failed!")
- term.setTextColor(colors.white) -- Reset to default color
- end
- end
- -- Turn the turtle left and update direction
- local function turnLeft(direction)
- turtle.turnLeft()
- direction = (direction - 1) % 4
- return direction
- end
- -- Turn the turtle right and update direction
- local function turnRight(direction)
- turtle.turnRight()
- direction = (direction + 1) % 4
- return direction
- end
- -- Initialize direction
- local direction = 0 -- 0: North, 1: East, 2: South, 3: West
- -- Start the GUI
- clear()
- drawBorder()
- displayFuelLevel()
- displayDirection(direction)
- displayBackupFuel()
- -- Main loop to handle user input
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.w then
- moveForward()
- elseif key == keys.s then
- moveBackward()
- elseif key == keys.a then
- direction = turnLeft(direction)
- elseif key == keys.d then
- direction = turnRight(direction)
- elseif key == keys.l then
- loadFuel()
- elseif key == keys.u then
- unloadFuel() -- Unload coal when 'U' is pressed
- elseif key == keys.q then
- break -- Exit the program
- end
- -- Refresh the display
- clear()
- drawBorder()
- displayFuelLevel()
- displayDirection(direction)
- displayBackupFuel()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement