Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configuration
- local MONITOR_SIDE = "top"
- local REDSTONE_SIDE = "bottom"
- local STATUS_FILE = "farmStatus"
- -- Colors
- local COLOR_RUNNING = colors.red
- local COLOR_STOPPED = colors.green
- local COLOR_BG = colors.black
- local COLOR_TEXT = colors.white
- local COLOR_BUTTON = colors.lime
- local COLOR_GROUND = colors.purple
- local COLOR_CREEPER = colors.lime
- local COLOR_X = colors.red
- -- Monitor setup
- local monitor = peripheral.wrap(MONITOR_SIDE)
- monitor.setTextScale(1)
- local w, h = monitor.getSize()
- -- State
- local farmRunning = false
- local creeperX = w - 3
- local blink = true
- -- Save/load
- local function saveStatus()
- local f = fs.open(STATUS_FILE, "w")
- f.write(farmRunning and "running" or "stopped")
- f.close()
- end
- local function loadStatus()
- if fs.exists(STATUS_FILE) then
- local f = fs.open(STATUS_FILE, "r")
- local data = f.readAll()
- f.close()
- return data == "running"
- end
- return false
- end
- -- Redstone
- local function updateRedstone()
- redstone.setOutput(REDSTONE_SIDE, not farmRunning)
- end
- -- Draw 3-line header with centered label
- local function drawHeader()
- local label = farmRunning and "RUNNING" or "STOPPED"
- monitor.setBackgroundColor(farmRunning and COLOR_RUNNING or COLOR_STOPPED)
- monitor.setTextColor(COLOR_TEXT)
- for y = 1, 3 do
- monitor.setCursorPos(1, y)
- monitor.write((" "):rep(w))
- end
- local x = math.floor((w - #label) / 2) + 1
- monitor.setCursorPos(x, 2) -- Middle line
- monitor.write(label)
- end
- -- Button
- local function drawButton()
- local label = farmRunning and "STOP" or "START"
- local x = math.floor((w - #label) / 2) + 1
- monitor.setCursorPos(x, 5)
- monitor.setBackgroundColor(COLOR_BUTTON)
- monitor.setTextColor(COLOR_TEXT)
- monitor.write(label)
- end
- -- Button check
- local function isButtonPressed(x, y)
- return y == 5
- end
- -- Draw purple ground line at bottom
- local function drawGround()
- monitor.setCursorPos(1, h)
- monitor.setTextColor(COLOR_GROUND)
- monitor.setBackgroundColor(COLOR_BG)
- monitor.write(("⯀"):rep(w))
- end
- -- Clear creeper line (above ground)
- local function clearCreeperRow()
- monitor.setCursorPos(1, h - 1)
- monitor.setBackgroundColor(COLOR_BG)
- monitor.write((" "):rep(w))
- end
- -- Draw creeper frame
- local function drawCreeper()
- clearCreeperRow()
- if creeperX > 3 then
- monitor.setCursorPos(creeperX, h - 1)
- monitor.setTextColor(COLOR_CREEPER)
- monitor.write("C")
- elseif creeperX == 3 then
- monitor.setCursorPos(creeperX, h - 1)
- monitor.setTextColor(COLOR_X)
- monitor.write("X")
- sleep(0.5)
- clearCreeperRow()
- end
- end
- -- Safe to enter (stopped)
- local function drawSafeBlink()
- local msg = "SAFE TO ENTER"
- local x = math.floor((w - #msg) / 2) + 1
- monitor.setCursorPos(x, h - 1)
- monitor.setBackgroundColor(COLOR_BG)
- monitor.setTextColor(COLOR_STOPPED)
- if blink then
- monitor.write(msg)
- else
- monitor.write((" "):rep(#msg))
- end
- end
- -- Full screen draw
- local function redraw()
- monitor.setBackgroundColor(COLOR_BG)
- monitor.clear()
- drawHeader()
- drawButton()
- drawGround()
- end
- -- Init
- farmRunning = loadStatus()
- updateRedstone()
- redraw()
- -- Parallel logic
- parallel.waitForAny(
- function() -- Animation
- while true do
- if farmRunning then
- drawGround()
- drawCreeper()
- creeperX = creeperX - 1
- if creeperX < 3 then
- creeperX = w - 3
- end
- else
- drawGround()
- drawSafeBlink()
- blink = not blink
- end
- sleep(0.3)
- end
- end,
- function() -- Touch handler
- while true do
- local _, _, x, y = os.pullEvent("monitor_touch")
- if isButtonPressed(x, y) then
- farmRunning = not farmRunning
- creeperX = w - 3
- saveStatus()
- updateRedstone()
- redraw()
- end
- end
- end
- )
Advertisement
Add Comment
Please, Sign In to add comment