RatcheT2497

Grav Jump

Nov 20th, 2016
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.54 KB | None | 0 0
  1.  
  2. if not term.isColor() or not term.isColor then
  3.     error("This game requires an advanced computer!")
  4. end
  5. local running = true
  6. local maintimer = os.startTimer(0.09)
  7. local randomtimer = 0
  8. local platforms = {}
  9.  
  10. local gamestate = 0
  11.  
  12. local scroll = 0
  13. local playingarea_y1 = 4
  14. local playingarea_y2 = 19 - 4
  15. local player_x = 1
  16. local player_y = 1
  17. local player_gravity = 0
  18. local canchange = true
  19. local meters = 0
  20.  
  21. local width, height = term.getSize()
  22.  
  23. function point_to_aabb(x, y, x2, y2, w2, h2)
  24.     return (
  25.         x >= x2 and
  26.         x <= x2 + w2 and
  27.         y >= y2 and
  28.         y <= y2 + h2
  29.     )
  30. end
  31.  
  32. function _vertical_line_aabb(x, y1, y2, xb1, yb1, xbw, ybh)
  33.     if y1 > y2 then
  34.         return (
  35.             x >= xb1 and
  36.             x <= xb1 + xbw and
  37.             y1 >= yb1 and
  38.             y2 <= yb1 + ybh
  39.         )
  40.     else
  41.         return (
  42.             x >= xb1 and
  43.             x <= xb1 + xbw and
  44.             y2 >= yb1 and
  45.             y1 <= yb1 + ybh
  46.         )
  47.     end
  48. end
  49. function vertical_line_aabb(x, y1, y2)
  50.     for _, v in pairs(platforms) do
  51.         if (_vertical_line_aabb(x, y1, y2, v.x, v.y, v.width, v.height)) then
  52.             return true
  53.         end
  54.     end
  55.     return false
  56. end
  57. function writeout(x, y, text, color)
  58.     term.setCursorPos(x, y)
  59.     term.setTextColor(color)
  60.     term.write(text)
  61. end
  62. function createplatform(xx, yy, w, h)
  63.     local platform = {x = xx; y = yy; width = w; height = h;}
  64.     platform.color = 2 ^ math.random(1, 14)
  65.     if platform.color == colors.lightBlue then
  66.         while platform.color == colors.lightBlue do
  67.             platform.color = 2 ^ math.random(1, 14)
  68.         end
  69.     end
  70.     table.insert(platforms, platform)
  71. end
  72. function drawplayer()
  73.     term.setCursorPos(player_x - scroll, player_y + playingarea_y1 - 1)
  74.     term.setBackgroundColor(colors.white)
  75.     term.setTextColor(colors.black)
  76.     term.write((player_gravity == 0) and "v" or "^")
  77. end
  78.  
  79. function moveplayer()
  80.     player_x = player_x + 1
  81.     scroll = scroll + 1
  82.     if player_gravity == 0 then
  83.         local down = false
  84.         for _, v in pairs(platforms) do
  85.             if point_to_aabb(player_x, player_y+1, v.x, v.y, v.width, v.height) then
  86.                 down = true
  87.                 break;
  88.             end
  89.         end
  90.         player_y = player_y + ((down) and 0 or 1)
  91.         for _, v in pairs(platforms) do
  92.             if point_to_aabb(player_x, player_y + 1, v.x, v.y, v.width, v.height) or point_to_aabb(player_x, player_y, v.x, v.y, v.width, v.height) then
  93.                 player_y = v.y - 1
  94.                 canchange = true
  95.                 break;
  96.             end
  97.         end
  98.     else
  99.         local up = false
  100.         for _, v in pairs(platforms) do
  101.             if point_to_aabb(player_x, player_y, v.x, v.y, v.width, v.height) then
  102.                 up = true
  103.                 break;
  104.             end
  105.         end
  106.         player_y = player_y - ((up) and 0 or 1)
  107.         for _, v in pairs(platforms) do
  108.             if point_to_aabb(player_x, player_y + 1, v.x, v.y, v.width, v.height) or point_to_aabb(player_x, player_y - 1, v.x, v.y, v.width, v.height) then
  109.                 player_y = v.y + 1
  110.                 canchange = true
  111.                 break;
  112.             end
  113.         end
  114.        
  115.     end
  116. end
  117.  
  118. function drawplatform(platform)
  119.     paintutils.drawFilledBox(platform.x - scroll, platform.y + playingarea_y1 - 1, platform.x + platform.width - scroll, platform.y + platform.height + playingarea_y1 - 2, platform.color)
  120. end
  121. function drawplatforms()
  122.     for _, v in pairs(platforms) do
  123.         drawplatform(v)
  124.     end
  125. end
  126. local levelwidth = 51
  127.  
  128. local _random_dir = 0
  129. local last_random_platform_x = 20
  130. local last_random_platform_y = 8
  131. local randomy = 0
  132. function randomplatform()
  133.     last_random_platform_y = randomy
  134.     randomy = math.random(2, 10)
  135.  
  136.     while math.abs(randomy - last_random_platform_y) < 4 do
  137.         randomy = math.random(2, 10)
  138.     end
  139.    
  140.     createplatform(
  141.                 last_random_platform_x,
  142.                 randomy,
  143.                 math.random(4, 16),
  144.                 1
  145.     )
  146.     last_random_platform_x = platforms[#platforms].x + platforms[#platforms].width + math.random(0, 3)
  147.     _random_dir = (_random_dir == 0) and 1 or 0
  148. end
  149.  
  150. function initgame()
  151.     meters = 0
  152.     _random_dir = 0
  153.     last_random_platform_x = 20
  154.     term.clear()
  155.     platforms = {}
  156.     createplatform(4, 8, 16, 1)
  157.     player_y = platforms[1].y - 1
  158.     player_x = platforms[1].x + 1
  159.    
  160.     for i = 1, 6 do
  161.         randomplatform()
  162.     end
  163.  
  164.     player_gravity = 0
  165. end
  166. function initgamestart()
  167.     meters = 0
  168.     term.clear()
  169.     player_y = platforms[1].y - 1
  170.     player_x = platforms[1].x + 1
  171.  
  172.     player_gravity = 0
  173. end
  174. function updategame()
  175.     if gamestate == 1 then
  176.         moveplayer()
  177.         meters = meters + 1
  178.         if player_y > (playingarea_y2 - playingarea_y1)+2 or player_y < 0 then gamestate = 2 end
  179.         randomtimer = randomtimer + 0.2
  180.         if randomtimer >= 1 then
  181.             randomplatform()
  182.             randomtimer = 0
  183.         end
  184.  
  185.     end
  186. end
  187. function drawgame()
  188.     if gamestate == 0 then
  189.         local title1 = "Grav"
  190.         local title2 = "Jump"
  191.         local start = "Press <SPACE> to start!"
  192.         local quitstr = "Press <Q> to quit."
  193.  
  194.         paintutils.drawFilledBox(1, playingarea_y1 + 1, width, playingarea_y2 - 1, colors.lightBlue)
  195.         paintutils.drawFilledBox(1, 1, width, playingarea_y1, colors.gray)
  196.         paintutils.drawFilledBox(1, playingarea_y2, width, height, colors.gray)
  197.        
  198.         drawplatforms()
  199.         drawplayer()
  200.         term.setBackgroundColor(colors.gray)
  201.         writeout(math.floor(width / 2) + 1, 2, title2, colors.red)
  202.         writeout(math.floor(width / 2) - #title2, 2, title1, colors.lime)
  203.        
  204.         writeout(math.floor(width / 2) - math.floor(#quitstr / 2), height - 2, quitstr, colors.white)
  205.         writeout(math.floor(width / 2) - math.floor(#start / 2), height - 3, start, colors.white)
  206.     elseif gamestate == 1 then
  207.         paintutils.drawFilledBox(1, playingarea_y1 + 1, width, playingarea_y2 - 1, colors.lightBlue)
  208.         drawplatforms()
  209.         drawplayer()
  210.        
  211.         paintutils.drawFilledBox(1, 1, width, playingarea_y1, colors.gray)
  212.         paintutils.drawFilledBox(1, playingarea_y2, width, height, colors.gray)
  213.         writeout(math.floor(width / 2) - math.ceil(#tostring(meters)/2), 2, tostring(meters), colors.white)
  214.  
  215.     elseif gamestate == 2 then
  216.         local gameover = "You died!"
  217.         local tryagain = "Try again?"
  218.         local restart = "Press <SPACE> to restart!"
  219.         local quit = "Press <Q> to quit."
  220.        
  221.         paintutils.drawFilledBox(1, playingarea_y1 + 1, width, playingarea_y2 - 1, colors.lightBlue)
  222.         drawplatforms()
  223.        
  224.         term.setBackgroundColor(colors.lightBlue)
  225.         writeout(math.ceil(width / 2) - math.ceil(#gameover / 2), math.floor(height / 2) - 2, gameover, colors.red)
  226.         writeout(math.floor(width / 2) - math.floor(#tryagain / 2), math.floor(height / 2), tryagain, colors.red)
  227.         writeout(math.floor(width / 2) - math.floor(#restart / 2), math.floor(height / 2) + 2, restart, colors.black)
  228.         writeout(math.floor(width / 2) - math.floor(#quit / 2), math.floor(height / 2) + 3, quit, colors.black)
  229.        
  230.         paintutils.drawFilledBox(1, 1, width, playingarea_y1, colors.gray)
  231.         paintutils.drawFilledBox(1, playingarea_y2, width, height, colors.gray)
  232.         writeout(math.floor(width / 2) - math.ceil(#tostring(meters)/2), 2, tostring(meters), colors.white)
  233.     end
  234.    
  235. end
  236. function input(e)
  237.     if gamestate == 0 then
  238.         if e[1] == "key" then
  239.             if e[2] == keys.space then
  240.                 scroll = 0
  241.                 initgamestart()
  242.                 gamestate = 1
  243.                 paintutils.drawFilledBox(1, playingarea_y1 + 1, width, playingarea_y2 - 1, colors.lightBlue)
  244.             elseif e[2] == keys.q then
  245.                 running = false
  246.             end
  247.  
  248.         end
  249.     elseif gamestate == 1 then
  250.         if e[1] == "key" then
  251.             if e[2] == keys.space and (vertical_line_aabb(player_x, player_y, 16 * ((player_gravity == 0) and 1 or -1)) or vertical_line_aabb(player_x - 1, player_y, 16 * ((player_gravity == 0) and 1 or -1)) or vertical_line_aabb(player_x + 1, player_y, 16 * ((player_gravity == 0) and 1 or -1))) then
  252.                 if canchange then
  253.                     player_gravity = (player_gravity == 0) and 1 or 0
  254.                     canchange = false
  255.                 end
  256.             end
  257.         end
  258.     elseif gamestate == 2 then
  259.         if e[1] == "key" then
  260.             if e[2] == keys.space then
  261.                 scroll = 0
  262.                 initgame()
  263.                 gamestate = 1
  264.                 paintutils.drawFilledBox(1, playingarea_y1 + 1, width, playingarea_y2 - 1, colors.lightBlue)
  265.                 math.randomseed(math.ceil(os.time() * 10))
  266.             end
  267.         end
  268.     end
  269. end
  270.  
  271. local mon = window.create(term.current(), 1, 1, width, height)
  272. local oldterm = term.redirect(mon)
  273.  
  274. initgame()
  275. while running do
  276.     local e = {os.pullEvent()}
  277.     input(e)
  278.     if e[1] == "timer" then
  279.         if e[2] == maintimer then
  280.             updategame()
  281.             mon.setVisible(true)
  282.             mon.setVisible(false)
  283.             drawgame()
  284.             maintimer = os.startTimer(0.09)
  285.         end
  286.     elseif e[1] == "key" then
  287.         if gamestate == 2 then
  288.             if e[2] == keys.q then running = false; term.setCursorPos(1, 1); term.setBackgroundColor(colors.black); term.clear() end
  289.         end
  290.     end
  291. end
  292.  
  293. term.redirect(oldterm)
  294. local msg = "Thank you for playing GravJump!"
  295. term.setBackgroundColor(colors.black)
  296. term.clear()
  297.    
  298. if width < #msg then
  299.     term.setCursorPos(1, 1)
  300.     term.setTextColor(colors.blue)
  301.     print(msg)
  302. else
  303.     writeout(math.floor(width / 2) - math.floor(#msg / 2), 1, msg, colors.blue)
  304.     term.setCursorPos(1, 2)
  305. end
Add Comment
Please, Sign In to add comment