Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local width, height = 20, 10 -- Size of the drawing grid
- local grid = {}
- for y = 1, height do
- grid[y] = {}
- for x = 1, width do
- grid[y][x] = " " -- Empty cell
- end
- end
- local cursorX, cursorY = 1, 1 -- Starting cursor position
- local function drawGrid()
- term.clear()
- term.setCursorPos(1, 1)
- for y = 1, height do
- for x = 1, width do
- if x == cursorX and y == cursorY then
- term.write("X")
- else
- term.write(grid[y][x])
- end
- end
- print()
- end
- end
- local function saveArt(filename)
- local file = fs.open(filename, "w")
- for y = 1, height do
- file.writeLine(table.concat(grid[y]))
- end
- file.close()
- print("Drawing saved as " .. filename)
- end
- local function loadArt(filename)
- if not fs.exists(filename) then
- print("File not found.")
- return
- end
- local file = fs.open(filename, "r")
- for y = 1, height do
- local line = file.readLine()
- for x = 1, #line do
- grid[y][x] = line:sub(x, x)
- end
- end
- file.close()
- print("Drawing loaded!")
- end
- while true do
- drawGrid()
- print("Controls: WASD to move, Space to draw, C to clear, Save, Load, Exit")
- write("Command: ")
- local input = read()
- if input == "w" and cursorY > 1 then
- cursorY = cursorY - 1
- elseif input == "s" and cursorY < height then
- cursorY = cursorY + 1
- elseif input == "a" and cursorX > 1 then
- cursorX = cursorX - 1
- elseif input == "d" and cursorX < width then
- cursorX = cursorX + 1
- elseif input == " " then
- grid[cursorY][cursorX] = "#"
- elseif input == "c" then
- for y = 1, height do
- for x = 1, width do
- grid[y][x] = " "
- end
- end
- print("Canvas cleared.")
- elseif input == "save" then
- write("Enter filename: ")
- local filename = read()
- saveArt(filename)
- elseif input == "load" then
- write("Enter filename: ")
- local filename = read()
- loadArt(filename)
- elseif input == "exit" then
- break
- else
- print("Unknown command.")
- end
- end
Add Comment
Please, Sign In to add comment