Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function loadMap(path)
- local tOut = {}
- if fs.exists(path) and not fs.isDir(path) then
- local file = fs.open(path,"r")
- if file then
- local sLine = file.readLine()
- while sLine do
- tOut[#tOut + 1] = {}
- for x = 1,#sLine do
- tOut[#tOut][x] = sLine:sub(x,x)
- end
- sLine = file.readLine()
- end
- else
- error("Unable to open file")
- end
- else
- error("Incorrect path")
- end
- return tOut
- end
- local function drawMap(tMap,offsetX,offsetY)
- offsetX = offsetX or 0 -- make shore these have a value
- offsetY = offsetY or 0
- local termX,termY = term.getSize()
- for y = 1,termY do
- term.setCursorPos(1,y)
- if tMap[y - offsetY] then
- for x = 1,termX do
- if tMap[y - offsetY][x - offsetX] then
- term.write(tMap[y - offsetY][x - offsetX])
- else
- term.write(" ")
- end
- end
- end
- end
- end
- local offX,offY = 0,0 -- offset of the map to be draw from
- local map = loadMap("map01") -- will load a file called map01 you can change this to the name of your actual map
- local termX,termY = term.getSize()
- local playerX = math.floor(termX/2)
- local playerY = math.floor(termY/2)
- local text = ""
- local function draw()
- term.clear()
- drawMap(map,offX,offY)
- term.setCursorPos(playerX,playerY)
- term.write("@")
- term.setCursorPos(1,termY)
- term.write("X "..tostring(offX).." Y "..tostring(offY).." Text : "..tostring(text))
- end
- while true do
- draw()
- local event = {os.pullEvent()}
- if event[1] == "key" then
- if event[2] == 200 then -- up key
- offY = offY + 1 -- we change thses to the players moves relitive to map
- elseif event[2] == 208 then -- down key
- offY = offY - 1
- elseif event[2] == 203 then -- left
- offX = offX + 1
- elseif event[2] == 205 then -- right
- offX = offX - 1
- elseif event[2] == 14 then -- Press [Backspace] key to exit
- break
- end
- end
- if map[playerY - offY] and map[playerY - offY][playerX - offX] then
- text = map[playerY - offY][playerX - offX]
- else
- text = ""
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement