Advertisement
nitrogenfingers

skyfall

Aug 30th, 2015
1,188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.93 KB | None | 0 0
  1. --[[    Skyfall
  2.         By Nitrogenfingers
  3. ]]--
  4.  
  5. local version = 1
  6. local reponame = "Skyfall"
  7. local requiresColour = true
  8. if requiresColour then
  9.     print("This game can only be played on an Advanced Computer.")
  10. end
  11.  
  12. local function displayNitrosoftTitle()
  13.     shell.run("clear")
  14.     local _w,_h = term.getSize()
  15.     local _t1,_t2 = "nitrosoft", "games"
  16.     term.setCursorPos(math.floor(_w/2-#_t1), math.floor(_h/2))
  17.     if term.isColour() then term.setTextColour(colours.blue) end
  18.     term.write(_t1)
  19.     term.setCursorPos(math.floor(_w/2-#_t2/2),math.floor(_h/2)+1)
  20.     term.setTextColour(colours.white)
  21.     term.write(_t2)
  22.     if term.isColour() then term.setTextColour(colours.red) end
  23.     term.setCursorPos(math.floor(_w/2-#_t1), math.floor(_h/2)+2)
  24.     term.write(string.rep("-",#_t1*2))
  25.     if term.isColour() then
  26.         term.setBackgroundColour(colours.green)
  27.         term.setCursorPos(_w/2+#_t1-4, math.floor(_h/2)-2)
  28.         term.write("  ")
  29.         term.setCursorPos(_w/2+#_t1-4, math.floor(_h/2)-1)
  30.         term.write("  ")
  31.         term.setBackgroundColour(colours.lime)
  32.         term.setCursorPos(_w/2+#_t1-3, math.floor(_h/2)-1)
  33.         term.write("  ")
  34.         term.setCursorPos(_w/2+#_t1-3, math.floor(_h/2))
  35.         term.write("  ")
  36.     end
  37.     term.setBackgroundColour(colours.black)
  38.     term.setTextColour(colours.white)
  39.     term.setCursorPos(1,_h)
  40.     term.write("v"..version)
  41.     os.pullEvent("key")
  42. end
  43. displayNitrosoftTitle()
  44.  
  45. local w,h = term.getSize()
  46.  
  47. local levelbg = {colours.lightBlue, colours.blue, colours.black}
  48. local level = {}
  49. local title = {
  50.     "##### ##  # ## ## #####  ###  ##  ## ";
  51.     "##    ## ## ## ## ##    ##### ##  ## ";
  52.     "##### ####  ##### ###   ## ## ##  ## ";
  53.     "   ## ## ##  ###  ##    ##### ### ###";
  54.     "##### ##  #  ###  ##    ## ## ### ###";
  55. }
  56.  
  57. local bgcol = colours.lightBlue
  58. local pcol = colours.red
  59. local flakes = {}
  60. local px, py = math.floor(w/2), h
  61. local pjump = 0
  62. local jtimer, jinterval = nil,0.06
  63. local gameOver = false
  64. local levelnum = 1
  65.  
  66. local gintdef = 0.3
  67. local gtimer, ginterval = nil,gintdef
  68.  
  69. local function makeLevel()
  70.     for y = 1, h+1 do
  71.         level[y] = {}
  72.     end
  73.     for x = 1, w do
  74.         level[h+1][x] = "#"
  75.     end
  76.     flakes = {}
  77. end
  78.  
  79. local function updateTile(x,y)
  80.     term.setCursorPos(x, y)
  81.     if level[y][x] == nil then term.setBackgroundColour(bgcol)
  82.     else term.setBackgroundColour(colours.white) end
  83.     term.write(" ")
  84. end
  85.  
  86. local function drawPlayer()
  87.     if py <= 0 then return end
  88.     if level[py][px] == nil then
  89.         term.setCursorPos(px, py)
  90.         term.setTextColour(pcol)
  91.         term.setBackgroundColour(bgcol)
  92.         term.write("&")
  93.     end
  94. end
  95.  
  96. local draw = function(self)
  97.     term.setCursorPos(self.x,self.y)
  98.     term.setBackgroundColour(colours.white)
  99.     term.write(" ")
  100. end
  101.  
  102. local function drawLevel()
  103.     for y = 1, h do
  104.         for x = 1, w do
  105.             updateTile(x,y)
  106.         end
  107.     end
  108.     drawPlayer()
  109.     for i=1,#flakes do
  110.         term.setCursorPos(flakes[i].x, flakes[i].y)
  111.         term.setBackgroundColour(colours.white)
  112.         tern.write(" ")
  113.     end
  114. end
  115.  
  116. local function addFlake()
  117.     local x,y = math.random(1,w),1
  118.     level[y][x] = "#"
  119.     updateTile(x,y)
  120. end
  121.  
  122. local function updateBG()
  123.     term.setBackgroundColour(bgcol)
  124.     term.setTextColour(colours.white)
  125.     for i=1,#flakes do
  126.         updateTile(flakes[i].x, flakes[i].y)
  127.         flakes[i].y = flakes[i].y + 1
  128.         if level[flakes[i].y][flakes[i].x] == nil then
  129.             term.setCursorPos(flakes[i].x, flakes[i].y)
  130.             term.setBackgroundColour(bgcol)
  131.             term.write(".")
  132.         end
  133.     end
  134.     for i=1,2 do
  135.         table.insert(flakes, {x = math.random(1,w), y = 1})
  136.     end
  137.     for i = #flakes,1,-1 do
  138.         if flakes[i].y >= h + 1 then
  139.             table.remove(flakes, i)
  140.         end
  141.     end
  142.     drawPlayer()
  143. end
  144.  
  145. local function updateSnow()
  146.     for y = h-1,1,-1 do
  147.         for x = 1,w do
  148.             if level[y][x] ~= nil and level[y+1][x] == nil then
  149.                 level[y][x] = nil
  150.                 updateTile(x,y)
  151.                 level[y+1][x] = "#"
  152.                 updateTile(x,y+1)
  153.             end
  154.         end
  155.     end
  156.    
  157.     if level[py][px] ~= nil then
  158.         if level[py+1][px] == nil then
  159.             py = py + 1
  160.             pjump = -1
  161.         else
  162.             gameOver = true
  163.         end
  164.     end
  165. end
  166.  
  167. local function transitionLevel()
  168.     levelnum = levelnum + 1
  169.     bgcol = levelbg[math.min(levelnum, #levelbg)]
  170.     term.setBackgroundColour(bgcol)
  171.     local ttimer = os.startTimer(0.1)
  172.     local inc = 0
  173.     while inc < h do
  174.         local id,p1 = os.pullEvent("timer")
  175.         if p1 == ttimer then
  176.             term.scroll(-1)
  177.             inc = inc + 1
  178.             ttimer = os.startTimer(0.1)
  179.         end
  180.     end
  181.     makeLevel()
  182.     ginterval = math.max(ginterval - 0.1, 0.1)
  183.     py = h
  184.     gtimer = os.startTimer(ginterval)
  185.     jtimer = os.startTimer(jinterval)
  186. end
  187.  
  188. local function update()
  189.     local bgcount = 0
  190.     gtimer = os.startTimer(ginterval)
  191.     jtimer = os.startTimer(jinterval)
  192.     drawLevel()
  193.     while not gameOver do
  194.         local _id, _p1 = os.pullEvent()
  195.        
  196.         if _id == "timer" then
  197.             if _p1 == gtimer then
  198.                 updateSnow()
  199.                 for i=0, math.random(0,levelnum) do
  200.                     addFlake()
  201.                 end
  202.                 gtimer = os.startTimer(ginterval)
  203.             elseif _p1 == jtimer then
  204.                 if pjump > 0 then
  205.                     updateTile(px,py)
  206.                     py = py -1
  207.                     pjump = pjump - 1
  208.                     if py <= 1 then
  209.                         transitionLevel()
  210.                         pjump = 0
  211.                     end
  212.                    
  213.                 elseif level[py+1][px] == nil then
  214.                     updateTile(px,py)
  215.                     py = py + 1
  216.                 else
  217.                     pjump = 0
  218.                 end
  219.                 drawPlayer()
  220.                 jtimer = os.startTimer(jinterval)
  221.             end
  222.             bgcount = bgcount + 1
  223.             if bgcount % 8 == 0 then updateBG() end
  224.         elseif _id == "key" then
  225.             if _p1 == keys.left and px > 1 then
  226.                 updateTile(px,py)
  227.                 if level[py][px-1] == nil then
  228.                     px = px - 1
  229.                 elseif level[py-1][px] == nil and level[py-1][px-1] == nil then
  230.                     px = px - 1
  231.                     py = py - 1
  232.                 end
  233.                 drawPlayer()
  234.             elseif _p1 == keys.right and px < w then
  235.                 updateTile(px,py)
  236.                 if level[py][px+1] == nil then
  237.                     px = px + 1
  238.                 elseif level[py-1][px] == nil and level[py-1][px+1] == nil then
  239.                     px = px + 1
  240.                     py = py - 1
  241.                 end
  242.                 drawPlayer()
  243.             elseif _p1 == keys.space and level[py+1][px] ~= nil then
  244.                 pjump = 2
  245.             elseif _p1 == keys.enter then
  246.                 gameOver = true
  247.             end
  248.         end
  249.     end
  250. end
  251.  
  252. makeLevel()
  253. update()
  254. term.setBackgroundColour(colours.black)
  255. shell.run("clear")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement