Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Assignment one by ikke009. Teacher: nitrogenfingers
- --Date of completion: 18-1-2013, Game Name: Basedefense
- --Inspiration and knowledge required to make this game was taken from a game called laserblast
- w,h = term.getSize()
- lives = 5
- kills = 0
- refreshRate = 0.18
- function load() --Runs once, does the intro
- term.clear()
- term.setCursorPos(10,3)
- term.setTextColour(colours.yellow)
- write("Computercraft University, assignment one")
- term.setCursorPos(10,4)
- write("By: ikke009 (inspired by laserblast)")
- term.setCursorPos(10,5)
- write("Instructions: Click baddies!")
- term.setCursorPos(10,7)
- write("Click here to continue")
- while true do
- local ev,but,x,y = os.pullEvent("mouse_click")
- if but == 1 and x >5 and x < 40 and y < 8 and y > 5 then
- term.clear()
- term.setTextColour(colours.white)
- break
- end
- end
- end
- function drawBase() --Draws the green base at the right side of the screen
- term.setBackgroundColour(colours.green)
- for i=3,h do
- term.setCursorPos(w-2,i)
- write(" ")
- end
- end
- function drawHeader() --Draws the header with the red healthbar
- term.setTextColour(colours.blue)
- for i=1,w do
- term.setCursorPos(i,2)
- write("-")
- end
- term.setTextColour(colours.white)
- term.setBackgroundColour(colours.red)
- local redbar = math.floor((w/5)*lives)
- for i=1,redbar do
- term.setCursorPos(i,1)
- write(" ")
- end
- term.setBackgroundColour(colours.black)
- end
- enemylist = {}
- enemyxvel = 1.5
- explosionlist = {}
- function updateGame()
- for i=1,#enemylist do
- local ent = enemylist[i]
- if ent then
- if ent.x < w-3 then
- ent.x = ent.x + enemyxvel
- else table.remove(enemylist,i) --If enemy hits base
- lives = lives - 1
- table.insert(explosionlist,{
- x=ent.x,
- y=ent.y,
- phase=1
- })
- end
- end
- end
- 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
- table.insert(enemylist,{
- x = 1,
- y = math.random(4,h-1)
- })
- end
- if #explosionlist > 0 then --If there are explosions then
- for i=1,#explosionlist do
- local expl = explosionlist[i]
- if expl then --If explosionlist[i] is not nill
- if expl.phase <= 3 then
- expl.phase = expl.phase + 1 --there are 3 explosion phases that make it "animated"
- else table.remove(explosionlist,i) --if "animation" is done, remove explosion
- end
- end
- end
- end
- if kills == 10 then --Speeds up the game >:D
- enemyxvel = 1.75
- elseif kills == 20 then
- enemyxvel = 2
- end
- end
- function drawGame()
- term.clear()
- drawHeader()
- drawBase()
- term.setBackgroundColour(colours.yellow) --Explosion drawing starts here
- for i=1,#explosionlist do
- local expl = explosionlist[i]
- if expl.x then --if explosionlist[i] is not nil
- if expl.phase == 1 then --various drawings for every phase
- term.setCursorPos(expl.x,expl.y)
- write(" ")
- elseif expl.phase == 2 then
- term.setCursorPos(expl.x-1,expl.y-1)
- write(" ")
- term.setCursorPos(expl.x+1,expl.y-1)
- write(" ")
- term.setCursorPos(expl.x-1,expl.y+1)
- write(" ")
- term.setCursorPos(expl.x+1,expl.y+1)
- write(" ")
- term.setCursorPos(expl.x,expl.y)
- write(" ")
- elseif expl.phase == 3 then
- term.setCursorPos(expl.x-1,expl.y-1)
- write(" ")
- term.setCursorPos(expl.x+1,expl.y-1)
- write(" ")
- term.setCursorPos(expl.x-1,expl.y+1)
- write(" ")
- term.setCursorPos(expl.x+1,expl.y+1)
- write(" ")
- end
- end
- end
- term.setBackgroundColour(colours.black)
- term.setTextColour(colours.red) --Enemy drawing starts here
- for i=1,#enemylist do
- local ent = enemylist[i]
- if ent.x then
- term.setCursorPos(ent.x,ent.y)
- write("=>")
- end
- end
- end
- function maingame()
- load()
- gtID = os.startTimer(refreshRate)
- while lives > 0 do
- ev,id,mx,my = os.pullEvent() --event, mousebutton/timerid, mousex/nil, mousey/nil
- if ev == "mouse_click" then
- for i=1,#enemylist do
- if enemylist[i] then
- local ent = enemylist[i]
- 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)
- table.remove(enemylist,i)
- kills = kills + 1
- table.insert(explosionlist,{
- x = mx,
- y = my,
- phase = 1
- })
- end
- end
- end
- elseif ev == "timer" and id == gtID then
- updateGame()
- drawGame()
- gtID = os.startTimer(refreshRate)
- end
- end
- term.setTextColour(colours.yellow) --Game Over screen, game outro
- term.setBackgroundColour(colours.black)
- term.clear()
- term.setCursorPos(10,4)
- textutils.slowPrint("GAME OVER",4)
- term.setCursorPos(10,6)
- write("Kills: "..kills)
- term.setCursorPos(1,8)
- end
- maingame()
Advertisement
Add Comment
Please, Sign In to add comment