Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tArgs = {...}
- mapW, mapH = tonumber(tArgs[1]), tonumber(tArgs[2])
- termW, termH = term.getSize()
- map = {}
- function loadMapImg()
- local mapImg = map
- local newMapImg = {}
- for i, v in pairs(mapImg) do
- newMapImg[#newMapImg + 1] = {}
- mapImg[i] = v:gsub("p", " ")
- for n = 1, mapW do
- local char = mapImg[i]:sub(n, n)
- if char == " " then
- char = 2 ^ 15
- end
- newMapImg[#newMapImg][n] = tonumber(char)
- end
- end
- return newMapImg
- end
- function win()
- local termW, termH = term.getSize()
- for i = 1, 7 do
- term.setCursorPos(termW/2 - 11, 1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.yellow)
- term.clearLine()
- term.write("You won! Congratulations!")
- sleep(.1)
- term.setCursorPos(termW/2 - 11, 1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.blue)
- term.clearLine()
- term.write("You won! Congratulations!")
- sleep(.1)
- end
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- end
- function mapGen()
- local mapWstr = ""
- local mapHstr = "1"
- for i = 1, mapW do
- mapWstr = mapWstr .. "1"
- end
- for i = 1, mapW - 2 do
- mapHstr = mapHstr .. " "
- end
- mapHstr = mapHstr .. "1"
- for i = 1, mapH do
- if i == 1 or i == mapH then
- map[i] = mapWstr
- else
- map[i] = mapHstr
- end
- end
- end
- function gameGen()
- pLocation = {math.random(2, mapW - 1), math.random(2, mapH - 1)}
- while map[pLocation[2]]:sub(pLocation[1], pLocation[1]) == "1" do
- pLocation = {math.random(2, mapW - 1), math.random(2, mapH - 1)}
- end
- map[pLocation[2]] = map[pLocation[2]]:sub(1, pLocation[1] - 1) .. "p" .. map[pLocation[2]]:sub(pLocation[1] + 1, mapW)
- gLocation = {math.random(2, mapW - 1), math.random(2, mapH - 1)}
- while map[gLocation[2]]:sub(gLocation[1], gLocation[1]) == "1" or map[gLocation[2]]:sub(gLocation[1], gLocation[1]) == "p" do
- gLocation = {math.random(2, mapW - 1), math.random(2, mapH - 1)}
- end
- map[gLocation[2]] = map[gLocation[2]]:sub(1, gLocation[1] - 1) .. "2" .. map[gLocation[2]]:sub(gLocation[1] + 1, mapW)
- end
- function mazeGen()
- for i = 1, (mapW - 1)*(mapH - 1)/3 do
- local mLocation = {math.random(2, mapW - 1), math.random(2, mapH - 1)}
- map[mLocation[2]] = map[mLocation[2]]:sub(1, mLocation[1] - 1) .. "1" .. map[mLocation[2]]:sub(mLocation[1] + 1, mapW)
- end
- end
- mapGen()
- mazeGen()
- gameGen()
- local mapImg = loadMapImg()
- while true do
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, termH)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.write("Get to the orange square to win...")
- term.setCursorPos(10, 1)
- --write(pLocation[1], " ", tostring(pLocation[2]))
- paintutils.drawImage(mapImg, termW/2 - pLocation[1] + 2, termH/2 - pLocation[2] + 2)
- term.setBackgroundColor(colors.lightBlue)
- term.setCursorPos(termW/2 + 1, termH/2 + 1)
- term.write(" ")
- if pLocation[1] == gLocation[1] and pLocation[2] == gLocation[2] then
- win()
- break
- end
- local events = {os.pullEvent()}
- if events[1] == "key" then
- if events[2] == 200 then
- if mapImg[pLocation[2] - 1][pLocation[1]] ~= 1 then
- pLocation[2] = pLocation[2] - 1
- end
- elseif events[2] == 203 then
- if mapImg[pLocation[2]][pLocation[1] - 1] ~= 1 then
- pLocation[1] = pLocation[1] - 1
- end
- elseif events[2] == 208 then
- if mapImg[pLocation[2] + 1][pLocation[1]] ~= 1 then
- pLocation[2] = pLocation[2] + 1
- end
- elseif events[2] == 205 then
- if mapImg[pLocation[2]][pLocation[1] + 1] ~= 1 then
- pLocation[1] = pLocation[1] + 1
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement