Advertisement
BigSHinyToys

RPG map basic example 2

Jun 5th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.43 KB | None | 0 0
  1. local textMap = {
  2. "            ",
  3. "            ",
  4. "    !       ",
  5. "            ",
  6. "            ",
  7. "            ",
  8. "        +   ",
  9. "            ",
  10. "  =         ",
  11. "            ",
  12. "      $     ",
  13. "            ",
  14. "            ",
  15. }
  16.  
  17. local map = {}
  18. for y = 1,#textMap do
  19.     map[y] = {}
  20.     for x = 1,#textMap[y] do
  21.         map[y][x] = textMap[y]:sub(x,x) -- you can use this insted string.sub(textMap[y],x,x)
  22.     end
  23. end
  24.  
  25. local termX,termY = term.getSize()
  26. local posX = math.floor(termX/2)
  27. local posY = math.floor(termY/2)
  28. local text = ""
  29.  
  30. local function draw()
  31.     term.clear()
  32.     for y = 1,#map do
  33.         term.setCursorPos(1,y)
  34.         for x = 1,#map[y] do
  35.             term.write(map[y][x])
  36.         end
  37.     end
  38.     term.setCursorPos(posX,posY)
  39.     term.write("@")
  40.     term.setCursorPos(1,termY)
  41.     term.write(text)
  42. end
  43.  
  44. while true do
  45.     draw()
  46.     local event = {os.pullEvent()}
  47.     if event[1] == "key" then
  48.         if event[2] == 200 then -- up key
  49.             posY = posY - 1
  50.         elseif event[2] == 208 then -- down key
  51.             posY = posY + 1
  52.         elseif event[2] == 203 then -- left
  53.             posX = posX - 1
  54.         elseif event[2] == 205 then -- right
  55.             posX = posX + 1
  56.         elseif event[2] == 14 then -- Press [Backspace] key to exit
  57.             break
  58.         end
  59.         if posY > termY then
  60.             posY = termY
  61.         elseif posY < 1 then
  62.             posY = 1
  63.         end
  64.         if posX > termX then
  65.             posX = termX
  66.         elseif posX < 1 then
  67.             posX = 1
  68.         end
  69.     end
  70.    
  71.     if map[posY] and map[posY][posX] then
  72.         text = map[posY][posX]
  73.     end
  74.    
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement