Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ScreenWidth, ScreenHeight = term.getSize()
- local char = ">"
- local playerX = math.floor(ScreenWidth / 2)
- local playerY = math.floor(ScreenHeight / 2)
- local offsetX, offsetY = 0, 0
- local map = {}
- function drawCharacter()
- term.setCursorPos(playerX - offsetX, playerY - offsetY)
- write(char)
- end
- function newObject(x, y, obj)
- map[y] = map[y] or {}; map[y][x] = {obj}
- end
- function newSolidObject(x, y, obj)
- map[x] = map[y] or {}; map[y][x] = {obj, true}
- end
- function checkForCollision(x, y)
- return map[y] and map[y][x] and map[y][x][2]
- end
- function drawMap()
- term.clear()
- for y = 1, ScreenHeight do
- if map[y + offsetY] then
- for x = 1, ScreenWidth do
- if map[y + offsetY][x + offsetX] then
- term.setCursorPos(x, y)
- term.write(map[y + offsetY][x + offsetX][1])
- end
- end
- end
- end
- drawCharacter()
- end
- newSolidObject(4, 5, "x")
- newSolidObject(6, 6, "x")
- newSolidObject(5, 5, "x")
- newObject(7, 7, "w")
- newObject(8, 8, "w")
- newObject(8, 7, "w")
- while true do
- drawMap()
- local e, key = os.pullEvent("key")
- if (key == 17 or key == 200) then
- if not checkForCollision(playerX, playerY - 1) then
- offsetY = offsetY - 1; playerY = playerY - 1
- end; char = "^"
- elseif (key == 31 or key == 208) then
- if not checkForCollision(playerX, playerY + 1) then
- offsetY = offsetY + 1; playerY = playerY + 1
- end; char = "v"
- elseif (key == 203 or key == 30) then
- if not checkForCollision(playerX - 1, playerY) then
- offsetX = offsetX - 1; playerX = playerX - 1
- end; char = "<"
- elseif (key == 205 or key == 32) then
- if not checkForCollision(playerX + 1, playerY) then
- offsetX = offsetX + 1; playerX = playerX + 1
- end; char = ">"
- elseif key == 15 then
- break
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment