Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local gpu = component.gpu
- local computer = require("computer")
- local oldPull = event.pull
- event.pull = function(...) return oldPull(...) end
- local args = {...}
- local tdifficulty = 2
- if args[1] then
- tdifficulty = args[1]
- end
- local wD, hT = gpu.getResolution()
- 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 = {left = 0, right = 0, win = 10}
- local gameBoard = {}
- local msg = ""
- local someoneWasDelayed = 0
- -- Define colors for OpenComputers
- local colors = {
- red = 0xFF0000, blue = 0x0000FF, white = 0xFFFFFF, black = 0x000000,
- pink = 0xFFC0CB, lightBlue = 0xADD8E6, orange = 0xFFA500, gray = 0x808080,
- yellow = 0xFFFF00, lime = 0x00FF00
- }
- 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
- gpu.setBackground(b["bgColor"])
- gpu.fill(k, p+1, 1, 1, " ")
- else
- gpu.setBackground(colors.black)
- gpu.fill(k, p, 1, 1, " ")
- end
- gpu.setForeground(b["charColor"])
- gpu.set(k, p+1, 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
- 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
- local collided = updatedBoard[newPos][p]
- if collided.explosion == nil and updatedBoard[k][p].explosion ~= true and updatedBoard[newPos][p].onCollision == nil then
- for i = 1, 3 do
- for j = 1, 3 do
- local charChoices = {"@", "#", "*"}
- local colorChoices = {colors.red, colors.orange, colors.yellow}
- local charChoice = charChoices[math.random(1, 3)]
- local colorChoice = colorChoices[math.random(1, 3)]
- if newPos+i-2 <= wD and p+j-2 > 0 then
- if updatedBoard[newPos+i-2] then
- updatedBoard[newPos+i-2][p+j-2] = {
- ["char"] = charChoice,
- ["charColor"] = colors.white,
- ["bgColor"] = colorChoice,
- ["explosion"] = 6 + (math.random(-1, 2))
- }
- end
- end
- end
- end
- else
- if updatedBoard[newPos][p].onCollision then
- updatedBoard[newPos][p].onCollision(gameBoard[k][p])
- end
- updatedBoard[k][p] = nil
- updatedBoard[newPos][p] = nil
- end
- else
- updatedBoard[newPos][p] = updatedBoard[k][p]
- local colorChoices = {colors.orange, colors.gray, colors.red}
- updatedBoard[k][p] = {
- ["char"] = "@",
- ["charColor"] = colorChoices[math.random(1, 3)],
- ["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
- gpu.setBackground(colors.black)
- gpu.fill(1, 1, wD, hT+2, " ")
- drawGameBoard()
- gpu.setBackground(colors.gray)
- gpu.fill(1, hT+2, wD, 1, " ")
- gpu.fill(1, 1, wD, 1, " ")
- gpu.setForeground(colors.white)
- gpu.set(1, hT+2, msg)
- gpu.setForeground(colors.blue)
- gpu.set(1, 1, tostring(points.left))
- gpu.setForeground(colors.red)
- gpu.set(wD - #tostring(points.right), 1, tostring(points.right))
- if points.left >= points.win then
- gpu.setBackground(colors.black)
- gpu.fill(1, 1, wD, hT+2, " ")
- gpu.setForeground(colors.lightBlue)
- gpu.set(1, 1, "The player has won!")
- os.exit()
- elseif points.right >= points.win then
- gpu.setBackground(colors.black)
- gpu.fill(1, 1, wD, hT+2, " ")
- gpu.setForeground(colors.red)
- gpu.set(1, 1, "The AI has won!")
- os.exit()
- 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 AIWait = 0
- 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
- computer.pushSignal("fireMissile", v.y)
- end
- end
- end
- if not enemyMissile and difficulty >= 2 then
- local chance = math.random(1, 30/difficulty)
- if chance == 1 then
- computer.pushSignal("fireMissile", math.random(1, hT))
- end
- end
- end
- local function powerUp()
- 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
- playerWait = 3
- someoneWasDelayed = 1
- elseif collided.char == ">" then
- AIWait = 3
- someoneWasDelayed = 2
- end
- end
- end
- }
- end
- end
- local function main()
- while true do
- local startTime = computer.uptime()
- 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
- someoneWasDelayed = 0
- end
- AIUpdate()
- updateGameBoard()
- local e = {event.pull(0)}
- if e[1] == "touch" then
- local _, _, x, y = table.unpack(e)
- y = y - 1
- if y > hT then y = hT end
- if x == 1 and playerWait <= 0 then
- spawnMissile(y, 1, true)
- end
- elseif e[1] == "fireMissile" then
- if AIWait <= 0 then
- local y = e[2]
- if y > hT then y = hT end
- spawnMissile(y, -1)
- AIWait = ({0.8, 0.6, 0.5})[tdifficulty] or 0.6
- end
- elseif e[1] == "interrupted" then
- gpu.setBackground(colors.black)
- gpu.fill(1, 1, wD, hT+2, " ")
- gpu.setForeground(colors.white)
- gpu.set(1, 1, "Aw, you quit the game early..")
- gpu.set(1, 2, "Oh well, thanks for playing!")
- break
- end
- local endTime = computer.uptime()
- local sleepTime = 0.1 - (endTime - startTime)
- if sleepTime > 0 then os.sleep(sleepTime) end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement