Advertisement
LDDestroier

Wall Dodge

Jul 21st, 2016
1,098
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.35 KB | None | 0 0
  1. --[[
  2.   Wall Dodge! What a riveting game!
  3.   Dodge the walls before they kill you.
  4.  
  5.   Download with:
  6.    pastebin get fDTts7wz dodge
  7.    std PB fDTts7wz dodge
  8.    std ld dodge dodge
  9. --]]
  10.  
  11. local scr_x, scr_y = term.getSize()
  12. local keysDown = {} --holds all pressed keys. It's way better than using "key" event for movement
  13. local walls = {}    --holds all screen data for walls. I could do slants if I wanted, not just walls
  14. local frame = 0     --for every screen update-oh, you know what a frame is
  15. local maxFrame = 26 --max frames until new wall
  16. local fframe = 0    --not a typo. is the buffer of spaces until the spaces between walls shrinks
  17. local maxFFrame = 6 --max fframes until the space between walls gets slightly tighter (down to 5, good luck m8)
  18. local pause = false --pausing is a nice thing
  19. local tsv = term.current().setVisible --it is my belief that monitors and normal computers do not have the setVisible method for term.current()
  20. for a = 1, scr_x do
  21.     table.insert(walls,{top=1,bottom=scr_y,color=colors.black})
  22. end
  23.  
  24. local score = 0  --increases for every wall.
  25. local time = 0   --in seconds, increases in increments of 0.1
  26.  
  27. local addNewWall = function(top,bottom,color)
  28.     table.remove(walls,1)
  29.     table.insert(walls,{top=top,bottom=bottom,color=color})
  30. end
  31.  
  32. local guyX = 2
  33. local guyY = math.floor(scr_y/2)
  34.  
  35. local maxY = scr_y-1
  36. local minY = 2
  37.  
  38. local clearLines = function(y1,y2)
  39.     local _x,_y = term.getCursorPos()
  40.     for a = y1, y2 or y1 do
  41.         term.setCursorPos(1,a)
  42.         term.clearLine()
  43.     end
  44.     term.setCursorPos(_x,_y)
  45. end
  46.  
  47. local renderTEXT = function(_txt)
  48.     local txt = _txt or "YOU ARE DEAD"
  49.     local midY = math.floor(scr_y/2)
  50.     for a = 0, 2 do
  51.         term.setBackgroundColor(colors.gray)
  52.         clearLines(midY-a,midY+a)
  53.         sleep(0.1)
  54.     end
  55.     term.setCursorPos(4,midY)
  56.     term.write(txt)
  57. end
  58.  
  59. local trymove = function(dir)
  60.     if (guyY+dir)>=minY and (guyY+dir)<=maxY then
  61.         guyY = guyY + dir
  62.         return true
  63.     end
  64.     return false
  65. end
  66.  
  67. local render = function()
  68.     if tsv then tsv(false) end
  69.     term.setBackgroundColor(colors.white)
  70.     term.setTextColor(colors.white)
  71.     term.clear()
  72.     term.setCursorPos(guyX,guyY)
  73.     term.setBackgroundColor(colors.black)
  74.     term.write(" ")
  75.     term.setCursorPos(1,1)
  76.     term.clearLine()
  77.     term.setCursorPos(1,scr_y)
  78.     term.clearLine()
  79.     for x = 1, #walls do
  80.         term.setBackgroundColor(walls[x].color)
  81.         for y = 1, walls[x].top do
  82.             term.setCursorPos(x,y)
  83.             term.write(" ")
  84.         end
  85.         for y = walls[x].bottom, scr_y do
  86.             term.setCursorPos(x,y)
  87.             term.write(" ")
  88.         end
  89.     end
  90.     term.setCursorPos(2,1)
  91.     term.setBackgroundColor(colors.black)
  92.     term.write("SCORE: "..score.." | TIME: "..time)
  93.     if tsv then tsv(true) end
  94. end
  95.  
  96. local keepTime = function()
  97.     time = 0
  98.     while true do
  99.         sleep(0.1)
  100.         if not pause then
  101.             time = time + 0.1
  102.         end
  103.     end
  104. end
  105.  
  106. local doGame = function()
  107.     local wf = 0
  108.     local gap = 2
  109.     local ypos, randomcol
  110.     while true do
  111.         if not pause then
  112.             if frame >= maxFrame or wf > 0 then
  113.                 if frame >= maxFrame then
  114.                     frame = 0
  115.                     fframe = fframe + 1
  116.                     ypos = math.random(4,scr_y-3)
  117.                     wf = 3
  118.                     randomcol = 2^math.random(1,14)
  119.                 end
  120.                 if wf > 0 then
  121.                     wf = wf - 1
  122.                 end
  123.                 if not term.isColor() then
  124.                     randomcol = colors.black --Shame.
  125.                 end
  126.                 addNewWall(ypos-gap,ypos+gap,randomcol)
  127.             else
  128.                 frame = frame + 1
  129.                 addNewWall(1,scr_y,colors.black)
  130.             end
  131.             if fframe >= maxFFrame then
  132.                 fframe = 0
  133.                 if maxFrame > 7 then
  134.                     maxFrame = maxFrame - 1
  135.                 end
  136.             end
  137.             if keysDown[keys.up] then
  138.                 trymove(-1)
  139.             end
  140.             if keysDown[keys.down] then
  141.                 trymove(1)
  142.             end
  143.             if walls[guyX-1].top > 1 or walls[guyX-1].bottom < scr_y then
  144.                 if walls[guyX].top < walls[guyX-1].top or walls[guyX].bottom > walls[guyX-1].bottom then
  145.                     score = score + 1
  146.                 end
  147.             end
  148.             render()
  149.         end
  150.         sleep(0)
  151.         if guyY<=walls[guyX].top or guyY>=walls[guyX].bottom then
  152.             return "dead"
  153.         end
  154.     end
  155. end
  156.  
  157. local getInput = function()
  158.     while true do
  159.         local evt = {os.pullEvent()}
  160.         if evt[1] == "key" then
  161.             if evt[2] == keys.q then
  162.                 return "quit"
  163.             end
  164.             if evt[2] == keys.p then
  165.                 pause = not pause
  166.                 if pause then
  167.                     local pauseMSGs = {
  168.                         "PAUSED",
  169.                         "Paused. Press 'P' to resume",
  170.                         "The game is paused",
  171.                         "GAME PAUSE !",
  172.                         "What, gotta catch your breath??",
  173.                         "Paused, the game is, hmmm?",
  174.                         "PAUSED GAME",
  175.                         "GAME PAUSED",
  176.                         "THE GAME IS PAUSED",
  177.                         "THE PAUSED IS GAME",
  178.                         "Buh-buh-buh-BEEP",
  179.                         "UNPAUSE WITH 'P'",
  180.                         "Tip: press UP to go up",
  181.                         "Tip: press DOWN to go down",
  182.                         "Tip: read Narbonic comic, you tool",
  183.                         "Tip: read Skin Horse comic, you twat",
  184.                         "YOU HAVE NO CHANCE TO SURVIVE MAKE YOUR TIME",
  185.                         "-PAUSED-",
  186.                         "=PAUSED=",
  187.                         "PAISED",
  188.                         "THOUST GAME BE PAUSETH",
  189.                         "Yon game is paused. Obvious exits are 'Q', 'CTRL+T'",
  190.                         "Tip: don't hit the walls",
  191.                         "Tip: press 'P' to pause the game",
  192.                     }
  193.                     renderTEXT(pauseMSGs[math.random(1,#pauseMSGs)])
  194.                     keysDown[keys.up] = false
  195.                     keysDown[keys.down] = false
  196.                 end
  197.             end
  198.             keysDown[evt[2]] = true
  199.         end
  200.         if evt[1] == "key_up" then
  201.             keysDown[evt[2]] = false
  202.         end
  203.     end
  204. end
  205.  
  206. local uut = parallel.waitForAny(getInput,doGame,keepTime)
  207. if uut == 2 then
  208.     renderTEXT()
  209. end
  210. sleep(0.05)
  211. term.setCursorPos(1,scr_y)
  212. term.setBackgroundColor(colors.black)
  213. term.clearLine()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement