Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local termW, termH = term.getSize()
- local debug = false
- function drawRelevantImage(img, xPos, yPos)
- for i, v in pairs(img) do
- for k, e in pairs(v) do
- if i + yPos - 1 >= 1 and i + yPos - 1 <= termH and k + xPos - 1 >= 1 and k + xPos - 1 <= termW then
- term.setCursorPos(k + xPos - 1, i + yPos - 1)
- term.setBackgroundColor(e)
- term.write(" ")
- end
- end
- end
- 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 gameGen(mapW, mapH)
- pLocation = math.random(1, 4)
- if pLocation == 1 then
- pLocation = {2, 2}
- gLocation = {mapW*2, mapH*2}
- elseif pLocation == 2 then
- pLocation = {mapW*2, 2}
- gLocation = {2, mapH*2}
- elseif pLocation == 3 then
- pLocation = {2, mapH*2}
- gLocation = {mapW*2, 2}
- elseif pLocation == 4 then
- pLocation = {mapW*2, mapH*2}
- gLocation = {2, 2}
- end
- return pLocation, gLocation
- end
- function mazeGenRecursive(mapW, mapH)
- local maze = {}
- for y = 1, mapH do
- maze[y] = {}
- for x = 1, mapW do
- maze[y][x] = {}
- for i = 1, 4 do
- maze[y][x][i] = true
- end
- end
- end
- local current = {1, 1} --y, x
- local stack = {}
- local cellsRemaining = mapW * mapH
- while cellsRemaining > 1 do
- local fullCells = {}
- if current[1] > 1 and maze[current[1] - 1][current[2]][1] and maze[current[1] - 1][current[2]][2] and maze[current[1] - 1][current[2]][3] and maze[current[1] - 1][current[2]][4] then
- fullCells[#fullCells + 1] = 1
- end
- if current[1] < mapH and maze[current[1] + 1][current[2]][1] and maze[current[1] + 1][current[2]][2] and maze[current[1] + 1][current[2]][3] and maze[current[1] + 1][current[2]][4]then
- fullCells[#fullCells + 1] = 3
- end
- if current[2] > 1 and maze[current[1]][current[2] - 1][1] and maze[current[1]][current[2] - 1][2] and maze[current[1]][current[2] - 1][3] and maze[current[1]][current[2] - 1][4] then
- fullCells[#fullCells + 1] = 4
- end
- if current[2] < mapW and maze[current[1]][current[2] + 1][1] and maze[current[1]][current[2] + 1][2] and maze[current[1]][current[2] + 1][3] and maze[current[1]][current[2] + 1][4] then
- fullCells[#fullCells + 1] = 2
- end
- if #fullCells > 0 then
- cellsRemaining = cellsRemaining - 1
- stack[#stack + 1] = {current[1], current[2]}
- fullCells = fullCells[math.random(1, #fullCells)]
- maze[current[1]][current[2]][fullCells] = false
- if fullCells == 1 then
- maze[current[1] - 1][current[2]][3] = false
- current = {current[1] - 1, current[2]}
- elseif fullCells == 2 then
- maze[current[1]][current[2] + 1][4] = false
- current = {current[1], current[2] + 1}
- elseif fullCells == 3 then
- maze[current[1] + 1][current[2]][1] = false
- current = {current[1] + 1, current[2]}
- elseif fullCells == 4 then
- maze[current[1]][current[2] - 1][2] = false
- current = {current[1], current[2] - 1}
- end
- else
- current = table.remove(stack)
- end
- end
- local gen = maze
- maze = {}
- for i = 1, mapH*2 + 1 do
- maze[i] = {}
- end
- for i = 1, mapW*2 + 1 do
- maze[1][i] = 1
- maze[mapH*2 + 1][i] = 1
- end
- for y = 2, mapH*2 do
- for x = 1, mapW*2 + 1 do
- if x == 1 or x == mapW*2 + 1 then
- maze[y][x] = 1
- elseif y%2 == 0 and x%2 == 0 then
- maze[y][x] = 2^15
- elseif y%2 == 1 and x%2 == 1 then
- maze[y][x] = 1
- else
- if y%2 == 0 then
- if gen[y/2][(x - 1)/2][2] then
- maze[y][x] = 1
- else
- maze[y][x] = 2^15
- end
- elseif y%2 == 1 then
- if gen[(y - 1)/2][x/2][3] then
- maze[y][x] = 1
- else
- maze[y][x] = 2^15
- end
- end
- end
- end
- end
- return maze
- end
- term.setBackgroundColor(colors.black)
- term.clear()
- local logo = mazeGenRecursive(5, 5)
- term.setCursorPos(termW/2 - 4, 3)
- term.write("Maze Game")
- logo[2][2] = colors.lightBlue
- logo[10][10] = colors.orange
- drawRelevantImage(logo, termW/2 - 5, 6)
- term.setCursorPos(termW/2 - 7, termH - 2)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.write("Press any key...")
- os.pullEvent("key")
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- term.write("Enter maze width: ")
- local mapW = read()
- while not tonumber(mapW) do
- term.setCursorPos(1, 1)
- term.clearLine()
- term.write("Enter maze width: ")
- mapW = read()
- end
- term.setCursorPos(1, 2)
- term.write("Enter maze height: ")
- local mapH = read()
- while not tonumber(mapH) do
- term.setCursorPos(1, 2)
- term.clearLine()
- term.write("Enter maze height: ")
- mapH = read()
- end
- mapW, mapH = tonumber(mapW), tonumber(mapH)
- local maze = mazeGenRecursive(mapW, mapH)
- local pLocation, gLocation = gameGen(mapW, mapH, maze)
- maze[gLocation[2]][gLocation[1]] = 2
- if debug then
- paintutils.drawImage(maze, 1, 1)
- term.setCursorPos(pLocation[1], pLocation[2])
- term.setBackgroundColor(colors.lightBlue)
- term.write(" ")
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- print("You are the blue square...")
- term.setCursorPos(termW/2 + 1, termH/2 + 1)
- term.setBackgroundColor(colors.lightBlue)
- term.write(" ")
- term.setCursorPos(termW/2 - 7, termH - 1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.write("Press any key...")
- os.pullEvent("key")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- print("Get to the orange square...")
- term.setCursorPos(termW/2 + 1, termH/2 + 1)
- term.setBackgroundColor(colors.orange)
- term.write(" ")
- term.setCursorPos(termW/2 - 7, termH - 1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.write("Press any key...")
- os.pullEvent("key")
- while true do
- term.setBackgroundColor(colors.black)
- term.clear()
- --term.setCursorPos(10, 1)
- --write(pLocation[1], " ", tostring(pLocation[2]))
- drawRelevantImage(maze, 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 maze[pLocation[2] - 1][pLocation[1]] ~= 1 then
- pLocation[2] = pLocation[2] - 1
- end
- elseif events[2] == 203 then
- if maze[pLocation[2]][pLocation[1] - 1] ~= 1 then
- pLocation[1] = pLocation[1] - 1
- end
- elseif events[2] == 208 then
- if maze[pLocation[2] + 1][pLocation[1]] ~= 1 then
- pLocation[2] = pLocation[2] + 1
- end
- elseif events[2] == 205 then
- if maze[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