Advertisement
CaptainSpaceCat

Break Loop

Jul 16th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.93 KB | None | 0 0
  1. local w, h = term.getSize()
  2. local levels = {
  3.     [0] = {
  4.         "",
  5.         "",
  6.         "",
  7.         "",
  8.         "",
  9.         "",
  10.         "",
  11.         "",
  12.         "                      REBOOT                      ",
  13.         "                       [  ]                       ",
  14.         "",
  15.         "",
  16.         "",
  17.         "",
  18.         "",
  19.         "",
  20.         "",
  21.         "",
  22.         ""
  23.           },
  24.     [1] = {
  25.         "_l0c?l ???? = ????",
  26.         "????????",
  27.         "",
  28.         "?????????_",
  29.         "???????_",
  30.         "?????? = _{????}",
  31.         "",
  32.         "????????",
  33.         "?????????_",
  34.         "????????_",
  35.         "???????",
  36.         "",
  37.         "???????_",
  38.         "????/????",
  39.         "????? = ???",
  40.         "????_????",
  41.         "???????????_",
  42.         "????????_",
  43.         "?????????_"
  44.           },
  45.     [2] = {
  46.         "for ??, ? ?? o",
  47.         "  x??7?",
  48.         "  ? ?? > 9 t??",
  49.         " ?   ??f????_",
  50.         " ?nd_",
  51.         "e?d",
  52.         "",
  53.         "?un?t??n??l ?d?(?, ??)",
  54.         " ?? o???????_",
  55.         "  ?? ?? ??_",
  56.         "?nd????_",
  57.         "",
  58.         "  ?? ??_",
  59.         "????/??>?",
  60.         "loc??? an ?? <?_ or??",
  61.         "????_nil ???",
  62.         "????? ?????_",
  63.         "?? i?????_",
  64.         "????//\"????_"
  65.           }
  66. }
  67.  
  68. local cInfo = {}
  69. for i = 0, #levels do
  70.     cInfo[i] = {}
  71.     for n = 1, 19 do
  72.         cInfo[i][n] = {}
  73.     end
  74. end
  75.  
  76. local cLvl = 1
  77. local ticknum = 5
  78. local cSpeed = 4
  79. local damageTick
  80.  
  81. local timers = {
  82.     gametick = os.startTimer(.05)
  83. }
  84.  
  85. local function restartTimers()
  86.     timers = {
  87.         gametick = os.startTimer(.05),
  88.         invuln = os.startTimer(.05)
  89.     }
  90. end
  91.  
  92. local returnTick = {
  93.     [0] = function() if ticknum%5 == 0 then return true end; return false end,
  94.     [1] = function() if ticknum%4 == math.random(0, 3) then return true end; return false end,
  95.     [2] = function() if ticknum%4 == 0 then return true end; return false end,
  96.     [3] = function() if ticknum%3 == math.random(0, 2) then return true end; return false end,
  97.     [4] = function() if ticknum%3 == 0 then return true end; return false end,
  98.     [5] = function() if ticknum%2 == math.random(0, 1) then return true end; return false end,
  99.     [6] = function() if ticknum%2 == 0 then return true end; return false end,
  100.     [7] = function() return true end
  101. }
  102.  
  103. local player = {
  104.     color = colors.lightBlue,
  105.     pos = {x = w - 1, y = 1, char = " ", txt = colors.white, back = colors.black},
  106.     prev = {x = w - 1, y = 1, char = " ", txt = colors.white, back = colors.black},
  107.     lives = 3,
  108.     invuln = false,
  109.     health = 100
  110. }
  111.  
  112. local function printLevel(lvl)        ---also buffers it
  113.     for i, v in pairs(levels[lvl]) do
  114.         local offset = 0
  115.         local unders = 0
  116.         for j = 1, string.len(v) do
  117.             term.setCursorPos(j + offset, i)
  118.             term.setTextColor(colors.white)
  119.             if v:sub(j, j) == "?" then
  120.                 if math.random(1, 5) == 1 then
  121.                     term.setTextColor(2^math.random(1, 15))
  122.                 end
  123.                 local char = math.random(0, 126)
  124.                 term.write(string.char(char))
  125.                 cInfo[cLvl][i][j + offset] = char
  126.             elseif v:sub(j, j) == "_" then
  127.                 unders = unders + 1
  128.                 term.setCursorPos(j, i)
  129.                 local addon = math.random(0, cSpeed*2)
  130.                 for n = 1, addon do
  131.                     if math.random(1, 5) == 1 then
  132.                         term.setTextColor(2^math.random(1, 15))
  133.                     end
  134.                     local char = math.random(0, 126)
  135.                     term.write(string.char(char))
  136.                     cInfo[cLvl][i][j + n] = char
  137.                 end
  138.                 offset = offset + addon
  139.             else
  140.                 term.write(v:sub(j, j))
  141.                 cInfo[cLvl][i][j + offset] = v:sub(j, j)
  142.             end
  143.         end
  144.         term.write(string.rep(" ", cSpeed*2*unders))
  145.         if #cInfo[cLvl][i] > string.len(v) + offset - unders then
  146.             for n = string.len(v) + offset - unders + 1, #cInfo[cLvl][i] do
  147.                 cInfo[cLvl][i][n] = nil
  148.             end
  149.         end
  150.     end
  151. end
  152.  
  153. local function drawInfoBars()
  154.     term.setCursorPos(w, 19)
  155.     term.setBackgroundColor(colors.black)
  156.     term.write(player.lives)
  157.     local color = player.invuln and colors.blue or player.color
  158.     for i = 1, 18 do
  159.         term.setCursorPos(w, i)
  160.         term.setBackgroundColor(player.health/100 >= (18 - i + 1)/18 and color or colors.black)
  161.         term.write(" ")
  162.     end
  163. end
  164.  
  165. local function transition(dir)
  166.     local st, en, inc
  167.     if dir == "up" then
  168.         st, en, inc = 1, 19, 1
  169.         player.pos.y = 19
  170.     elseif dir == "down" then
  171.         st, en, inc = 19, 1, -1
  172.         player.pos.y = 1
  173.     end
  174.     term.setBackgroundColor(colors.gray)
  175.     for i = st, en, inc do
  176.         term.setCursorPos(1, i)
  177.         term.clearLine()
  178.         sleep(.01)
  179.     end
  180.     term.setBackgroundColor(colors.black)
  181.     for i = st, en, inc do
  182.         term.setCursorPos(1, i)
  183.         term.clearLine()
  184.         sleep(.01)
  185.     end
  186.     player.prev.char = " "
  187.     player.prev.back = colors.black
  188.     printLevel(cLvl)
  189.     restartTimers()
  190. end
  191.  
  192. local function printPlayer()   --again, also buffers
  193.     term.setBackgroundColor(player.invuln and colors.blue or player.color)
  194.     term.setTextColor(player.pos.txt)
  195.     term.setCursorPos(player.pos.x, player.pos.y)
  196.     term.write(player.pos.char)
  197.     player.prev = {x = player.pos.x, y = player.pos.y, char = player.pos.char, txt = player.pos.txt, back = player.pos.back}
  198. end
  199.  
  200. local function updateScreen()
  201.     term.setBackgroundColor(colors.black)
  202.     if returnTick[cSpeed]() then
  203.         damageTick = true
  204.         printLevel(cLvl)
  205.     end
  206.     if player.pos ~= player.prev then
  207.         term.setCursorPos(player.prev.x, player.prev.y)
  208.         term.setBackgroundColor(player.prev.back)
  209.         term.setTextColor(player.prev.txt)
  210.         term.write(player.prev.char)
  211.     end
  212.     printPlayer()
  213. end
  214.  
  215. local function updateGame()
  216.     if cInfo[cLvl][player.pos.y] and cInfo[cLvl][player.pos.y][player.pos.x] and type(cInfo[cLvl][player.pos.y][player.pos.x]) == "number" and damageTick and not player.invuln then
  217.         player.health = player.health - 10
  218.         drawInfoBars()
  219.     end
  220.     if cInfo[cLvl][player.pos.y] and cInfo[cLvl][player.pos.y][player.pos.x] then
  221.         if type(cInfo[cLvl][player.pos.y][player.pos.x]) == "string" then
  222.             player.pos.char = cInfo[cLvl][player.pos.y][player.pos.x]
  223.         else
  224.             player.pos.char = string.char(cInfo[cLvl][player.pos.y][player.pos.x])
  225.         end
  226.     else
  227.         player.pos.char = " "
  228.     end
  229.     if player.pos.y < 1 then
  230.         cLvl = cLvl - 1
  231.         transition("up")
  232.     elseif player.pos.y > 19 then
  233.         cLvl = cLvl + 1
  234.         transition("down")
  235.     end
  236.     if player.health <= 0 then
  237.         player.health = 100
  238.         player.lives = player.lives - 1
  239.         player.invuln = true
  240.         drawInfoBars()
  241.         timers.invuln = os.startTimer(3)
  242.     end
  243. end
  244.  
  245. local function eventHandler(events)
  246.     if events[1] == "key" then
  247.         if events[2] == keys.up then
  248.             if cLvl > 0 or player.pos.y > 1 then
  249.                 player.pos.y = player.pos.y - 1
  250.             end
  251.         elseif events[2] == keys.right and player.pos.x < w - 1 then
  252.             player.pos.x = player.pos.x + 1
  253.         elseif events[2] == keys.down then
  254.             if cLvl < #levels or player.pos.y < 19 then
  255.                 player.pos.y = player.pos.y + 1
  256.             end
  257.         elseif events[2] == keys.left and player.pos.x > 1 then
  258.             player.pos.x = player.pos.x - 1
  259.         end
  260.     end
  261. end
  262.  
  263. term.setBackgroundColor(colors.black)
  264. term.clear()
  265. updateScreen()
  266. drawInfoBars()
  267. while player.lives > 0 do
  268.     local events = {os.pullEvent()}
  269.     damageTick = false
  270.     if events[1] == "timer" then
  271.         if events[2] == timers.gametick then
  272.             updateScreen()
  273.             ticknum = ticknum + 1
  274.             if ticknum > 20 then ticknum = 1 end
  275.             timers.gametick = os.startTimer(.05)
  276.         end
  277.         if events[2] == timers.invuln then
  278.             player.invuln = false
  279.             drawInfoBars()
  280.         end
  281.     else
  282.         eventHandler(events)
  283.     end
  284.     updateGame()
  285. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement