Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local oldPull = os.pullEvent
- os.pullEvent = os.pullEventRaw
- local args = {...}
- local tdifficulty = 2
- if args[1] then
- tdifficulty = args[1]
- end
- local wD,hT = term.getSize()
- hT = hT-2
- local difficulty = tonumber(tdifficulty) -- 1 = Easy, 2 = Mid, 3 = Hard
- if difficulty ~= 1 and difficulty ~= 2 and difficulty ~= 3 then
- difficulty = 2
- end
- local points = {}
- points.left = 0
- points.right = 0
- points.win = 10
- local gameBoard = {}
- local msg = ""
- local someoneWasDelayed = 0
- for i=1,wD do
- gameBoard[i] = {}
- for j=1,hT do
- gameBoard[i][j] = nil
- end
- end
- for i=1,hT do
- gameBoard[math.floor((wD/2)-5)][i] = {
- ["char"] = "@",
- ["charColor"] = colors.red,
- ["bgColor"] = colors.pink,
- }
- gameBoard[math.floor((wD/2)+5)][i] = {
- ["char"] = "@",
- ["charColor"] = colors.blue,
- ["bgColor"] = colors.lightBlue,
- }
- end
- local function drawGameBoard()
- for k,v in pairs(gameBoard) do
- for p,b in pairs(v) do
- if b ~= nil then
- if b["bgColor"] then paintutils.drawPixel(k,p+1,b["bgColor"]) else paintutils.drawPixel(k,p,colors.black) end
- term.setCursorPos(k,p+1)
- term.setTextColor(b["charColor"])
- term.write(b["char"])
- end
- end
- end
- end
- local function updateGameBoard()
- local updatedBoard = {}
- for k,v in pairs(gameBoard) do
- updatedBoard[k] = {}
- for p,b in pairs(v) do
- updatedBoard[k][p] = b
- end
- end
- for k,v in pairs(gameBoard) do
- --print(k)
- --print(v)
- for p,b in pairs(v) do
- if b ~= nil then
- if b.velocity then
- local newPos = k+b.velocity
- if newPos > wD or newPos <= 0 then
- if newPos > wD then points.left = points.left + 1 end
- if newPos <= 0 then points.right = points.right + 1 end
- updatedBoard[k][p] = nil
- else
- if updatedBoard[newPos][p] ~= nil then
- -- collision!
- local collided = updatedBoard[newPos][p]
- if collided.explosion == nil and updatedBoard[k][p].explosion ~= true and updatedBoard[newPos][p].onCollision == nil then
- -- create an explosion! :D
- for i=1,3 do
- for j=1,3 do
- local charChoice = math.random(1,3)
- local charChoice2 = ""
- if charChoice == 1 then
- charChoice2 = "@"
- elseif charChoice == 2 then
- charChoice2 = "#"
- elseif charChoice == 3 then
- charChoice2 = "*"
- end
- local colorChoice = math.random(1,3)
- local colorChoice2 = 0
- if colorChoice == 1 then
- colorChoice2 = colors.red
- elseif colorChoice == 2 then
- colorChoice2 = colors.orange
- elseif colorChoice == 3 then
- colorChoice2 = colors.yellow
- end
- if newPos+i-2 <= wD or p+j-2 > 0 then
- if not updatedBoard[newPos+i-2] then
- else
- updatedBoard[newPos+i-2][p+j-2] = {
- ["char"] = charChoice2,
- ["charColor"] = colors.white,
- ["bgColor"] = colorChoice2,
- ["explosion"] = 6+(math.random(-1,2)),
- }
- end
- end
- end
- end
- else
- -- aw, just delete me already :(
- if updatedBoard[newPos][p].onCollision then
- updatedBoard[newPos][p].onCollision(gameBoard[k][p])
- end
- updatedBoard[k][p] = nil
- updatedBoard[newPos][p] = nil
- end
- else
- -- empty..
- updatedBoard[newPos][p] = updatedBoard[k][p]
- local colorChoice = math.random(1,3)
- local colorChoice2 = nil
- if colorChoice == 1 then
- colorChoice2 = colors.orange
- elseif colorChoice == 2 then
- colorChoice2 = colors.gray
- elseif colorChoice == 3 then
- colorChoice2 = colors.red
- end
- updatedBoard[k][p] = {
- ["char"] = "@",
- ["charColor"] = colorChoice2,
- ["bgColor"] = nil,
- ["explosion"] = math.random(2,3)
- }
- end
- end
- end
- if b.explosion then
- b.explosion = b.explosion - 1
- if b.explosion <= 0 then
- updatedBoard[k][p] = nil
- end
- end
- end
- end
- end
- gameBoard = updatedBoard
- term.setBackgroundColor(colors.black)
- term.clear()
- drawGameBoard()
- paintutils.drawLine(1,hT+2,wD,hT+2,colors.gray)
- term.setCursorPos(1,hT+2)
- term.setTextColor(colors.white)
- term.write(msg)
- paintutils.drawLine(1,1,wD,1,colors.gray)
- term.setCursorPos(1,1)
- term.setTextColor(colors.blue)
- term.write(points.left)
- term.setCursorPos(wD,1)
- term.setTextColor(colors.red)
- term.write(points.right)
- if points.left >= points.win then
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.lightBlue)
- print("The player has won!")
- term.setTextColor(colors.white)
- os.pullEvent = oldPull
- error()
- elseif points.right >= points.win then
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.red)
- print("The AI has won!")
- term.setTextColor(colors.white)
- os.pullEvent = oldPull
- error()
- end
- end
- local playerWait = 0
- local function spawnMissile(y,v,plr)
- local x = 0
- local charChoice = "<"
- if v <= -1 then
- x = wD
- charChoice = "<"
- elseif v >= 1 then
- x = 1
- charChoice = ">"
- end
- gameBoard[x][y] = {
- ["char"] = charChoice,
- ["charColor"] = colors.white,
- ["velocity"] = v,
- }
- if plr then
- playerWait = 0.5
- end
- end
- local function scanForMissiles(enemy)
- local missiles = {}
- for k,v in pairs(gameBoard) do
- for p,b in pairs(v) do
- if enemy and b.char == ">" then
- missiles[#missiles+1] = {
- ["x"] = k,
- ["y"] = p,
- }
- elseif enemy == false and b.char == "<" then
- missiles[#missiles+1] = {
- ["x"] = k,
- ["y"] = p,
- }
- end
- end
- end
- return missiles
- end
- local function AIUpdate()
- local missiles = scanForMissiles(true)
- local counterMissiles = scanForMissiles(false)
- local enemyMissile = false
- for k,v in pairs(missiles) do
- local notice = math.random(1,5-difficulty)
- if notice == 1 then
- local found = false
- for p,b in pairs(counterMissiles) do
- if b.y == v.y then
- found = true
- end
- end
- if not found then
- enemyMissile = true
- os.queueEvent("fireMissile",v.y)
- end
- end
- end
- if not enemyMissile and difficulty >= 2 then
- local chance = math.random(1,30/difficulty)
- os.queueEvent("fireMissile",math.random(1,hT))
- end
- end
- local AIWait = 0
- local function powerUp()
- --sleep(math.random(8,20))
- local chance = math.random(1,200)
- if chance == 7 then
- msg = "A bonus box has spawned!"
- local X = math.random(math.floor(wD/2)-4,math.floor(wD/2)+4)
- local Y = math.random(math.floor(hT/2)-4,math.floor(hT/2)+4)
- gameBoard[X][Y] = {
- ["char"] = "$",
- ["charColor"] = colors.white,
- ["bgColor"] = colors.lime,
- ["onCollision"] = function(collided)
- if someoneWasDelayed == 0 then
- if collided.char == "<" then
- --msg = "You cannot fire missiles for 3 seconds!"
- playerWait = 3
- someoneWasDelayed = 1
- elseif collided.char == ">" then
- --msg = "The enemy cannot fire missiles for 3 seconds!"
- AIWait = 3
- someoneWasDelayed = 2
- end
- end
- end
- }
- end
- end
- --lastTime = 0
- parallel.waitForAny(
- function()
- while true do
- sleep(0.1)
- powerUp()
- AIWait = AIWait - 0.1
- playerWait = playerWait - 0.1
- msg = tostring(someoneWasDelayed)
- if playerWait > 0 and someoneWasDelayed == 1 then
- msg = "You cannot fire a missile for "..playerWait.." seconds!"
- elseif playerWait <= 0 and someoneWasDelayed == 1 then
- msg = ""
- someoneWasDelayed = 0
- end
- if AIWait > 0 and someoneWasDelayed == 2 then
- msg = "The AI cannot fire a missile for "..AIWait.." seconds!"
- elseif AIWait <= 0 and someoneWasDelayed == 2 then
- --msg = "2"
- someoneWasDelayed = 0
- end
- AIUpdate()
- updateGameBoard()
- end
- end,
- function()
- while true do
- local ev = {os.pullEvent()}
- if ev[1] == "mouse_click" then
- ev[4] = ev[4]-1
- if ev[4] > hT then
- ev[4] = hT
- end
- if ev[2] == 1 and playerWait <= 0 then
- spawnMissile(ev[4],1,true)
- end
- elseif ev[1] == "fireMissile" then
- if AIWait <= 0 then
- --print(os.time()-lastTime)
- --lastTime = os.time()
- if ev[2] > hT then
- ev[2] = hT
- end
- spawnMissile(ev[2],-1)
- if tdifficulty == 1 then
- AIWait = 0.8
- elseif tdifficulty == 2 then
- AIWait = 0.6
- elseif tdifficulty == 3 then
- AIWait = 0.5
- end
- sleep()
- end
- elseif ev[1] == "terminate" then
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- print("Aw, you quit the game early..")
- print("Oh well, thanks for playing!")
- os.pullEvent = oldPull
- error()
- end
- end
- end
- )
Add Comment
Please, Sign In to add comment