Advertisement
nonogamer9

MissileCanyon For OpenOS (credit to SALDOR010)

May 5th, 2024 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.71 KB | Gaming | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local gpu = component.gpu
  4. local computer = require("computer")
  5.  
  6. local oldPull = event.pull
  7. event.pull = function(...) return oldPull(...) end
  8.  
  9. local args = {...}
  10. local tdifficulty = 2
  11.  
  12. if args[1] then
  13.     tdifficulty = args[1]
  14. end
  15.  
  16. local wD, hT = gpu.getResolution()
  17. hT = hT - 2
  18.  
  19. local difficulty = tonumber(tdifficulty) -- 1 = Easy, 2 = Mid, 3 = Hard
  20. if difficulty ~= 1 and difficulty ~= 2 and difficulty ~= 3 then
  21.     difficulty = 2
  22. end
  23.  
  24. local points = {left = 0, right = 0, win = 10}
  25. local gameBoard = {}
  26. local msg = ""
  27. local someoneWasDelayed = 0
  28.  
  29. -- Define colors for OpenComputers
  30. local colors = {
  31.     red = 0xFF0000, blue = 0x0000FF, white = 0xFFFFFF, black = 0x000000,
  32.     pink = 0xFFC0CB, lightBlue = 0xADD8E6, orange = 0xFFA500, gray = 0x808080,
  33.     yellow = 0xFFFF00, lime = 0x00FF00
  34. }
  35.  
  36. for i = 1, wD do
  37.     gameBoard[i] = {}
  38.     for j = 1, hT do
  39.         gameBoard[i][j] = nil
  40.     end
  41. end
  42.  
  43. for i = 1, hT do
  44.     gameBoard[math.floor((wD/2)-5)][i] = {
  45.         ["char"] = "@",
  46.         ["charColor"] = colors.red,
  47.         ["bgColor"] = colors.pink
  48.     }
  49.     gameBoard[math.floor((wD/2)+5)][i] = {
  50.         ["char"] = "@",
  51.         ["charColor"] = colors.blue,
  52.         ["bgColor"] = colors.lightBlue
  53.     }
  54. end
  55.  
  56. local function drawGameBoard()
  57.     for k, v in pairs(gameBoard) do
  58.         for p, b in pairs(v) do
  59.             if b ~= nil then
  60.                 if b["bgColor"] then
  61.                     gpu.setBackground(b["bgColor"])
  62.                     gpu.fill(k, p+1, 1, 1, " ")
  63.                 else
  64.                     gpu.setBackground(colors.black)
  65.                     gpu.fill(k, p, 1, 1, " ")
  66.                 end
  67.                 gpu.setForeground(b["charColor"])
  68.                 gpu.set(k, p+1, b["char"])
  69.             end
  70.         end
  71.     end
  72. end
  73.  
  74. local function updateGameBoard()
  75.     local updatedBoard = {}
  76.     for k, v in pairs(gameBoard) do
  77.         updatedBoard[k] = {}
  78.         for p, b in pairs(v) do
  79.             updatedBoard[k][p] = b
  80.         end
  81.     end
  82.  
  83.     for k, v in pairs(gameBoard) do
  84.         for p, b in pairs(v) do
  85.             if b ~= nil then
  86.                 if b.velocity then
  87.                     local newPos = k + b.velocity
  88.                     if newPos > wD or newPos <= 0 then
  89.                         if newPos > wD then points.left = points.left + 1 end
  90.                         if newPos <= 0 then points.right = points.right + 1 end
  91.                         updatedBoard[k][p] = nil
  92.                     else
  93.                         if updatedBoard[newPos][p] ~= nil then
  94.                             local collided = updatedBoard[newPos][p]
  95.                             if collided.explosion == nil and updatedBoard[k][p].explosion ~= true and updatedBoard[newPos][p].onCollision == nil then
  96.                                 for i = 1, 3 do
  97.                                     for j = 1, 3 do
  98.                                         local charChoices = {"@", "#", "*"}
  99.                                         local colorChoices = {colors.red, colors.orange, colors.yellow}
  100.                                         local charChoice = charChoices[math.random(1, 3)]
  101.                                         local colorChoice = colorChoices[math.random(1, 3)]
  102.                                         if newPos+i-2 <= wD and p+j-2 > 0 then
  103.                                             if updatedBoard[newPos+i-2] then
  104.                                                 updatedBoard[newPos+i-2][p+j-2] = {
  105.                                                     ["char"] = charChoice,
  106.                                                     ["charColor"] = colors.white,
  107.                                                     ["bgColor"] = colorChoice,
  108.                                                     ["explosion"] = 6 + (math.random(-1, 2))
  109.                                                 }
  110.                                             end
  111.                                         end
  112.                                     end
  113.                                 end
  114.                             else
  115.                                 if updatedBoard[newPos][p].onCollision then
  116.                                     updatedBoard[newPos][p].onCollision(gameBoard[k][p])
  117.                                 end
  118.                                 updatedBoard[k][p] = nil
  119.                                 updatedBoard[newPos][p] = nil
  120.                             end
  121.                         else
  122.                             updatedBoard[newPos][p] = updatedBoard[k][p]
  123.                             local colorChoices = {colors.orange, colors.gray, colors.red}
  124.                             updatedBoard[k][p] = {
  125.                                 ["char"] = "@",
  126.                                 ["charColor"] = colorChoices[math.random(1, 3)],
  127.                                 ["bgColor"] = nil,
  128.                                 ["explosion"] = math.random(2, 3)
  129.                             }
  130.                         end
  131.                     end
  132.                 end
  133.                 if b.explosion then
  134.                     b.explosion = b.explosion - 1
  135.                     if b.explosion <= 0 then
  136.                         updatedBoard[k][p] = nil
  137.                     end
  138.                 end
  139.             end
  140.         end
  141.     end
  142.  
  143.     gameBoard = updatedBoard
  144.     gpu.setBackground(colors.black)
  145.     gpu.fill(1, 1, wD, hT+2, " ")
  146.     drawGameBoard()
  147.     gpu.setBackground(colors.gray)
  148.     gpu.fill(1, hT+2, wD, 1, " ")
  149.     gpu.fill(1, 1, wD, 1, " ")
  150.     gpu.setForeground(colors.white)
  151.     gpu.set(1, hT+2, msg)
  152.     gpu.setForeground(colors.blue)
  153.     gpu.set(1, 1, tostring(points.left))
  154.     gpu.setForeground(colors.red)
  155.     gpu.set(wD - #tostring(points.right), 1, tostring(points.right))
  156.  
  157.     if points.left >= points.win then
  158.         gpu.setBackground(colors.black)
  159.         gpu.fill(1, 1, wD, hT+2, " ")
  160.         gpu.setForeground(colors.lightBlue)
  161.         gpu.set(1, 1, "The player has won!")
  162.         os.exit()
  163.     elseif points.right >= points.win then
  164.         gpu.setBackground(colors.black)
  165.         gpu.fill(1, 1, wD, hT+2, " ")
  166.         gpu.setForeground(colors.red)
  167.         gpu.set(1, 1, "The AI has won!")
  168.         os.exit()
  169.     end
  170. end
  171.  
  172. local playerWait = 0
  173.  
  174. local function spawnMissile(y, v, plr)
  175.     local x = 0
  176.     local charChoice = "<"
  177.     if v <= -1 then
  178.         x = wD
  179.         charChoice = "<"
  180.     elseif v >= 1 then
  181.         x = 1
  182.         charChoice = ">"
  183.     end
  184.     gameBoard[x][y] = {
  185.         ["char"] = charChoice,
  186.         ["charColor"] = colors.white,
  187.         ["velocity"] = v
  188.     }
  189.     if plr then
  190.         playerWait = 0.5
  191.     end
  192. end
  193.  
  194. local function scanForMissiles(enemy)
  195.     local missiles = {}
  196.     for k, v in pairs(gameBoard) do
  197.         for p, b in pairs(v) do
  198.             if enemy and b.char == ">" then
  199.                 missiles[#missiles+1] = {
  200.                     ["x"] = k,
  201.                     ["y"] = p
  202.                 }
  203.             elseif enemy == false and b.char == "<" then
  204.                 missiles[#missiles+1] = {
  205.                     ["x"] = k,
  206.                     ["y"] = p
  207.                 }
  208.             end
  209.         end
  210.     end
  211.     return missiles
  212. end
  213.  
  214. local AIWait = 0
  215.  
  216. local function AIUpdate()
  217.     local missiles = scanForMissiles(true)
  218.     local counterMissiles = scanForMissiles(false)
  219.     local enemyMissile = false
  220.     for k, v in pairs(missiles) do
  221.         local notice = math.random(1, 5-difficulty)
  222.         if notice == 1 then
  223.             local found = false
  224.             for p, b in pairs(counterMissiles) do
  225.                 if b.y == v.y then
  226.                     found = true
  227.                 end
  228.             end
  229.             if not found then
  230.                 enemyMissile = true
  231.                 computer.pushSignal("fireMissile", v.y)
  232.             end
  233.         end
  234.     end
  235.     if not enemyMissile and difficulty >= 2 then
  236.         local chance = math.random(1, 30/difficulty)
  237.         if chance == 1 then
  238.             computer.pushSignal("fireMissile", math.random(1, hT))
  239.         end
  240.     end
  241. end
  242.  
  243. local function powerUp()
  244.     local chance = math.random(1, 200)
  245.     if chance == 7 then
  246.         msg = "A bonus box has spawned!"
  247.         local X = math.random(math.floor(wD/2)-4, math.floor(wD/2)+4)
  248.         local Y = math.random(math.floor(hT/2)-4, math.floor(hT/2)+4)
  249.         gameBoard[X][Y] = {
  250.             ["char"] = "$",
  251.             ["charColor"] = colors.white,
  252.             ["bgColor"] = colors.lime,
  253.             ["onCollision"] = function(collided)
  254.                 if someoneWasDelayed == 0 then
  255.                     if collided.char == "<" then
  256.                         playerWait = 3
  257.                         someoneWasDelayed = 1
  258.                     elseif collided.char == ">" then
  259.                         AIWait = 3
  260.                         someoneWasDelayed = 2
  261.                     end
  262.                 end
  263.             end
  264.         }
  265.     end
  266. end
  267.  
  268. local function main()
  269.     while true do
  270.         local startTime = computer.uptime()
  271.         powerUp()
  272.         AIWait = AIWait - 0.1
  273.         playerWait = playerWait - 0.1
  274.         msg = tostring(someoneWasDelayed)
  275.         if playerWait > 0 and someoneWasDelayed == 1 then
  276.             msg = "You cannot fire a missile for "..playerWait.." seconds!"
  277.         elseif playerWait <= 0 and someoneWasDelayed == 1 then
  278.             msg = ""
  279.             someoneWasDelayed = 0
  280.         end
  281.         if AIWait > 0 and someoneWasDelayed == 2 then
  282.             msg = "The AI cannot fire a missile for "..AIWait.." seconds!"
  283.         elseif AIWait <= 0 and someoneWasDelayed == 2 then
  284.             someoneWasDelayed = 0
  285.         end
  286.        
  287.         AIUpdate()
  288.         updateGameBoard()
  289.        
  290.         local e = {event.pull(0)}
  291.         if e[1] == "touch" then
  292.             local _, _, x, y = table.unpack(e)
  293.             y = y - 1
  294.             if y > hT then y = hT end
  295.             if x == 1 and playerWait <= 0 then
  296.                 spawnMissile(y, 1, true)
  297.             end
  298.         elseif e[1] == "fireMissile" then
  299.             if AIWait <= 0 then
  300.                 local y = e[2]
  301.                 if y > hT then y = hT end
  302.                 spawnMissile(y, -1)
  303.                 AIWait = ({0.8, 0.6, 0.5})[tdifficulty] or 0.6
  304.             end
  305.         elseif e[1] == "interrupted" then
  306.             gpu.setBackground(colors.black)
  307.             gpu.fill(1, 1, wD, hT+2, " ")
  308.             gpu.setForeground(colors.white)
  309.             gpu.set(1, 1, "Aw, you quit the game early..")
  310.             gpu.set(1, 2, "Oh well, thanks for playing!")
  311.             break
  312.         end
  313.        
  314.         local endTime = computer.uptime()
  315.         local sleepTime = 0.1 - (endTime - startTime)
  316.         if sleepTime > 0 then os.sleep(sleepTime) end
  317.     end
  318. end
  319.  
  320. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement