Guest User

laserblast

a guest
Aug 5th, 2013
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.06 KB | None | 0 0
  1. --  LaserBlast
  2. --       By NitrogenFingers
  3.  
  4. local w,h = term.getSize()
  5. local plpos = math.floor(w/2)
  6.  
  7. --music stuff
  8. local minterval = 1
  9. local mtimer = 1
  10. local left = false
  11.  
  12. local level = 1
  13. local score = 0
  14. local gameover=false
  15. local killc = 0
  16.  
  17. --x,y,dir
  18. local projlist = {}
  19. --x,y,intvspeed,dtimer
  20. local baddylist = {}
  21. local btimer = 0
  22. local bintv = 1
  23. local utime = 0.05
  24. local bsmp = 6
  25. local powerup = 0
  26.  
  27. --Landscape and stars
  28. local stars = {}
  29. for i=1,math.random()*10+10 do
  30.   stars[i] = { x = math.ceil(math.random()*w),
  31.     y = math.ceil(math.random() * h-8) }
  32. end
  33. local landscape = {
  34.   [6]="                       _____________              ";
  35.   [5]="          _______     /             \\_____        ";
  36.   [4]="         /       \\___/____                \\_________";
  37.   [3]="        /                 \\                      ";
  38.   [2]="       /                   \\_________               ";
  39.   [1]="______/                              \\______________";
  40. }
  41.  
  42. function drawHeader()
  43.   if term.isColour() then term.setTextColour(colours.white) end
  44.   term.setCursorPos(5, 1)
  45.   term.write("Score: "..score)
  46.   if score~=0 then term.write("00") end
  47.   local lstr = "Level: "..level
  48.   term.setCursorPos(w-#lstr-5,1)
  49.   term.write(lstr)
  50.  
  51.   if powerup > 0 then
  52.     local pstr = "POWERUP"
  53.     term.setCursorPos(w/2 - #pstr/2,1)
  54.     if term.isColour() then term.setTextColour(colours.cyan) end
  55.     term.write(pstr)
  56.   end
  57. end
  58.  
  59. function drawPlayer()
  60.   if term.isColour() then term.setTextColour(colours.white) end
  61.   term.setCursorPos(plpos-1, h-1)
  62.   term.write("@@@")
  63. end
  64.  
  65. function drawProjectile(proj)
  66.   if term.isColour() then term.setTextColour(colours.red) end
  67.   term.setCursorPos(proj.x, proj.y)
  68.   term.write("|")
  69. end
  70.  
  71. function drawVacance(x,y)
  72.   term.setCursorPos(x, y)
  73.   for _,baddy in pairs(baddylist) do
  74.     if baddy.x == x and baddy.y == y and baddy.dtimer > 0 then
  75.       drawBaddie(baddy)
  76.       return
  77.     end
  78.   end
  79.  
  80.   if y >= h-8 and y <= h-3 then
  81.     if term.isColour() then term.setTextColour(colours.lime) end
  82.     term.write(string.sub(landscape[h - y - 2], x, x))
  83.   elseif y < h-8 then
  84.     for i=1,#stars do
  85.       if x == stars[i].x and y == stars[i].y then
  86.         if term.isColour() then term.setTextColour(colours.yellow) end
  87.         term.write(".")
  88.         return
  89.       end
  90.     end
  91.     term.write(" ")
  92.   else
  93.     term.write(" ")
  94.   end
  95. end
  96.  
  97. function drawBaddie(baddy)
  98.   term.setCursorPos(baddy.x, baddy.y)
  99.     if baddy.dtimer==0 then
  100.       if baddy.pup then
  101.         if term.isColour() then term.setTextColour(colours.blue) end
  102.         term.write("P")
  103.       elseif baddy.frag then
  104.         if term.isColour() then term.setTextColour(colours.brown) end
  105.         term.write("*")
  106.       else
  107.         if term.isColour() then term.setTextColour(colours.brown) end
  108.         term.write("O")
  109.       end
  110.     else
  111.       if term.isColour() then term.setTextColour(colours.orange) end
  112.       term.write("#")
  113.     end
  114. end
  115.  
  116. --This now only needs to be called once. Awesome!
  117. function drawWorld()
  118.   drawLandscape()
  119.   drawPlayer()
  120.   drawHeader()
  121. end
  122.  
  123. function drawLandscape()
  124.   if term.isColour() then
  125.     term.setTextColour(colours.yellow)
  126.   end
  127.   for i=1,#stars do
  128.     term.setCursorPos(stars[i].x, stars[i].y)
  129.     term.write(".")
  130.   end
  131.  
  132.   term.setCursorPos(1,h)
  133.   local land = string.rep("-", w)
  134.   if term.isColour() then
  135.     term.setTextColour(colours.green)
  136.   end
  137.   term.write(land)
  138.   if term.isColour() then
  139.     term.setTextColour(colours.lime)
  140.   end
  141.  
  142.   for y,line in ipairs(landscape) do
  143.     term.setCursorPos(1,h-y-2)
  144.     term.write(line)
  145.   end
  146. end
  147.  
  148. function updateWorld()
  149.   --The music stuff  
  150.   redstone.setOutput("back", false)
  151.   mtimer=mtimer-utime
  152.   if mtimer<=0 then
  153.     mtimer = minterval
  154.     if left then
  155.       redstone.setOutput("left", true)
  156.       redstone.setOutput("right", false)
  157.     else
  158.       redstone.setOutput("left", false)
  159.       redstone.setOutput("right", true)
  160.     end
  161.     left = not left
  162.   end
  163.  
  164.   local i=1
  165.   while i<=#projlist do
  166.     drawVacance(projlist[i].x, projlist[i].y)
  167.     projlist[i].y = projlist[i].y+projlist[i].dir
  168.     if projlist[i].y <= 1 or projlist[i].y > h-1 then
  169.       table.remove(projlist,i)
  170.       i=i-1
  171.     else drawProjectile(projlist[i]) end
  172.     i=i+1
  173.   end
  174.   i=1
  175.   while i<=#baddylist do
  176.     local baddy = baddylist[i]
  177.     baddy.timer=baddy.timer+utime
  178.  
  179.     if baddy.y==h-1 and math.abs(baddy.x-plpos)<2 then
  180.       if baddy.pup then
  181.         powerup = 10
  182.         drawPlayer()
  183.       else
  184.         gameover = true
  185.         redstone.setOutput("back", true)
  186.       end
  187.     end
  188.  
  189.     j=1
  190.     while j<=#projlist do
  191.       local proj = projlist[j]
  192.       if baddy.x==proj.x and math.abs(baddy.y-proj.y)<2
  193.       and baddy.dtimer==0 then
  194.         baddy.dtimer = 0.5
  195.         drawBaddie(baddy)
  196.         drawVacance(projlist[j].x, projlist[j].y)
  197.         table.remove(projlist,j)
  198.         j=j-1
  199.         score=score+5
  200.         redstone.setOutput("back", true)
  201.         killc=killc+1
  202.         if killc>5+(level*5) and level<10 then levelUp() end
  203.         drawHeader()
  204.  
  205.         --Adds fragments
  206.         if math.random(1, 5) == 2 and not baddy.frag then
  207.           table.insert(baddylist, {
  208.             x = baddy.x-1,
  209.             y = baddy.y,
  210.             pup = false,
  211.             frag = true,
  212.             timer = 0,
  213.             dtimer = 0,
  214.             speed = baddy.speed/2
  215.           })
  216.           drawBaddie(baddylist[#baddylist])
  217.           table.insert(baddylist, {
  218.             x = baddy.x+1,
  219.             y = baddy.y,
  220.             pup = false,
  221.             frag = true,
  222.             timer = 0,
  223.             dtimer = 0,
  224.             speed = baddy.speed/2
  225.           })
  226.           drawBaddie(baddylist[#baddylist])
  227.         end
  228.       end
  229.       j=j+1
  230.     end
  231.  
  232.     if baddy.timer>baddy.speed and baddy.dtimer==0 then
  233.       drawVacance(baddy.x, baddy.y)
  234.       baddy.y=baddy.y+1
  235.       if baddy.y==h then
  236.         table.remove(baddylist,i)
  237.         i=i-1
  238.         score=score-1
  239.         drawHeader()
  240.       else
  241.         drawBaddie(baddy)
  242.         baddy.timer = 0
  243.       end
  244.     elseif baddy.dtimer>0 then
  245.       baddy.dtimer=baddy.dtimer-utime
  246.       if baddy.dtimer<=0 then
  247.         drawVacance(baddy.x, baddy.y)
  248.         table.remove(baddylist,i)
  249.         i=i-1
  250.       end
  251.     end    
  252.     i=i+1
  253.   end  
  254.   btimer=btimer+utime
  255.   if btimer > bintv then
  256.     table.insert(baddylist, {
  257.       x = math.random(w/4, 3*(w/4)),
  258.       y = 2,
  259.       speed = utime*bsmp,
  260.       timer = 0,
  261.       dtimer = 0,
  262.       pup = math.random(1,20)==5,
  263.       frag = false
  264.     })
  265.     drawBaddie(baddylist[#baddylist])
  266.     btimer=0
  267.   end
  268. end
  269.  
  270. function levelUp()
  271.   level=level+1
  272.   bintv=bintv-0.10
  273.   bsmp=bsmp-0.5
  274.   killc=0
  275.   minterval=minterval-0.10
  276. end
  277.  
  278. function updatePlayer(key)
  279.   if powerup>0 then
  280.     powerup = powerup-utime
  281.   end
  282.  
  283.   if key==203 and plpos>1 then
  284.     term.setCursorPos(plpos+1,h-1)
  285.     term.write(" ")
  286.     plpos=plpos-1
  287.     drawPlayer()
  288.   elseif key==205 and plpos<w then
  289.     term.setCursorPos(plpos-1,h-1)
  290.     term.write(" ")
  291.     plpos=plpos+1
  292.     drawPlayer()
  293.   elseif key==57 then
  294.     if powerup>0 then
  295.       table.insert(projlist, {
  296.         dir = -1,
  297.         x = plpos+1,
  298.         y = h-2
  299.       })
  300.       drawProjectile(projlist[#projlist])
  301.       table.insert(projlist, {
  302.         dir = -1,
  303.         x = plpos-1,
  304.         y = h-2
  305.       })
  306.       drawProjectile(projlist[#projlist])
  307.     else
  308.       table.insert(projlist, {
  309.         dir = -1,
  310.         x = plpos,
  311.         y = h-2
  312.       })
  313.       drawProjectile(projlist[#projlist])
  314.     end
  315.   end
  316. end
  317.  
  318. term.setBackgroundColour(colours.black)
  319. term.clear()
  320. drawWorld()
  321. local wtimer os.startTimer(utime)
  322. while not gameover do
  323.   local e, v = os.pullEvent()
  324.  
  325.   if e=="timer" then
  326.     updateWorld()
  327.     wtimer = os.startTimer(utime)
  328.   elseif e=="key" then
  329.     if v==28 then break end
  330.     updatePlayer(v)
  331.   end
  332. end
  333.  
  334. term.setCursorPos(plpos-1, h-1)
  335. if term.isColour() then term.setTextColour(colours.red) end
  336. term.write("###")
  337. local go = "Game Over!"
  338. term.setCursorPos(w/2 - #go/2, 10)
  339. if term.isColour() then term.setTextColour(colours.white) end
  340. term.write(go)
  341. term.setCursorPos(1,h)
  342. sleep(5)
  343. redstone.setOutput("back", false)
  344. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment