Guest User

basedef

a guest
Feb 6th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.76 KB | None | 0 0
  1. --Assignment one by ikke009. Teacher: nitrogenfingers
  2. --Date of completion: 18-1-2013, Game Name: Basedefense
  3. --Inspiration and knowledge required to make this game was taken from a game called laserblast
  4.  
  5. w,h = term.getSize()
  6. lives = 5
  7. kills = 0
  8. refreshRate = 0.18
  9.    
  10. function load() --Runs once, does the intro
  11.     term.clear()
  12.     term.setCursorPos(10,3)
  13.     term.setTextColour(colours.yellow)
  14.     write("Computercraft University, assignment one")
  15.     term.setCursorPos(10,4)
  16.     write("By: ikke009 (inspired by laserblast)")
  17.     term.setCursorPos(10,5)
  18.     write("Instructions: Click baddies!")
  19.     term.setCursorPos(10,7)
  20.     write("Click here to continue")
  21.     while true do
  22.         local ev,but,x,y = os.pullEvent("mouse_click")
  23.         if but == 1 and x >5 and x < 40 and y < 8 and y > 5 then
  24.             term.clear()
  25.             term.setTextColour(colours.white)
  26.             break
  27.         end
  28.     end
  29. end
  30.  
  31. function drawBase() --Draws the green base at the right side of the screen
  32.     term.setBackgroundColour(colours.green)
  33.     for i=3,h do
  34.         term.setCursorPos(w-2,i)
  35.         write("   ")
  36.     end
  37. end
  38.  
  39. function drawHeader() --Draws the header with the red healthbar
  40.     term.setTextColour(colours.blue)
  41.     for i=1,w do
  42.         term.setCursorPos(i,2)
  43.         write("-")
  44.     end
  45.  
  46.     term.setTextColour(colours.white)
  47.     term.setBackgroundColour(colours.red)
  48.     local redbar = math.floor((w/5)*lives)
  49.     for i=1,redbar do
  50.         term.setCursorPos(i,1)
  51.         write(" ")
  52.     end
  53.     term.setBackgroundColour(colours.black)  
  54. end
  55.  
  56. enemylist = {}
  57. enemyxvel = 1.5
  58. explosionlist = {}
  59. function updateGame()
  60.     for i=1,#enemylist do
  61.         local ent = enemylist[i]
  62.         if ent then
  63.             if ent.x < w-3 then
  64.                 ent.x = ent.x + enemyxvel
  65.             else table.remove(enemylist,i) --If enemy hits base
  66.                 lives = lives - 1
  67.                 table.insert(explosionlist,{
  68.                     x=ent.x,
  69.                     y=ent.y,
  70.                     phase=1
  71.                 })
  72.             end
  73.         end
  74.     end
  75.  
  76.     if #enemylist < math.ceil(kills/4) or (#enemylist == 0 and kills == 0) then --for every 4 kills an additional enemy gets spawned, or if there are no enemys and no kills
  77.         table.insert(enemylist,{
  78.             x = 1,
  79.             y = math.random(4,h-1)
  80.         })
  81.     end
  82.  
  83.     if #explosionlist > 0 then --If there are explosions then
  84.         for i=1,#explosionlist do
  85.             local expl = explosionlist[i]
  86.             if expl then --If explosionlist[i] is not nill
  87.                 if expl.phase <= 3 then
  88.                     expl.phase = expl.phase + 1 --there are 3 explosion phases that make it "animated"
  89.                 else table.remove(explosionlist,i) --if "animation" is done, remove explosion
  90.                 end
  91.             end
  92.         end
  93.     end
  94.  
  95.     if kills == 10 then --Speeds up the game >:D
  96.         enemyxvel = 1.75
  97.     elseif kills == 20 then
  98.         enemyxvel = 2
  99.     end
  100. end
  101.  
  102. function drawGame()
  103.     term.clear()
  104.     drawHeader()
  105.     drawBase()
  106.     term.setBackgroundColour(colours.yellow) --Explosion drawing starts here
  107.     for i=1,#explosionlist do
  108.         local expl = explosionlist[i]
  109.         if expl.x then --if explosionlist[i] is not nil
  110.             if expl.phase == 1 then --various drawings for every phase
  111.                 term.setCursorPos(expl.x,expl.y)
  112.                 write(" ")
  113.             elseif expl.phase == 2 then
  114.                 term.setCursorPos(expl.x-1,expl.y-1)
  115.                 write(" ")
  116.                 term.setCursorPos(expl.x+1,expl.y-1)
  117.                 write(" ")
  118.                 term.setCursorPos(expl.x-1,expl.y+1)
  119.                 write(" ")
  120.                 term.setCursorPos(expl.x+1,expl.y+1)
  121.                 write(" ")
  122.                 term.setCursorPos(expl.x,expl.y)
  123.                 write(" ")
  124.             elseif expl.phase == 3 then
  125.                 term.setCursorPos(expl.x-1,expl.y-1)
  126.                 write(" ")
  127.                 term.setCursorPos(expl.x+1,expl.y-1)
  128.                 write(" ")
  129.                 term.setCursorPos(expl.x-1,expl.y+1)
  130.                 write(" ")
  131.                 term.setCursorPos(expl.x+1,expl.y+1)
  132.                 write(" ")
  133.             end
  134.         end
  135.     end
  136.     term.setBackgroundColour(colours.black)
  137.     term.setTextColour(colours.red) --Enemy drawing starts here
  138.     for i=1,#enemylist do
  139.         local ent = enemylist[i]
  140.         if ent.x then
  141.             term.setCursorPos(ent.x,ent.y)
  142.             write("=>")
  143.         end
  144.     end
  145. end
  146.      
  147. function maingame()
  148.     load()
  149.     gtID = os.startTimer(refreshRate)
  150.     while lives > 0 do
  151.         ev,id,mx,my = os.pullEvent() --event, mousebutton/timerid, mousex/nil, mousey/nil
  152.         if ev == "mouse_click" then
  153.             for i=1,#enemylist do
  154.                 if enemylist[i] then
  155.                     local ent = enemylist[i]
  156.                     if (ent.x == mx+2 or ent.x == mx+3) and ent.y == my then --if the click was on an enemy (mx+2 and mx+3 accounts for lag on the CC uni server)
  157.                         table.remove(enemylist,i)
  158.                         kills = kills + 1
  159.                         table.insert(explosionlist,{
  160.                             x = mx,
  161.                             y = my,
  162.                             phase = 1
  163.                         })
  164.                     end
  165.                 end
  166.             end
  167.         elseif ev == "timer" and id == gtID then
  168.             updateGame()
  169.             drawGame()
  170.             gtID = os.startTimer(refreshRate)
  171.         end
  172.     end
  173.     term.setTextColour(colours.yellow) --Game Over screen, game outro
  174.     term.setBackgroundColour(colours.black)
  175.     term.clear()
  176.     term.setCursorPos(10,4)
  177.     textutils.slowPrint("GAME OVER",4)
  178.     term.setCursorPos(10,6)
  179.     write("Kills:   "..kills)
  180.     term.setCursorPos(1,8)
  181. end
  182.      
  183. maingame()
Advertisement
Add Comment
Please, Sign In to add comment