Advertisement
LDDestroier

SUPER Dodge!! (computercraft)

Aug 6th, 2016
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. SUPER Dodge!!
  3. A remake of that last game I made. Mostly an experiment with cool background.
  4. Get with
  5.  pastebin get 5BUnGkUJ dodge2
  6. And soon
  7.  std ld dodge2 dodge2
  8.  
  9. This game isn't finished, but it is certainly playable.
  10.  
  11. ...you fool!
  12. --]]
  13. local scr_x, scr_y = term.getSize()
  14. local sprite = {}
  15. sprite.dw = {{128,128,128,128,128,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,128,256,256,128,128,},{0,16384,16384,16384,16384,0,},{16384,2,2,2,2,16384,},{16384,2,2,16,16,16384,},{16384,16,16,16,2,16384,},{0,16384,16384,16384,16384,0,},}
  16. sprite.uw = {{0,16384,16384,16384,16384,0,},{16384,16,16,2,2,16384,},{16384,16,2,2,2,16384,},{16384,2,2,16,16,16384,},{0,16384,16384,16384,16384,0,},{128,128,256,256,128,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,256,256,256,256,128,},{128,128,128,128,128,128,},}
  17. sprite.guy = {{2,0,8192,32,32,0},{16384,8192,8192,32,2048,32},{2,0,8192,32,32,0}}
  18. sprite.guybig = {{},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,32,32,32,32,32,32,32,},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,32,32,32,32,32,32,32,32,32,32,32,32,32,},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,32,32,32,32,32,32,32,32,32768,32768,32,32,32,32,32,32,32,},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,32,32,32,32,32,32,32,32,32,32,32768,8,8,32768,32,32,32,32,32,32,},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,32,32,32,32,32,32,32,32,32,32,0,8,8,8,8,32768,32,32,32,32,0,},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,32,32,32,32,32,32,32,32,32,32,32,32,32768,8,8,8,32768,32,32,32,32,32,0,},{0,0,0,0,0,0,0,0,0,0,0,0,0,8192,8192,8192,32,32,32,32,32,32,32,32,32,32,32,32,32768,32768,32768,32,32,32,32,32,0,0,},{0,0,0,0,0,0,0,0,0,0,0,0,0,8192,8192,8192,8192,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,0,0,},{0,0,0,0,0,0,0,0,0,0,0,0,0,8192,8192,8192,8192,8192,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,0,0,0,},{0,0,0,0,0,0,0,0,0,0,0,0,8192,8192,8192,8192,8192,8192,8192,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,0,0,0,0,},{0,0,0,0,0,0,0,0,0,0,0,0,8192,8192,8192,8192,8192,8192,8192,8192,8192,32,32,32,32,32,32,32,32,32,32,32,32,0,0,0,0,0,},{0,0,0,0,0,0,0,0,0,0,0,0,0,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,32,32,32,32,32,32,32,0,0,0,0,0,0,},{0,0,0,0,0,0,0,0,0,0,0,0,256,256,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,0,0,0,0,0,0,0,0,},{0,0,0,0,0,0,0,0,0,0,0,256,256,256,256,256,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,0,0,0,0,0,0,0,0,0,},{0,0,0,0,0,0,0,0,0,0,0,2,2048,256,256,256,256,8192,8192,8192,8192,8192,8192,8192,8192,8192,8192,0,0,0,0,0,0,0,0,0,0,0,},{0,0,0,0,0,0,2,2,2,2,2,2,2048,2048,2048,256,256,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},{0,0,0,0,2,2,2,2,2,2,16,16,16,16,16,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},{0,0,0,2,2,2,2,2,2,16,16,16,16,16,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},{0,0,0,0,0,0,2,2,2,2,16,16,16,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},{0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},{0,0,0,0,0,2,0,0,0,2,2,2,2,2,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},{0,0,0,0,0,0,0,0,0,2,0,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}}
  19. sprite.title = {{1,1,1,1,1,0,1,0,0,0,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,0,0,0,0,0,0,0,},{1,1,1,1,1,0,1,0,0,0,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,0,0,0,0,0,0,0,},{1,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,},{1,1,1,1,1,0,1,0,0,0,1,0,1,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,},{0,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,},{0,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,},{1,1,1,1,1,0,1,1,1,1,1,0,1,0,0,0,0,0,1,1,1,1,1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,},{1,1,1,1,1,0,1,1,1,1,1,0,1,0,0,0,0,0,1,1,1,1,1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,},{},{8,8,8,8,8,0,0,0,0,8,8,8,8,8,0,0,8,8,8,8,8,0,0,0,0,8,8,8,8,0,0,8,8,8,8,8,8,},{8,8,8,8,8,8,0,0,8,8,8,8,8,8,8,0,8,8,8,8,8,8,0,0,8,8,8,8,8,8,0,8,8,8,8,8,8,},{8,8,0,0,8,8,8,0,8,8,0,0,0,8,8,0,8,8,0,0,8,8,8,0,8,8,0,0,8,8,0,8,8,0,0,0,0,},{8,8,0,0,0,8,8,0,8,8,0,0,0,8,8,0,8,8,0,0,0,8,8,0,8,8,0,0,0,0,0,8,8,8,8,8,0,},{8,8,0,0,0,8,8,0,8,8,0,0,0,8,8,0,8,8,0,0,0,8,8,0,8,8,0,8,8,8,0,8,8,0,0,0,0,},{8,8,0,0,8,8,8,0,8,8,0,0,0,8,8,0,8,8,0,0,8,8,8,0,8,8,0,0,8,8,0,8,8,0,0,0,0,},{8,8,8,8,8,8,0,0,8,8,8,8,8,8,8,0,8,8,8,8,8,8,0,0,8,8,8,8,8,8,0,8,8,8,8,8,8,},{8,8,8,8,8,0,0,0,0,8,8,8,8,8,0,0,8,8,8,8,8,0,0,0,0,8,8,8,8,0,0,8,8,8,8,8,8,}}
  20. sprite.bg = {{32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768},{32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768},{4096,32768,32768,32768,32768,32768,32768,32768,32768,4096,4096,4096,4096,4096,4096,4096,4096,4096},{4096,4096,4096,4096,4096,4096,4096,4096,4096,4096,0,0,0,0,0,0,0,0},{},{},{},{},{},{},{},{},{},{},{},{4096,4096,4096,4096,4096,4096,4096,4096,4096,4096,0,0,0,0,0,0,0,0},{4096,32768,32768,32768,32768,32768,32768,32768,32768,4096,4096,4096,4096,4096,4096,4096,4096,4096},{32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768},{32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768,32768}}
  21.  
  22. local gm = {
  23.     x = 2,
  24.     y = math.floor(scr_y/2),
  25.     score = 0,
  26.     hiscore = 0,
  27.     deaths = 0,
  28. }
  29. local walls = {}
  30.  
  31. local keysDown = {}
  32.  
  33. local inc = function(a)
  34.   local x,y = term.getCursorPos()
  35.   term.setCursorPos(x,y+a)
  36. end
  37.  
  38. local addWall = function()
  39.     table.insert(walls,{x=scr_x,y=math.random(4,scr_y-4)})
  40. end
  41.  
  42. local moveWalls
  43. moveWalls = function()
  44.     for k,v in pairs(walls) do
  45.         if walls[k] then
  46.             walls[k].x = walls[k].x - 1
  47.             if walls[k].x <= -5 then
  48.                 walls[k] = nil
  49.                 moveWalls()
  50.                 break
  51.             end
  52.         end
  53.     end
  54. end
  55.  
  56. local renderBG = function(scroll,bgscroll)
  57.     local ivl = 5 --interval
  58.     local skew = 2
  59.     term.setBackgroundColor(colors.black)
  60.     term.clear()
  61.     local pos = (ivl - scroll) + 1
  62.     while pos <= scr_x do
  63.         local endpos = ((pos-(scr_x/2))*(skew))+(scr_x/2)
  64.         local midpos = ((pos-(scr_x/2))*(skew*0.8))+(scr_x/2) -- skew*0.75 is perfect lines
  65.         paintutils.drawLine(endpos, scr_y,      midpos, scr_y*0.75, colors.cyan) --render bottom
  66.         paintutils.drawLine(midpos, scr_y*0.75, pos   , scr_y*0.5,  colors.lightBlue) --render bottom
  67.         paintutils.drawLine(endpos, 1,          midpos, scr_y*0.25, colors.cyan) --render top
  68.         paintutils.drawLine(midpos, scr_y*0.25, pos,    scr_y*0.5,  colors.lightBlue) --render top
  69.         pos = pos + ivl
  70.     end
  71.     for x = 1-bgscroll, scr_x, 18 do
  72.         paintutils.drawImage(sprite.bg,x,1)
  73.     end
  74. end
  75. local gap = 6
  76. local t = term.current().setVisible
  77.  
  78. local checkCollision = function()
  79.     for k,v in pairs(walls) do
  80.         if gm.x >= v.x-3 and gm.x <= v.x+3 then --intentionally allowed front and back to touch wall
  81.             if math.abs((gm.y+1) - v.y) >= (gap/2)-1 then
  82.                 return false
  83.             end
  84.         end
  85.     end
  86.     return true
  87. end
  88.  
  89. local render = function(scroll,bgscroll)
  90.     if t then t(false) end
  91.     renderBG(scroll,bgscroll)
  92.     paintutils.drawImage(sprite.guy,gm.x,gm.y)
  93.    
  94.     for k,v in pairs(walls) do
  95.         paintutils.drawImage(sprite.uw,v.x,v.y+(gap/2))
  96.         paintutils.drawImage(sprite.dw,v.x,(v.y-(gap/2))-scr_y)
  97.     end
  98.    
  99.     term.setCursorPos(2,1)
  100.     term.setBackgroundColor(colors.black)
  101.     term.clearLine()
  102.     write("SCORE: "..gm.score.."  ")
  103.     if t then t(true) end
  104. end
  105.  
  106. local game = function()
  107.     local scroll = 1
  108.     local frame = 0
  109.     local maxframe = 32
  110.     local bgscroll = 0
  111.     while true do
  112.         render(math.floor(scroll),math.floor(bgscroll))
  113.         scroll = scroll + 0.5
  114.         frame = frame + 1
  115.         bgscroll = bgscroll + 2
  116.         if scroll % 5 == 0 then
  117.             scroll = 0
  118.         end
  119.         if frame == maxframe then
  120.             addWall()
  121.             frame = 1
  122.         end
  123.         if bgscroll % 18 == 0 then
  124.             bgscroll = 0
  125.         end
  126.         moveWalls()
  127.        
  128.         if keysDown[keys.up] and gm.y > 2 then
  129.             gm.y = gm.y - 1
  130.         end
  131.         if keysDown[keys.down] and gm.y < scr_y-3 then
  132.             gm.y = gm.y + 1
  133.         end
  134.         local isHit = not checkCollision()
  135.         if isHit then
  136.             return
  137.         end
  138.         gm.score = gm.score + 1
  139.         if gm.hiscore < gm.score then --conglaturations
  140.             gm.hiscore = gm.score
  141.         end
  142.         sleep(0)
  143.     end
  144. end
  145.  
  146. local getInput = function()
  147.     while true do
  148.         local evt, key = os.pullEvent()
  149.         if evt == "key" then
  150.             keysDown[key] = true
  151.         elseif evt == "key_up" then
  152.             keysDown[key] = false
  153.         end
  154.         if key == keys.q then
  155.             return
  156.         end
  157.     end
  158. end
  159.  
  160. local cleanExit = function()
  161.     term.setBackgroundColor(colors.black)
  162.     term.setTextColor(colors.white)
  163.     term.clear()
  164.     term.setCursorPos(1,1)
  165.     print("Thanks for playing!")
  166.     if t then t(true) end
  167.     sleep(0.05)
  168. end
  169.  
  170. local showTitle = function()
  171.     if gm.deaths == 0 then
  172.         local x = -38
  173.         local y = scr_y
  174.         repeat
  175.             y = y - 1
  176.             x = x + 2
  177.             if t then t(false) end
  178.             term.setBackgroundColor(colors.black)
  179.             term.clear()
  180.             paintutils.drawImage(sprite.guybig,math.floor(x),math.floor(y))
  181.             if t then t(true) end
  182.             sleep(0)
  183.         until y <= -24
  184.     end
  185.     term.setBackgroundColor(colors.white)
  186.     term.clear()
  187.     sleep(0)
  188.     term.setBackgroundColor(colors.black)
  189.     term.clear()
  190.     paintutils.drawImage(sprite.title,3,2)
  191.     sleep(0.1)
  192.     term.setCursorPos(4,scr_y)
  193.     term.setTextColor(colors.white)
  194.     term.setBackgroundColor(colors.black)
  195.     term.write("PUSH ANY KEY TO NEXT")
  196.     term.setCursorPos(2,1)
  197.     write("TOP: "..gm.hiscore.." | LAST: "..gm.score)
  198.     os.pullEvent("char")
  199. end
  200.  
  201. while true do
  202.     showTitle()
  203.     walls = {}
  204.     gm.y = math.floor(scr_y/2)
  205.     gm.score = 0
  206.     keysDown = {}
  207.     local res = parallel.waitForAny(getInput,game)
  208.     if res == 2 then
  209.         gm.deaths = gm.deaths + 1
  210.     else
  211.         cleanExit()
  212.         break
  213.     end
  214. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement