Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Clear the screen --
- term.clear()
- -- Character position --
- pos = {x = 25, y = 9}
- -- List of all solids. c defines character, b defines if in background --
- solids = {
- {x = 1, y = 1, c = '@'},
- {x = 1, y = 3},
- {x = 2, y = 3, c = '-'},
- {x = 3, y = 3},
- {x = 5, y = 1},
- {x = 5, y = 2},
- {x = 5, y = 3},
- {x = 50, y = 1, c = ':'},
- {x = 51, y = 1, c = 'O'},
- {x = 50, y = 2, c = '|'},
- {x = 51, y = 2, c = '-'},
- {x = 24, y = 8, c = 'X', b = true},
- {x = 25, y = 8, c = 'X', b = true},
- {x = 26, y = 8, c = 'X', b = true}
- }
- -- Function to draw scene --
- function drawScene()
- -- Draw objects --
- for _, solid in pairs(solids) do
- term.setCursorPos(solid.x, solid.y)
- -- Check if solid has a specific character --
- if solid.c then
- term.write(solid.c)
- else
- term.write("#")
- end
- end
- -- Draw player --
- term.setCursorPos(pos.x, pos.y)
- term.write("@")
- end
- -- The last character that the player went on top of --
- lastchar = '!'
- -- Function to move character --
- function move(newPos)
- -- Clear old pos --
- term.setCursorPos(pos.x, pos.y)
- term.write(" ")
- -- Set new pos --
- pos = {x = newPos.x, y = newPos.y}
- -- Draw char at new pos --
- term.setCursorPos(newPos.x, newPos.y)
- term.write("@")
- end
- -- Draw the scene for first time --
- drawScene()
- while true do
- event, key = os.pullEvent("key")
- -- Set default values for variables --
- local collide = false
- local newPos = {x = pos.x, y = pos.y}
- local speed = 1
- -- If key is UP
- if key == 200 then
- newPos.y = newPos.y - speed
- -- If key is DOWN
- elseif key == 208 then
- newPos.y = newPos.y + speed
- -- If key is LEFT
- elseif key == 203 then
- newPos.x = newPos.x - speed
- -- If key is RIGHT
- elseif key == 205 then
- newPos.x = newPos.x + speed
- end
- -- Make sure doesn't go off screen --
- sizeX, sizeY = term.getSize()
- if newPos.x > sizeX or newPos.x < 1 then
- collide = true
- elseif newPos.y > sizeY or newPos.y < 1 then
- collide = true
- else
- -- Make sure newPos does not collide solids --
- for _, solid in pairs(solids) do
- if not solid.b and newPos.x == solid.x and newPos.y == solid.y then
- collide = true
- break -- Exit the for loop
- end
- end
- end
- -- Move if not colliding and newPos is not old pos --
- if not collide then
- move(newPos)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment