Advertisement
CaptainSpaceCat

2D Game Engine

Jun 8th, 2015
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.71 KB | None | 0 0
  1. tArgs = {...}
  2. mapW, mapH = tonumber(tArgs[1]), tonumber(tArgs[2])
  3. termW, termH = term.getSize()
  4. map = {}
  5.  
  6. function loadMapImg()
  7.   local mapImg = map
  8.   local newMapImg = {}
  9.   for i, v in pairs(mapImg) do
  10.     newMapImg[#newMapImg + 1] = {}
  11.     mapImg[i] = v:gsub("p", " ")
  12.     for n = 1, mapW do
  13.       local char = mapImg[i]:sub(n, n)
  14.       if char == " " then
  15.         char = 2 ^ 15
  16.       end
  17.       newMapImg[#newMapImg][n] = tonumber(char)
  18.     end
  19.   end
  20.   return newMapImg
  21. end
  22.  
  23. function win()
  24.   local termW, termH = term.getSize()
  25.   for i = 1, 7 do
  26.     term.setCursorPos(termW/2 - 11, 1)
  27.     term.setBackgroundColor(colors.black)
  28.     term.setTextColor(colors.yellow)
  29.     term.clearLine()
  30.     term.write("You won! Congratulations!")
  31.     sleep(.1)
  32.     term.setCursorPos(termW/2 - 11, 1)
  33.     term.setBackgroundColor(colors.black)
  34.     term.setTextColor(colors.blue)
  35.     term.clearLine()
  36.     term.write("You won! Congratulations!")
  37.     sleep(.1)
  38.   end
  39.   term.setBackgroundColor(colors.black)
  40.   term.clear()
  41.   term.setCursorPos(1, 1)
  42. end
  43.  
  44. function mapGen()
  45.   local mapWstr = ""
  46.   local mapHstr = "1"
  47.   for i = 1, mapW do
  48.     mapWstr = mapWstr .. "1"
  49.   end
  50.   for i = 1, mapW - 2 do
  51.     mapHstr = mapHstr .. " "
  52.   end
  53.   mapHstr = mapHstr .. "1"
  54.   for i = 1, mapH do
  55.     if i == 1 or i == mapH then
  56.       map[i] = mapWstr
  57.     else
  58.       map[i] = mapHstr
  59.     end
  60.   end
  61. end
  62.  
  63. function gameGen()
  64.   pLocation = {math.random(2, mapW - 1), math.random(2, mapH - 1)}
  65.   while map[pLocation[2]]:sub(pLocation[1], pLocation[1]) == "1" do
  66.     pLocation = {math.random(2, mapW - 1), math.random(2, mapH - 1)}
  67.   end
  68.   map[pLocation[2]] = map[pLocation[2]]:sub(1, pLocation[1] - 1) .. "p" .. map[pLocation[2]]:sub(pLocation[1] + 1, mapW)
  69.   gLocation = {math.random(2, mapW - 1), math.random(2, mapH - 1)}
  70.   while map[gLocation[2]]:sub(gLocation[1], gLocation[1]) == "1" or map[gLocation[2]]:sub(gLocation[1], gLocation[1]) == "p" do
  71.     gLocation = {math.random(2, mapW - 1), math.random(2, mapH - 1)}
  72.   end
  73.   map[gLocation[2]] = map[gLocation[2]]:sub(1, gLocation[1] - 1) .. "2" .. map[gLocation[2]]:sub(gLocation[1] + 1, mapW)
  74. end
  75.  
  76. function mazeGen()
  77.   for i = 1, (mapW - 1)*(mapH - 1)/3 do
  78.     local mLocation = {math.random(2, mapW - 1), math.random(2, mapH - 1)}
  79.     map[mLocation[2]] = map[mLocation[2]]:sub(1, mLocation[1] - 1) .. "1" .. map[mLocation[2]]:sub(mLocation[1] + 1, mapW)
  80.   end
  81. end
  82.  
  83. mapGen()
  84. mazeGen()
  85. gameGen()
  86. local mapImg = loadMapImg()
  87. while true do
  88.   term.setBackgroundColor(colors.black)
  89.   term.clear()
  90.   term.setCursorPos(1, termH)
  91.   term.setBackgroundColor(colors.black)
  92.   term.setTextColor(colors.white)
  93.   term.write("Get to the orange square to win...")
  94.   term.setCursorPos(10, 1)
  95.   --write(pLocation[1], " ", tostring(pLocation[2]))
  96.   paintutils.drawImage(mapImg, termW/2 - pLocation[1] + 2, termH/2 - pLocation[2] + 2)
  97.   term.setBackgroundColor(colors.lightBlue)
  98.   term.setCursorPos(termW/2 + 1, termH/2 + 1)
  99.   term.write(" ")
  100.   if pLocation[1] == gLocation[1] and pLocation[2] == gLocation[2] then
  101.     win()
  102.     break
  103.   end
  104.   local events = {os.pullEvent()}
  105.   if events[1] == "key" then
  106.     if events[2] == 200 then
  107.       if mapImg[pLocation[2] - 1][pLocation[1]] ~= 1 then
  108.         pLocation[2] = pLocation[2] - 1
  109.       end
  110.     elseif events[2] == 203 then
  111.       if mapImg[pLocation[2]][pLocation[1] - 1] ~= 1 then
  112.         pLocation[1] = pLocation[1] - 1
  113.       end
  114.     elseif events[2] == 208 then
  115.       if mapImg[pLocation[2] + 1][pLocation[1]] ~= 1 then
  116.         pLocation[2] = pLocation[2] + 1
  117.       end
  118.     elseif events[2] == 205 then
  119.       if mapImg[pLocation[2]][pLocation[1] + 1] ~= 1 then
  120.         pLocation[1] = pLocation[1] + 1
  121.       end
  122.     end
  123.   end
  124. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement