Don't like ads? PRO users don't see any ads ;-)
Guest

laserblast

By: a guest on Jun 27th, 2012  |  syntax: Lua  |  size: 5.48 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. w,h = term.getSize()
  2. plpos = w/2
  3.  
  4. --music stuff
  5. minterval = 1
  6. mtimer = 1
  7. left = false
  8.  
  9. level = 1
  10. score = 0
  11. gameover=false
  12. killc = 0
  13.  
  14. --x,y,dir
  15. projlist = {}
  16. --x,y,intvspeed,dtimer
  17. baddylist = {}
  18. btimer = 0
  19. bintv = 1
  20. utime = 0.05
  21. bsmp = 6
  22. powerup = 0
  23.  
  24. function drawHeader()
  25.   term.setCursorPos(5, 1)
  26.   term.write("Score: "..score)
  27.   if score~=0 then term.write("00") end
  28.   local lstr = "Level: "..level
  29.   term.setCursorPos(w-#lstr-5,1)
  30.   term.write(lstr)
  31. end
  32.  
  33. function drawWorld()
  34.   term.clear()
  35.   drawHeader()
  36.   drawLandscape()
  37.   term.setCursorPos(plpos-1, h-1)
  38.   term.write("@@@")
  39.   for i=1,#projlist do
  40.     local proj = projlist[i]
  41.     term.setCursorPos(proj.x, proj.y)
  42.     term.write("|")
  43.   end
  44.   for i=1,#baddylist do
  45.     local baddy = baddylist[i]
  46.     term.setCursorPos(baddy.x, baddy.y)
  47.     if baddy.dtimer==0 then
  48.       if baddy.pup then term.write("P")
  49.       elseif baddy.frag then term.write("*")
  50.       else term.write("O") end
  51.     else
  52.       term.write("#")
  53.     end
  54.   end
  55. end
  56.  
  57. function drawLandscape()
  58.   term.setCursorPos(1,h)
  59.   local land = string.rep("-", w)
  60.   term.write(land)
  61.   local m6="                       _____________              "
  62.   local m5="          _______     /             \\____         "
  63.   local m4="         /       \\___/___                \\________"
  64.   local m3="        /                \\                        "
  65.   local m2="       /                  \\________               "
  66.   local m1="______/                            \\______________"
  67.   term.setCursorPos(1, h-8)
  68.   term.write(m6)
  69.   term.setCursorPos(1, h-7)
  70.   term.write(m5)
  71.   term.setCursorPos(1, h-6)
  72.   term.write(m4)
  73.   term.setCursorPos(1, h-5)
  74.   term.write(m3)
  75.   term.setCursorPos(1, h-4)
  76.   term.write(m2)
  77.   term.setCursorPos(1, h-3)
  78.   term.write(m1)
  79. end
  80.  
  81. function updateWorld()
  82.   --The music stuff  
  83.   redstone.setOutput("back", false)
  84.   mtimer=mtimer-utime
  85.   if mtimer<=0 then
  86.     mtimer = minterval
  87.     if left then
  88.       redstone.setOutput("left", true)
  89.       redstone.setOutput("right", false)
  90.     else
  91.       redstone.setOutput("left", false)
  92.       redstone.setOutput("right", true)
  93.     end
  94.     left = not left
  95.   end
  96.  
  97.   local i=1
  98.   while i<=#projlist do
  99.     projlist[i].y = projlist[i].y+projlist[i].dir
  100.     if projlist[i].y < 0 or projlist[i].y > h-1 then
  101.       table.remove(projlist,i)
  102.       i=i-1
  103.     end
  104.     i=i+1
  105.   end
  106.   i=1
  107.   while i<=#baddylist do
  108.     local baddy = baddylist[i]
  109.     baddy.timer=baddy.timer+utime
  110.  
  111.     if baddy.y==h-1 and math.abs(baddy.x-plpos)<2 then
  112.       if baddy.pup then powerup = 10
  113.       else
  114.         gameover = true
  115.         redstone.setOutput("back", true)
  116.       end
  117.     end
  118.  
  119.     j=1
  120.     while j<=#projlist do
  121.       local proj = projlist[j]
  122.       if baddy.x==proj.x and math.abs(baddy.y-proj.y)<2
  123.       and baddy.dtimer==0 then
  124.         baddy.dtimer = 0.5
  125.         table.remove(projlist,j)
  126.         j=j-1
  127.         score=score+5
  128.         redstone.setOutput("back", true)
  129.         killc=killc+1
  130.         if killc>5+(level*5) and level<10 then levelUp() end
  131.    
  132.         --Adds fragments
  133.         if math.random(1, 5) == 2 and not baddy.frag then
  134.           table.insert(baddylist, {
  135.             x = baddy.x-1,
  136.             y = baddy.y,
  137.             pup = false,
  138.             frag = true,
  139.             timer = 0,
  140.             dtimer = 0,
  141.             speed = baddy.speed/2
  142.           })
  143.           table.insert(baddylist, {
  144.             x = baddy.x+1,
  145.             y = baddy.y,
  146.             pup = false,
  147.             frag = true,
  148.             timer = 0,
  149.             dtimer = 0,
  150.             speed = baddy.speed/2
  151.           })
  152.         end
  153.       end
  154.       j=j+1
  155.     end
  156.    
  157.     if baddy.timer>baddy.speed and baddy.dtimer==0 then
  158.       baddy.y=baddy.y+1
  159.       baddy.timer = 0
  160.       if baddy.y==h then
  161.         table.remove(baddylist,i)
  162.         i=i-1
  163.         score=score-1
  164.       end
  165.     elseif baddy.dtimer>0 then
  166.       baddy.dtimer=baddy.dtimer-utime
  167.       if baddy.dtimer<=0 then
  168.         table.remove(baddylist,i)
  169.         i=i-1
  170.       end
  171.     end    
  172.     i=i+1
  173.   end  
  174.   btimer=btimer+utime
  175.   if btimer > bintv then
  176.     table.insert(baddylist, {
  177.       x = math.random(w/4, 3*(w/4)),
  178.       y = 1,
  179.       speed = utime*bsmp,
  180.       timer = 0,
  181.       dtimer = 0,
  182.       pup = math.random(1,20)==5,
  183.       frag = false
  184.     })
  185.     btimer=0
  186.   end
  187. end
  188.  
  189. function levelUp()
  190.   level=level+1
  191.   bintv=bintv-0.10
  192.   bsmp=bsmp-0.5
  193.   killc=0
  194.   minterval=minterval-0.10
  195. end
  196.  
  197. function updatePlayer(key)
  198.   if powerup>0 then
  199.     powerup = powerup-utime
  200.   end
  201.  
  202.   if key==203 and plpos>1 then
  203.     plpos=plpos-1
  204.   elseif key==205 and plpos<w then
  205.     plpos=plpos+1
  206.   elseif key==57 then
  207.     if powerup>0 then
  208.       table.insert(projlist, {
  209.         dir = -1,
  210.         x = plpos+1,
  211.         y = h-2
  212.       })
  213.       table.insert(projlist, {
  214.         dir = -1,
  215.         x = plpos-1,
  216.         y = h-2
  217.       })
  218.     else
  219.       table.insert(projlist, {
  220.         dir = -1,
  221.         x = plpos,
  222.         y = h-2
  223.       })
  224.     end
  225.   end
  226. end
  227.  
  228. local wtimer os.startTimer(utime)
  229. while not gameover do
  230.   local e, v = os.pullEvent()
  231.  
  232.   if e=="timer" then
  233.     updateWorld()
  234.     wtimer = os.startTimer(utime)
  235.   elseif e=="key" then
  236.     if v==28 then break end
  237.     updatePlayer(v)
  238.   end
  239.   drawWorld()
  240. end
  241.  
  242. term.setCursorPos(plpos-1, h-1)
  243. term.write("###")
  244. local go = "Game Over!"
  245. term.setCursorPos(w/2 - #go/2, 10)
  246. term.write(go)
  247. term.setCursorPos(1,h)
  248. sleep(5)
  249. redstone.setOutput("back", false)
  250. term.clear()