Advertisement
Vilsol

Bounce

Sep 10th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.74 KB | None | 0 0
  1. levels = {}
  2. levels['1'] = {}
  3. levels['1']['x'] = 9
  4. levels['1']['y'] = 9
  5. levels['1']['map'] = {
  6.     "#############################################################################",
  7.     "#BSX                                                                        #",
  8.     "#                                                                           #",
  9.     "#                                                                           #",
  10.     "#                                                                           #",
  11.     "#                                                                           #",
  12.     "#                                                                           #",
  13.     "#                                                                           #",
  14.     "#                                                                           #",
  15.     "#                                                                           #",
  16.     "#                                                                           #",
  17.     "#                                                                           #",
  18.     "#                                                                           #",
  19.     "#                                                                           #",
  20.     "#                                                                           #",
  21.     "#                                                                           #",
  22.     "#                                                                           #",
  23.     "#                                                                           #",
  24.     "#                                                                           #",
  25.     "#                                                                           #",
  26.     "#                                                                           #",
  27.     "#                                                                           #",
  28.     "#                                                                           #",
  29.     "#                                                                           #",
  30.     "#                                                                           #",
  31.     "#                                                                           #",
  32.     "#                                                                           #",
  33.     "#                                                                           #",
  34.     "#                                                                           #",
  35.     "#                                                                           #",
  36.     "#                                                                           #",
  37.     "#                                                                           #",
  38.     "#                                                                           #",
  39.     "#                                                                           #",
  40.     "#                                                                           #",
  41.     "#############################################################################"
  42. }
  43.  
  44. ball = {}
  45. ball['size'] = 1
  46. ball['x'] = levels['1']['x']
  47. ball['y'] = levels['1']['y']
  48. ball['drawx'] = levels['1']['x']
  49. ball['drawy'] = levels['1']['y']
  50.  
  51. scroll = {}
  52. scroll['x'] = 0
  53. scroll['y'] = 0
  54.  
  55. w, h = term.getSize()
  56.  
  57. function getChar(x, y)
  58.     lvl = levels['1']['map'][y]
  59.     print(lvl)
  60.     if(lvl ~= nil)then
  61.         char = string.sub(lvl, x, x)
  62.         return char
  63.     end
  64.     return " "
  65. end
  66.  
  67. function canMove(x, y, size)
  68.     if(size == 1)then
  69.         if(getChar(x, y) ~= " ")then
  70.             return false
  71.         end
  72.     elseif(size == 2)then
  73.         for u = 0, 1 do
  74.             for d = 0, 1 do
  75.                 if(getChar(x+u, y+d) ~= " ")then
  76.                     return false
  77.                 end
  78.             end
  79.         end
  80.     end
  81.     return true
  82. end
  83.  
  84. function drawBall()
  85.     term.clear()
  86.     term.setBackgroundColor(colors.orange)
  87.     if(ball['size'] == 1)then
  88.         term.setCursorPos(ball['drawx'], ball['drawy']);
  89.         term.write(" ")
  90.     elseif(ball['size'] == 2)then
  91.         term.setCursorPos(ball['drawx'], ball['drawy']);
  92.         term.write("  ")
  93.         term.setCursorPos(ball['drawx'], ball['drawy']+1);
  94.         term.write("  ")
  95.     end
  96.     term.setBackgroundColor(colors.black)
  97. end
  98.  
  99. function drawMap()
  100.     for id, data in pairs(levels['1']['map']) do
  101.         for f = 1, #data do
  102.             char = string.sub(data, f, f)
  103.             term.setCursorPos(f-scroll['x'], id-scroll['y'])
  104.             if(char == "#")then
  105.                 term.setBackgroundColor(colors.yellow)
  106.             elseif(char == "B")then
  107.                 term.setBackgroundColor(colors.lightBlue)
  108.             elseif(char == "S")then
  109.                 term.setBackgroundColor(colors.pink)
  110.             elseif(char == "X")then
  111.                 term.setBackgroundColor(colors.red)
  112.             end
  113.             if(char ~= " ")then
  114.                 term.write(" ")
  115.             end
  116.         end
  117.     end
  118.     term.setBackgroundColor(colors.black)
  119. end
  120.  
  121. function checkScroll()
  122.     if(#levels['1']['map'] > h)then
  123.         if(ball['drawx'] < 25)then
  124.             if(scroll['x'] > 0)then
  125.                 scroll['x'] = scroll['x'] - 1
  126.                 ball['drawx'] = 25
  127.             end
  128.         else
  129.             calc = scroll['x'] + (scroll['x'] - ball['drawx']) + (scroll['x']+w-ball['drawx']) + 2
  130.             if(string.sub(levels['1']['map'][ball['drawy']], calc, calc) ~= "")then
  131.                 if(w-ball['drawx'] == 25)then
  132.                     scroll['x'] = scroll['x'] + 1
  133.                     ball['drawx'] = 25
  134.                 end
  135.             end
  136.         end
  137.     end
  138.     if(#levels['1']['map'] > h)then
  139.         if(ball['drawy'] < 9)then
  140.             if(scroll['y'] > 0)then
  141.                 scroll['y'] = scroll['y'] - 1
  142.                 ball['drawy'] = 9
  143.             end
  144.         else
  145.             if(levels['1']['map'][ball['y']+10] ~= nil)then
  146.                 if(h-ball['drawy'] == 9)then
  147.                     scroll['y'] = scroll['y'] + 1
  148.                     ball['drawy'] = 9
  149.                 end
  150.             end
  151.         end
  152.     end
  153. end
  154.  
  155. function drawScreen()
  156.     checkScroll()
  157.     drawBall()
  158.     drawMap()
  159. end
  160.  
  161. drawScreen()
  162.  
  163. while true do
  164.     event, key = os.pullEvent("key")
  165.     if key == 205 then
  166.         if(canMove(ball['x']+1, ball['y'], ball['size']))then
  167.             ball['x'] = ball['x'] + 1
  168.             if(ball['x'] < 26)then
  169.                 ball['drawx'] = ball['x']
  170.             else
  171.                 ball['drawx'] = ball['drawx'] + 1
  172.             end
  173.         end
  174.     elseif key == 203 then
  175.         if(canMove(ball['x']-1, ball['y'], ball['size']))then
  176.             ball['x'] = ball['x'] - 1
  177.             if(ball['x'] < 26)then
  178.                 ball['drawx'] = ball['x']
  179.             else
  180.                 ball['drawx'] = ball['drawx'] - 1
  181.             end
  182.         end
  183.     elseif key == 200 then
  184.         if(canMove(ball['x'], ball['y']-1, ball['size']))then
  185.             ball['y'] = ball['y'] - 1
  186.             if(ball['y'] < 10)then
  187.                 ball['drawy'] = ball['y']
  188.             else
  189.                 ball['drawy'] = ball['drawy'] - 1
  190.             end
  191.         end
  192.     elseif key == 208 then
  193.         if(canMove(ball['x'], ball['y']+1, ball['size']))then
  194.             ball['y'] = ball['y'] + 1
  195.             if(ball['y'] < 10)then
  196.                 ball['drawy'] = ball['y']
  197.             else
  198.                 ball['drawy'] = ball['drawy'] + 1
  199.             end
  200.         end
  201.     elseif key == 42 then
  202.         if ball['size'] == 1 then
  203.             if(canMove(ball['x'], ball['y'], ball['size']+1))then
  204.                 ball['size'] = 2
  205.             end
  206.         end
  207.     elseif key == 29 then
  208.         if ball['size'] == 2 then
  209.             ball['size'] = 1
  210.         end
  211.     end
  212.     drawScreen()
  213. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement