yeeeeeeeeeeeee

Doom

Apr 30th, 2025
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 34.84 KB | None | 0 0
  1.  
  2. -- Made by Xella
  3.  
  4. -- Ugly old code
  5. -- Couldn't be bothered to fix everything when updating to the new Pine3D
  6. -- Don't mod this mess. Please just make your own game which has readable code
  7.  
  8. local path = "/"..fs.getDir(shell.getRunningProgram())
  9. local Pine3D = require("Pine3D-minified")
  10. os.loadAPI(path.."/blittle")
  11.  
  12. local objects = {}
  13.  
  14. local gun = paintutils.loadImage(path.."/images/gun")
  15. local gunf = paintutils.loadImage(path.."/images/gunf")
  16. local bgun = paintutils.loadImage(path.."/images/bgun")
  17. local bgunf = paintutils.loadImage(path.."/images/bgunf")
  18.  
  19. local heart = paintutils.loadImage(path.."/images/heart")
  20. local bheart = paintutils.loadImage(path.."/images/bheart")
  21. local hearts = 5
  22.  
  23. local fire = paintutils.loadImage(path.."/images/fire")
  24. local bfire = paintutils.loadImage(path.."/images/bfire")
  25. local shootCooldown = 3 / 16
  26. local lastShot = os.clock() - shootCooldown
  27. local resetGame = false
  28.  
  29. local playerX = 0
  30. local playerY = 0
  31. local playerZ = -2
  32.  
  33. local playerSpeed = 3
  34. local playerTurnSpeed = 100
  35. local keysDown = {}
  36.  
  37. local gunbobX, gunbobY = 0, 0
  38.  
  39. local username = "Unnamed"
  40. local submitScore = true
  41. local graphics = "Good"
  42. local score = 0
  43. local scoreTime = 0
  44. local endGame = false
  45. local levelCount = 0
  46. local reloadedLevel = false
  47.  
  48. local xDoor = true
  49. local endless = false
  50.  
  51. local enemySpeed = 2.5
  52.  
  53. local FoV = 90
  54.  
  55. local playerDirectionHor = 0
  56. local playerDirectionVer = 0
  57.  
  58. local finishMode
  59.  
  60. local termWidth, termHeight = term.getSize()
  61.  
  62. function round(num, numDecimalPlaces)
  63.     local mult = 10^(numDecimalPlaces or 0)
  64.     return math.floor(num * mult + 0.5) / mult
  65. end
  66.  
  67. local ThreeDFrame = Pine3D.newFrame()
  68. local blittleOn = true
  69. ThreeDFrame:highResMode(blittleOn)
  70. ThreeDFrame:setBackgroundColor(colors.lightGray)
  71.  
  72. local environmentObjects = {
  73.     ThreeDFrame:newObject(Pine3D.models:plane({size = 100, y = -0.1, color = colors.orange}))
  74. }
  75.  
  76. local latestScore = {}
  77.  
  78. local function min(a, b)
  79.     if (a <= b) then
  80.         return a
  81.     end
  82.     return b
  83. end
  84.  
  85. local function loadSettings()
  86.     if (fs.exists(path.."/settings") == false) then
  87.         local file = fs.open(path.."/settings", "w")
  88.         file.writeLine("Unnamed")
  89.         file.writeLine("true")
  90.         file.writeLine("Good")
  91.         file.close()
  92.     end
  93.  
  94.     local file = fs.open(path.."/settings", "r")
  95.     username = file.readLine()
  96.     submitScore = file.readLine()
  97.     graphics = file.readLine()
  98.     file.close()
  99.  
  100.     if (graphics == "Good") then
  101.         blittleOn = true
  102.     else
  103.         blittleOn = false
  104.     end
  105.     ThreeDFrame:highResMode(blittleOn)
  106. end
  107.  
  108. local function saveSettings()
  109.     local file = fs.open(path.."/settings", "w")
  110.     file.writeLine(username)
  111.     file.writeLine(submitScore)
  112.     file.writeLine(graphics)
  113.     file.close()
  114. end
  115.  
  116. local function loadLevel(levelname)
  117.     if (reloadedLevel == false) then
  118.         objects = {}
  119.     end
  120.     local level = paintutils.loadImage(path.."/levels/"..levelname)
  121.  
  122.     for z, row in pairs(level) do
  123.         for x, value in pairs(row) do
  124.             if (value ~= nil and value > 0) then
  125.                 if (value == 1) then
  126.                     playerX = x
  127.                     playerY = 0
  128.                     playerZ = z
  129.                 end
  130.                 if (reloadedLevel == false) then
  131.                     local object = nil
  132.                     if (value == colors.orange) then
  133.                         object = ThreeDFrame:newObject("models/wallz", x, 0, z)
  134.                         object.solid = true
  135.                     elseif (value == colors.magenta) then
  136.                         object = ThreeDFrame:newObject("models/wallx", x, 0, z)
  137.                         object.solid = true
  138.                     elseif (value == colors.lightBlue) then
  139.                         object = ThreeDFrame:newObject("models/wallxz", x, 0, z)
  140.                         object.solid = true
  141.                     elseif (value == colors.red) then
  142.                         object = ThreeDFrame:newObject("models/doorz", x, 0, z)
  143.                         object.model = "doorz"
  144.                     elseif (value == colors.green) then
  145.                         object = ThreeDFrame:newObject("models/doorx", x, 0, z)
  146.                         object.model = "doorx"
  147.                     elseif (value == colors.yellow) then
  148.                         object = ThreeDFrame:newObject("models/enemy1", x, 0, z)
  149.                         object.model = "enemy1"
  150.                         object.lastHit = os.clock()
  151.                     elseif (value == colors.lime) then
  152.                         object = ThreeDFrame:newObject("models/enemy2", x, 0, z)
  153.                         object.model = "enemy2"
  154.                         object.lastHit = os.clock() - 1/20
  155.                     elseif (value == colors.pink) then
  156.                         object = ThreeDFrame:newObject("models/emerald", x, 0, z, 0, 45, 0)
  157.                         object.solid = true
  158.                     end
  159.  
  160.                     if object then
  161.                         objects[#objects+1] = object
  162.                     end
  163.                 end
  164.             end
  165.         end
  166.     end
  167. end
  168.  
  169. local function loadRandomLevel(xDoor)
  170.     if (xDoor == false) then
  171.         local levels = {"level3", "level5", "level7"}
  172.         loadedLevel = levels[math.random(1, table.getn(levels))]
  173.     else
  174.         local levels = {"level2", "level4", "level6", "level8"}
  175.         loadedLevel = levels[math.random(1, table.getn(levels))]
  176.     end
  177.  
  178.     loadLevel(loadedLevel)
  179. end
  180.  
  181. local function free(x, y, z)
  182.     for objectNr, object in pairs(objects) do
  183.         if object.solid then
  184.             if (x >= object[1] - 0.5 and x <= object[1] + 0.5) then
  185.                 if (y >= object[2] and y <= object[2] + 1) then
  186.                     if (z >= object[3] - 0.5 and z <= object[3] + 0.5) then
  187.                         return false
  188.                     end
  189.                 end
  190.             end
  191.         end
  192.     end
  193.     return true
  194. end
  195.  
  196. local function rendering()
  197.     ThreeDFrame:setCamera(playerX, playerY + 0.5, playerZ, 0, playerDirectionHor, playerDirectionVer)
  198.  
  199.     while true do
  200.         --ThreeDFrame:loadGround(backgroundColor1)
  201.         --ThreeDFrame:loadSky(backgroundColor2)
  202.         ThreeDFrame:drawObjects(environmentObjects)
  203.         ThreeDFrame:drawObjects(objects)
  204.         if (blittleOn) then
  205.             if (lastShot > os.clock() - shootCooldown) then
  206.                 ThreeDFrame.buffer:image(29+gunbobX+(termWidth-51), 8+gunbobY+(termHeight-19), bfire, true)
  207.                 ThreeDFrame.buffer:image(32+gunbobX+(termWidth-51), 10+gunbobY+(termHeight-19), bgunf, true)
  208.             else
  209.                 ThreeDFrame.buffer:image(32+gunbobX+(termWidth-51), 10+gunbobY+(termHeight-19), bgun, true)
  210.             end
  211.  
  212.             for i = 1, hearts do
  213.                 ThreeDFrame.buffer:image(2 + (i-1) * 6, 2, bheart, true)
  214.             end
  215.         else
  216.             if (lastShot > os.clock() - shootCooldown) then
  217.                 ThreeDFrame.buffer:image(29+gunbobX+(termWidth-51), 8+gunbobY+(termHeight-19), fire, false)
  218.                 ThreeDFrame.buffer:image(32+gunbobX+(termWidth-51), 10+gunbobY+(termHeight-19), gunf, false)
  219.             else
  220.                 ThreeDFrame.buffer:image(32+gunbobX+(termWidth-51), 10+gunbobY+(termHeight-19), gun, false)
  221.             end
  222.  
  223.             for i = 1, hearts do
  224.                 ThreeDFrame.buffer:image(2 + (i-1) * 6, 2, heart, false)
  225.             end
  226.         end
  227.         ThreeDFrame:drawBuffer()
  228.  
  229.         os.queueEvent("FakeEvent")
  230.         os.pullEvent("FakeEvent")
  231.     end
  232. end
  233.  
  234. local function shoot()
  235.     local bulletDirHor = playerDirectionHor
  236.     local bulletX = playerX
  237.     local bulletZ = playerZ
  238.  
  239.     local step = 0.15
  240.  
  241.     local hit = false
  242.     for i = 1, 10 / step do
  243.         bulletX = bulletX + math.cos(math.rad(playerDirectionHor)) * step
  244.         bulletZ = bulletZ + math.sin(math.rad(playerDirectionHor)) * step
  245.  
  246.         if (free(bulletX, 0, bulletZ) == false) then
  247.             hit = true
  248.             break
  249.         end
  250.  
  251.         for objectNr, object in pairs(objects) do
  252.             if (object.model == "enemy1" or object.model == "enemy2") then
  253.                 if ((math.abs(bulletX - object[1])^2 + math.abs(bulletZ - object[3]))^0.5 <= 0.5) then
  254.                     object:setModel("models/corpse")
  255.                     object.model = "models/corpse"
  256.                     hit = true
  257.                     score = score + 10
  258.                     break
  259.                 end
  260.             end
  261.         end
  262.  
  263.         if (hit) then
  264.             break
  265.         end
  266.     end
  267. end
  268.  
  269. local gunbobby = -1
  270.  
  271. local function inputPlayer(time)
  272.     local playerXvel = 0
  273.     local playerZvel = 0
  274.     local isMoving
  275.  
  276.     if (keysDown[keys.left]) then
  277.         playerDirectionHor = playerDirectionHor - playerTurnSpeed * time
  278.         if (playerDirectionHor <= -180) then
  279.             playerDirectionHor = playerDirectionHor + 360
  280.         end
  281.     end
  282.     if (keysDown[keys.right]) then
  283.         playerDirectionHor = playerDirectionHor + playerTurnSpeed * time
  284.         if (playerDirectionHor >= 180) then
  285.             playerDirectionHor = playerDirectionHor - 360
  286.         end
  287.     end
  288.     if (keysDown[keys.down]) then
  289.         playerDirectionVer = playerDirectionVer - playerTurnSpeed * time
  290.         if (playerDirectionVer < -80) then
  291.             playerDirectionVer = -80
  292.         end
  293.     end
  294.     if (keysDown[keys.up]) then
  295.         playerDirectionVer = playerDirectionVer + playerTurnSpeed * time
  296.         if (playerDirectionVer > 80) then
  297.             playerDirectionVer = 80
  298.         end
  299.     end
  300.     if (keysDown[keys.w]) then
  301.         playerXvel = playerSpeed * math.cos(math.rad(playerDirectionHor)) + playerXvel
  302.         playerZvel = playerSpeed * math.sin(math.rad(playerDirectionHor)) + playerZvel
  303.     end
  304.     if (keysDown[keys.s]) then
  305.         playerXvel = -playerSpeed * math.cos(math.rad(playerDirectionHor)) + playerXvel
  306.         playerZvel = -playerSpeed * math.sin(math.rad(playerDirectionHor)) + playerZvel
  307.     end
  308.     if (keysDown[keys.a]) then
  309.         playerXvel = playerSpeed * math.cos(math.rad(playerDirectionHor - 90)) + playerXvel
  310.         playerZvel = playerSpeed * math.sin(math.rad(playerDirectionHor - 90)) + playerZvel
  311.     end
  312.     if (keysDown[keys.d]) then
  313.         playerXvel = playerSpeed * math.cos(math.rad(playerDirectionHor + 90)) + playerXvel
  314.         playerZvel = playerSpeed * math.sin(math.rad(playerDirectionHor + 90)) + playerZvel
  315.     end
  316.  
  317.     if (keysDown[keys.space]) then
  318.         if (lastShot < os.clock() - shootCooldown) then
  319.             lastShot = os.clock()
  320.             shoot()
  321.         end
  322.     end
  323.  
  324.     if (math.abs(playerXvel) > 0.5) or (math.abs(playerZvel) > 0.5) then
  325.         if gunbobby == -1 then gunbobby = 1 end
  326.         gunbobby = (gunbobby + (math.abs(playerXvel*time) + math.abs(playerZvel*time)) * 2) % 360
  327.     else
  328.         gunbobby = -1
  329.     end
  330.     if gunbobby == -1 then
  331.         gunbobX = 0
  332.         gunbobY = 0
  333.     else
  334.         gunbobX = round(math.sin(gunbobby),1)
  335.         gunbobY = round(math.abs(math.cos(gunbobby)),1)
  336.     end
  337.  
  338.     if (free(playerX + playerXvel * time, playerY, playerZ)) then
  339.         playerX = playerX + playerXvel * time
  340.     end
  341.     if (free(playerX, playerY, playerZ + playerZvel * time)) then
  342.         playerZ = playerZ + playerZvel * time
  343.     end
  344.  
  345.     for _, object in pairs(objects) do
  346.         if (object.model == "doorx" or object.model == "doorz") then
  347.             local dx = object[1] - playerX
  348.             local dz = object[3] - playerZ
  349.             local distance = math.sqrt(dx^2 + dz^2)
  350.  
  351.             if (distance < 0.5) then
  352.                 if (endless == false) then
  353.                     reloadedLevel = true
  354.                     if (loadedLevel == "level1") then
  355.                         loadedLevel = "level2"
  356.                         reloadedLevel = false
  357.                     elseif (loadedLevel == "level2" and object.model == "doorx") then
  358.                         loadedLevel = "level3"
  359.                         reloadedLevel = false
  360.                     elseif (loadedLevel == "level3" and object.model == "doorz") then
  361.                         loadedLevel = "level4"
  362.                         reloadedLevel = false
  363.                     elseif (loadedLevel == "level4" and object.model == "doorx") then
  364.                         loadedLevel = "level5"
  365.                         reloadedLevel = false
  366.                     elseif (loadedLevel == "level5" and object.model == "doorz") then
  367.                         loadedLevel = "level6"
  368.                         reloadedLevel = false
  369.                     elseif (loadedLevel == "level6" and object.model == "doorx") then
  370.                         loadedLevel = "level7"
  371.                         reloadedLevel = false
  372.                     elseif (loadedLevel == "level7" and object.model == "doorz") then
  373.                         loadedLevel = "level8"
  374.                         reloadedLevel = false
  375.                     elseif (loadedLevel == "level8" and object.model == "doorx") then
  376.                         loadedLevel = "level9"
  377.                         reloadedLevel = false
  378.  
  379.                         backgroundColor1 = colors.lime
  380.                         backgroundColor2 = colors.lightBlue
  381.  
  382.                         playerDirectionHor = 270
  383.                         playerDirectionVer = 0
  384.  
  385.                         endGame = true
  386.                         score = score + hearts * 16 + 400 - 8*min(50, scoreTime)
  387.                     end
  388.  
  389.                     loadLevel(loadedLevel)
  390.                 else
  391.                     if (object.model == "doorx" and xDoor) then
  392.                         xDoor = false
  393.                         loadRandomLevel(xDoor)
  394.                         levelCount = levelCount + 1
  395.                     elseif (object.model == "doorz" and xDoor == false) then
  396.                         xDoor = true
  397.                         loadRandomLevel(xDoor)
  398.                         levelCount = levelCount + 1
  399.                     else
  400.                         reloadedLevel = true
  401.                         loadLevel(loadedLevel)
  402.                         reloadedLevel = false
  403.                     end
  404.                 end
  405.             end
  406.         end
  407.     end
  408.  
  409.     ThreeDFrame:setCamera(playerX, playerY + 0.5, playerZ, 0, playerDirectionHor, playerDirectionVer)
  410. end
  411.  
  412. local function smoothKeyInput()
  413.     --keysDown = {}
  414.     while true do
  415.         local sEvent, key = os.pullEvent()
  416.         if sEvent == "key" then
  417.             keysDown[key] = true
  418.             if (key == keys.g) then
  419.                 loadSettings()
  420.                 if (blittleOn == false) then
  421.                     blittleOn = true
  422.                     graphics = "Good"
  423.                     ThreeDFrame:highResMode(true)
  424.                 else
  425.                     blittleOn = false
  426.                     graphics = "Bad"
  427.                     ThreeDFrame:highResMode(false)
  428.                 end
  429.                 saveSettings()
  430.             elseif (key == keys.q) then
  431.                 playerDirectionHor = playerDirectionHor + 180
  432.                 playerDirectionVer = -playerDirectionVer
  433.             elseif key == keys.leftCtrl then
  434.                 resetGame = true
  435.             end
  436.         elseif sEvent == "key_up" then
  437.             keysDown[key] = nil
  438.         elseif sEvent == "term_resize" then
  439.             termWidth, termHeight = term.getSize()
  440.             ThreeDFrame:setSize(1, 1, termWidth, termHeight)
  441.         end
  442.     end
  443. end
  444.  
  445. local function lineOfSight(x, z)
  446.     local dx = x - playerX
  447.     local dz = z - playerZ
  448.     local lineDir = math.atan(dx / dz)
  449.     local distance = math.sqrt(dx^2 + dz^2)
  450.  
  451.     local curX = x
  452.     local curZ = z
  453.  
  454.     local step = 0.15
  455.  
  456.     local sight = true
  457.     for i = 1, distance / step do
  458.         curX = curX + math.cos(lineDir) * step
  459.         curZ = curZ + math.sin(lineDir) * step
  460.  
  461.         if (free(curX, 0, curZ) == false) then
  462.             sight = false
  463.             break
  464.         end
  465.     end
  466.  
  467.     return sight
  468. end
  469.  
  470. local function updateGame(time)
  471.     if (endGame == false) then
  472.         scoreTime = scoreTime + time
  473.     end
  474.  
  475.     for objectNr, object in pairs(objects) do
  476.         if (object.model == "enemy1") then
  477.             local dx = object[1] - playerX
  478.             local dz = object[3] - playerZ
  479.  
  480.             if (dz == 0) then
  481.                 dz = 0.00001
  482.             end
  483.  
  484.             local playerDir = math.deg(math.atan(dx/dz))
  485.             if (dz < 0) then
  486.                 if (dx < 0) then
  487.                     playerDir = playerDir - 180
  488.                 else
  489.                     playerDir = playerDir + 180
  490.                 end
  491.             else
  492.                 if (dx < 0) then
  493.                     playerDir = playerDir
  494.                 else
  495.                     playerDir = playerDir
  496.                 end
  497.             end
  498.  
  499.             object:setRot(nil, math.rad(playerDir + 90), nil)
  500.  
  501.             if (object.lastHit < os.clock() - 3) then
  502.                 if (lineOfSight(object[1], object[3])) then
  503.                     object.lastHit = os.clock()
  504.                     hearts = hearts - 1
  505.                 end
  506.             end
  507.         elseif (object.model == "enemy2") then
  508.             local dx = object[1] - playerX
  509.             local dz = object[3] - playerZ
  510.  
  511.             if (dz == 0) then
  512.                 dz = 0.00001
  513.             end
  514.  
  515.             local playerDir = math.deg(math.atan(dx/dz))
  516.             if (dz < 0) then
  517.                 if (dx < 0) then
  518.                     playerDir = playerDir - 180
  519.                 else
  520.                     playerDir = playerDir + 180
  521.                 end
  522.             else
  523.                 if (dx < 0) then
  524.                     playerDir = playerDir
  525.                 else
  526.                     playerDir = playerDir
  527.                 end
  528.             end
  529.  
  530.             object:setRot(nil, math.rad(playerDir + 90), nil)
  531.             local playerDistance = math.sqrt(dx^2 + dz^2)
  532.  
  533.             if (playerDistance >= 0.5) then
  534.                 local edX = -enemySpeed * math.sin(math.rad(playerDir)) * time
  535.                 local edZ = -enemySpeed * math.cos(math.rad(playerDir)) * time
  536.                 if (free(object[1] + edX, object[2], object[3])) then
  537.                     object[1] = object[1] + edX
  538.                 end
  539.                 if (free(object[1], object[2], object[3] + edZ)) then
  540.                     object[3] = object[3] + edZ
  541.                 end
  542.             end
  543.  
  544.             if (object.lastHit < os.clock() - 3) then
  545.                 if (playerDistance < 1) then
  546.                     object.lastHit = os.clock()
  547.                     hearts = hearts - 1
  548.                 end
  549.             end
  550.         end
  551.     end
  552. end
  553.  
  554. local function deathAnimation()
  555.     local blood = {{16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384},{16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,0,0,0,0,16384,16384,16384,16384,16384,16384,0,0,0,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,0,16384},{16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,16384,1,16384,16384,16384,16384,16384,16384,16384,0,0,0,0,0,0,16384,16384,16384,16384,0,0,0,0,16384,16384,1,16384,16384,0,0,0,0,16384,16384,16384,16384,16384,0,0,0},{16384,16384,16384,16384,16384,1,16384,16384,0,16384,16384,16384,1,1,16384,16384,0,0,16384,16384,0,0,0,0,0,0,0,16384,1,16384,16384,0,0,0,0,16384,16384,16384,16384,0,0,0,0,16384,16384,16384,16384,16384,0,0,0},{0,16384,16384,16384,1,1,16384,16384,0,0,0,16384,16384,16384,16384,16384,0,0,0,0,0,0,0,0,0,0,0,16384,1,16384,16384,0,0,0,0,16384,16384,16384,0,0,0,0,0,0,16384,16384,16384,16384,0,0,0},{0,0,0,16384,16384,16384,16384,0,0,0,0,0,0,16384,16384,0,0,0,0,0,0,0,0,0,0,0,0,16384,16384,16384,16384,0,0,0,0,0,0,0,0,0,0,0,0,0,16384,1,1,16384,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16384,16384,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16384,16384,1,16384,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16384,16384,16384,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16384,16384,16384,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16384,0,0,0,0}}
  556.     local scr_x, scr_y = term.getSize()
  557.     local function fade(col1, col2, delay)
  558.         local oldeTXT, oldeBG = term.getTextColor(), term.getBackgroundColor()
  559.         term.setBackgroundColor(col1)
  560.         term.setTextColor(col2)
  561.         term.clear()
  562.         sleep(delay or 0)
  563.         for a = 1, 2 do
  564.             for y = 1, scr_y do
  565.                 term.setCursorPos((y%2),y)
  566.                 term.write(("\127 "):rep(math.ceil(scr_x/2)+1))
  567.             end
  568.             sleep(delay or 0)
  569.             if a == 1 then
  570.                 for y = 1, scr_y do
  571.                     term.setCursorPos(1,y)
  572.                     term.write(("\127"):rep(scr_x))
  573.                 end
  574.             else
  575.                 term.clear()
  576.             end
  577.             sleep(delay or 0)
  578.             term.setBackgroundColor(col2)
  579.             term.setTextColor(col1)
  580.         end
  581.         sleep(0 or delay)
  582.         term.setTextColor(oldeTXT)
  583.         term.setBackgroundColor(oldeBG)
  584.     end
  585.  
  586.     local topcenterwrite = function(txt)
  587.         term.setCursorPos((scr_x/2)-(#txt/2) + 1,1)
  588.         term.write(txt)
  589.     end
  590.  
  591.     if scr_x == 51 then
  592.         for y = 1, #blood do
  593.             paintutils.drawImage(blood,1,y-#blood)
  594.             sleep(0.1)
  595.         end
  596.     end
  597.     term.setBackgroundColor(colors.red)
  598.     term.setTextColor(colors.white)
  599.     local script = {
  600.         "You feel the last of",
  601.         "your life drain away",
  602.         "as your body succumbs to",
  603.         "the deeply inflicted wounds",
  604.         "you failed to prevent.",
  605.         "",
  606.         "Your lungs collapse.",
  607.         "",
  608.         "GAME OVER"
  609.     }
  610.     local skip = false
  611.     for y = scr_y, 1, -1 do
  612.         term.scroll(-1)
  613.         if script[y-4] then
  614.             topcenterwrite(script[y-4])
  615.         end
  616.         if (skip) then
  617.             sleep(0)
  618.             skip = false
  619.         else
  620.             skip = true
  621.         end
  622.     end
  623.     --sleep(0.5)
  624.     os.pullEvent("char")
  625.     term.clear()
  626.     sleep(0.1)
  627.     fade(colors.red,colors.black,0.1)
  628.     sleep(0.2)
  629.     term.setBackgroundColor(colors.black)
  630.     term.setCursorPos(1,1)
  631.     term.clear()
  632. end
  633.  
  634. local function gameUpdate()
  635.     local timeFromLastUpdate = os.clock()
  636.     --local avgUpdateSpeed = 0
  637.     --local updateCount = 0
  638.     --local timeOff = 0
  639.  
  640.     while true do
  641.         local currentTime = os.clock()
  642.         --[[if (currentTime > timeFromLastUpdate) then
  643.             updateGame(currentTime - timeFromLastUpdate - timeOff)
  644.             inputPlayer(currentTime - timeFromLastUpdate - timeOff)
  645.             avgUpdateSpeed = (currentTime - timeFromLastUpdate) / (updateCount + 1)
  646.             updateCount = 0
  647.             timeOff = 0
  648.         else
  649.             updateGame(avgUpdateSpeed)
  650.             newTimeOff = timeOff + avgUpdateSpeed
  651.             newUpdateCount = updateCount + 1
  652.         end]]--
  653.         local dt = currentTime - timeFromLastUpdate
  654.         updateGame(dt)
  655.         inputPlayer(dt)
  656.         timeFromLastUpdate = currentTime
  657.  
  658.         if resetGame then
  659.             finishMode = "reset"
  660.             break
  661.         end
  662.  
  663.         --sleep(0)
  664.         os.queueEvent("gameupdate")
  665.         os.pullEvent("gameupdate")
  666.  
  667.         if (hearts <= 0) then
  668.             finishMode = "death"
  669.             break
  670.         end
  671.         if (endGame) then
  672.             finishMode = "finish"
  673.             break
  674.         end
  675.     end
  676. end
  677.  
  678. local function viewHighscores(endless)
  679.     term.setBackgroundColor(colors.black)
  680.     term.setTextColor(colors.yellow)
  681.     term.clear()
  682.  
  683.     term.setCursorPos(12+(termWidth-51)/2, 10)
  684.     write("Connecting to the database...")
  685.  
  686.     local rawData = {}
  687.     if (endless == false) then
  688.         rawData = http.get("https://doom.pine3d.cc/api/highscores")
  689.     else
  690.         rawData = http.get("https://doom.pine3d.cc/api/highscoresendless")
  691.     end
  692.  
  693.     if (rawData == nil) then
  694.         term.setBackgroundColor(colors.black)
  695.         term.setTextColor(colors.red)
  696.         term.clear()
  697.         term.setCursorPos(2, 2)
  698.         write("Server is not reachable.")
  699.     else
  700.         local rawData2 = rawData.readAll()
  701.  
  702.         term.clear()
  703.         term.setCursorPos(2+(termWidth-51)/2, 2)
  704.         write("Online Highscores:")
  705.  
  706.         term.setTextColor(colors.red)
  707.         term.setCursorPos(2+(termWidth-51)/2, 4)
  708.         if (endless == false) then
  709.             write("#  Nickname             Score  Time    Date")
  710.         else
  711.             write("#  Nickname             Score  Levels  Date")
  712.         end
  713.         term.setTextColor(colors.orange)
  714.         local recordNr = 0
  715.         for record in rawData2:gmatch("[^~]*~") do
  716.             record = record:gsub("~", "")
  717.  
  718.             local values = {}
  719.             for recordField in record:gmatch("[^;]*;") do
  720.                 values[#values+1] = recordField
  721.             end
  722.  
  723.             local val2 = values[2]:gsub(";", "")
  724.             local val3 = values[3]:gsub(";", "")
  725.  
  726.             if values[1]:gsub(";", "") == latestScore[1] and tonumber(val2) == math.floor(latestScore[2]*10 + 0.5)/10 and tonumber(val3) == math.floor(latestScore[3]*100 + 0.5)/100 then
  727.                 term.setTextColor(colors.white)
  728.             else
  729.                 term.setTextColor(colors.orange)
  730.             end
  731.  
  732.             term.setCursorPos(2+(termWidth-51)/2, 6 + recordNr)
  733.             write(recordNr + 1)
  734.  
  735.             for i = 1, #values do
  736.                 local value = values[i]
  737.                 if (i == 1) then
  738.                     term.setCursorPos(5+(termWidth-51)/2, 6 + recordNr)
  739.                 elseif (i == 2) then
  740.                     term.setCursorPos(26+(termWidth-51)/2, 6 + recordNr)
  741.                 elseif (i == 3) then
  742.                     term.setCursorPos(33+(termWidth-51)/2, 6 + recordNr)
  743.                 elseif (i == 4) then
  744.                     term.setCursorPos(41+(termWidth-51)/2, 6 + recordNr)
  745.                     local year = ""
  746.                     local month = ""
  747.                     local day = ""
  748.  
  749.                     local partNr = 0
  750.                     for part in value:gmatch("[^-]*") do
  751.                         if (partNr == 0) then
  752.                             year = part
  753.                         elseif (partNr == 2) then
  754.                             month = part
  755.                         elseif (partNr == 4) then
  756.                             day = part
  757.                         end
  758.  
  759.                         partNr = partNr + 1
  760.                     end
  761.  
  762.                     value = day.."-"..month.."-"..year
  763.                 end
  764.  
  765.                 write(value:gsub(";", ""))
  766.             end
  767.             recordNr = recordNr + 1
  768.  
  769.             if (recordNr >= 11 + (termHeight-19)) then
  770.                 break
  771.             end
  772.         end
  773.     end
  774.  
  775.     sleep(1)
  776.  
  777.     term.setTextColor(colors.yellow)
  778.     term.setCursorPos(2+(termWidth-51)/2, termHeight-1)
  779.     write("Press any key to close...")
  780.     sleep(0.5)
  781.     while true do
  782.         local event, key = os.pullEvent()
  783.         if event == "term_resize" then
  784.             termWidth, termHeight = term.getSize()
  785.             ThreeDFrame:setSize(1, 1, termWidth, termHeight)
  786.             return viewHighscores(endless)
  787.         elseif event == "key" and key ~= keys.leftAlt and key ~= keys.f2 then
  788.             return
  789.         end
  790.     end
  791. end
  792.  
  793. local function startGame()
  794.     loadSettings()
  795.     resetGame = false
  796.     ThreeDFrame:highResMode(blittleOn)
  797.     parallel.waitForAny(smoothKeyInput, rendering, gameUpdate)
  798. end
  799.  
  800. local function newGameNormal()
  801.     score = 0
  802.     scoreTime = 0
  803.     hearts = 5
  804.     endGame = false
  805.     playerDirectionHor = 0
  806.     playerDirectionVer = 0
  807.     reloadedLevel = false
  808.  
  809.     loadedLevel = "level1"
  810.     loadLevel(loadedLevel)
  811.     endless = false
  812.     backgroundColor1 = colors.orange
  813.     backgroundColor2 = colors.lightGray
  814.  
  815.     startGame()
  816.  
  817.     if finishMode == "reset" then
  818.         return newGameNormal()
  819.     end
  820.  
  821.     if (hearts > 0) then
  822.         term.setBackgroundColor(colors.black)
  823.         term.setTextColor(colors.red)
  824.         term.clear()
  825.  
  826.         term.setCursorPos(2, 2)
  827.         write("You have survived!")
  828.  
  829.         term.setTextColor(colors.orange)
  830.         term.setCursorPos(2, 4)
  831.         write("Score: "..(math.floor(score*10+0.5)/10))
  832.         term.setCursorPos(2, 5)
  833.         write("Time: "..(math.floor(scoreTime*100+0.5)/100))
  834.  
  835.         if (submitScore == "true") then
  836.             latestScore = {username, score, scoreTime}
  837.             local result = http.get("https://doom.pine3d.cc/api/newscore?name="..textutils.urlEncode(username).."&score="..score.."&time="..scoreTime)
  838.             if (result == nil) then
  839.                 term.setBackgroundColor(colors.black)
  840.                 term.setTextColor(colors.red)
  841.                 term.clear()
  842.                 term.setCursorPos(2, 2)
  843.                 write("Server is not reachable. Score has not been submitted.")
  844.             end
  845.         end
  846.         sleep(2)
  847.         term.setTextColor(colors.yellow)
  848.         term.setCursorPos(2, termHeight-1)
  849.         write("Press any key to close...")
  850.         os.pullEvent("key")
  851.  
  852.         if (submitScore == "true") then
  853.             viewHighscores(false)
  854.         end
  855.     else
  856.         deathAnimation()
  857.     end
  858. end
  859.  
  860. local function newGameEndless()
  861.     score = 0
  862.     time = 0
  863.     hearts = 8
  864.     endGame = false
  865.     playerDirectionHor = 0
  866.     playerDirectionVer = 0
  867.     reloadedLevel = false
  868.  
  869.     xDoor = false
  870.     loadRandomLevel(xDoor)
  871.     endless = true
  872.     levelCount = 0
  873.     backgroundColor1 = colors.orange
  874.     backgroundColor2 = colors.lightGray
  875.  
  876.     startGame()
  877.  
  878.     if finishMode == "reset" then
  879.         return newGameNormal()
  880.     end
  881.  
  882.     score = score + levelCount * 22
  883.     deathAnimation()
  884.     term.setBackgroundColor(colors.black)
  885.     term.setTextColor(colors.red)
  886.     term.clear()
  887.  
  888.     term.setCursorPos(2, 2)
  889.     write("You died!")
  890.  
  891.     term.setTextColor(colors.orange)
  892.     term.setCursorPos(2, 4)
  893.     write("Score: "..(math.floor(score*10+0.5)/10))
  894.     term.setCursorPos(2, 5)
  895.     write("Levels: "..levelCount)
  896.  
  897.     if (submitScore == "true") then
  898.         latestScore = {username, score, levelCount}
  899.         local result = http.get("https://doom.pine3d.cc/api/newscoreendless?name="..textutils.urlEncode(username).."&score="..score.."&levels="..levelCount)
  900.         if (result == nil) then
  901.             term.setBackgroundColor(colors.black)
  902.             term.setTextColor(colors.red)
  903.             term.clear()
  904.             term.setCursorPos(2, 2)
  905.             write("Server is not reachable. Score has not been submitted.")
  906.         end
  907.     end
  908.     sleep(2)
  909.     term.setTextColor(colors.yellow)
  910.     term.setCursorPos(2, termHeight-1)
  911.     write("Press any key to close...")
  912.     os.pullEvent("key")
  913.  
  914.     if (submitScore == "true") then
  915.         viewHighscores(true)
  916.     end
  917. end
  918.  
  919. local function newGame()
  920.     term.setBackgroundColor(colors.black)
  921.     term.setTextColor(colors.red)
  922.     term.clear()
  923.  
  924.     term.setCursorPos(21+(termWidth-51)/2, 8)
  925.     write("Normal Mode")
  926.  
  927.     term.setCursorPos(20+(termWidth-51)/2, 10)
  928.     write("Endless Mode")
  929.  
  930.     term.setCursorPos(24+(termWidth-51)/2, 12)
  931.     write("Back")
  932.  
  933.     local selected2 = 0
  934.     while true do
  935.         term.setTextColor(colors.yellow)
  936.         term.setCursorPos(19+(termWidth-51)/2, 8)
  937.         if (selected2 == 0) then
  938.             write(">")
  939.         else
  940.             write(" ")
  941.         end
  942.         term.setCursorPos(18+(termWidth-51)/2, 10)
  943.         if (selected2 == 1) then
  944.             write(">")
  945.         else
  946.             write(" ")
  947.         end
  948.         term.setCursorPos(22+(termWidth-51)/2, 12)
  949.         if (selected2 == 2) then
  950.             write(">")
  951.         else
  952.             write(" ")
  953.         end
  954.  
  955.         local event, key = os.pullEvent()
  956.         if event == "key" then
  957.             if (key == keys.w or key == keys.up) then
  958.                 selected2 = (selected2 - 1 + 3) % 3
  959.             elseif (key == keys.s or key == keys.down) then
  960.                 selected2 = (selected2 + 1) % 3
  961.             elseif (key == keys.space or key == keys.enter) then
  962.                 if (selected2 == 0) then
  963.                     newGameNormal()
  964.                 elseif (selected2 == 1) then
  965.                     newGameEndless()
  966.                 end
  967.                 break
  968.             end
  969.         elseif event == "term_resize" then
  970.             termWidth, termHeight = term.getSize()
  971.             ThreeDFrame:setSize(1, 1, termWidth, termHeight)
  972.             term.setBackgroundColor(colors.black)
  973.             term.setTextColor(colors.red)
  974.             term.clear()
  975.  
  976.             term.setCursorPos(21+(termWidth-51)/2, 8)
  977.             write("Normal Mode")
  978.  
  979.             term.setCursorPos(20+(termWidth-51)/2, 10)
  980.             write("Endless Mode")
  981.  
  982.             term.setCursorPos(24+(termWidth-51)/2, 12)
  983.             write("Back")
  984.         end
  985.     end
  986. end
  987.  
  988. local function showHighscores()
  989.     term.setBackgroundColor(colors.black)
  990.     term.setTextColor(colors.red)
  991.     term.clear()
  992.  
  993.     term.setCursorPos(21+(termWidth-51)/2, 8)
  994.     write("Normal Mode")
  995.  
  996.     term.setCursorPos(20+(termWidth-51)/2, 10)
  997.     write("Endless Mode")
  998.  
  999.     term.setCursorPos(24+(termWidth-51)/2, 12)
  1000.     write("Back")
  1001.  
  1002.     local selected2 = 0
  1003.     while true do
  1004.         term.setTextColor(colors.yellow)
  1005.         term.setCursorPos(19+(termWidth-51)/2, 8)
  1006.         if (selected2 == 0) then
  1007.             write(">")
  1008.         else
  1009.             write(" ")
  1010.         end
  1011.         term.setCursorPos(18+(termWidth-51)/2, 10)
  1012.         if (selected2 == 1) then
  1013.             write(">")
  1014.         else
  1015.             write(" ")
  1016.         end
  1017.         term.setCursorPos(22+(termWidth-51)/2, 12)
  1018.         if (selected2 == 2) then
  1019.             write(">")
  1020.         else
  1021.             write(" ")
  1022.         end
  1023.  
  1024.         local event, key = os.pullEvent()
  1025.         if event == "key" then
  1026.             if (key == keys.w or key == keys.up) then
  1027.                 selected2 = (selected2 - 1 + 3) % 3
  1028.             elseif (key == keys.s or key == keys.down) then
  1029.                 selected2 = (selected2 + 1) % 3
  1030.             elseif (key == keys.space or key == keys.enter) then
  1031.                 if (selected2 == 0) then
  1032.                     viewHighscores(false)
  1033.                 elseif (selected2 == 1) then
  1034.                     viewHighscores(true)
  1035.                 end
  1036.                 break
  1037.             end
  1038.         elseif event == "term_resize" then
  1039.             termWidth, termHeight = term.getSize()
  1040.             ThreeDFrame:setSize(1, 1, termWidth, termHeight)
  1041.             term.setBackgroundColor(colors.black)
  1042.             term.setTextColor(colors.red)
  1043.             term.clear()
  1044.  
  1045.             term.setCursorPos(21+(termWidth-51)/2, 8)
  1046.             write("Normal Mode")
  1047.  
  1048.             term.setCursorPos(20+(termWidth-51)/2, 10)
  1049.             write("Endless Mode")
  1050.  
  1051.             term.setCursorPos(24+(termWidth-51)/2, 12)
  1052.             write("Back")
  1053.         end
  1054.     end
  1055. end
  1056.  
  1057. local function drawMenu()
  1058.     local logo = paintutils.loadImage(path.."/images/logo")
  1059.     local blogo = blittle.shrink(logo)
  1060.     term.setBackgroundColor(colors.black)
  1061.     term.setTextColor(colors.red)
  1062.     term.clear()
  1063.     blittle.draw(blogo, 12+(termWidth-51)/2, 3)
  1064.  
  1065.     term.setCursorPos(21+(termWidth-51)/2, 10)
  1066.     write("Start Game")
  1067.  
  1068.     term.setCursorPos(17+(termWidth-51)/2, 12)
  1069.     write("Online Highscores")
  1070.  
  1071.     term.setCursorPos(22+(termWidth-51)/2, 14)
  1072.     write("Settings")
  1073.  
  1074.     term.setCursorPos(24+(termWidth-51)/2, 16)
  1075.     write("Exit")
  1076.  
  1077.     term.setTextColor(colors.yellow)
  1078.     term.setCursorPos(1, termHeight)
  1079.     write("Copyright (c) 2022 Xella")
  1080. end
  1081.  
  1082. local function drawSettings()
  1083.     term.setBackgroundColor(colors.black)
  1084.     term.setTextColor(colors.yellow)
  1085.     term.clear()
  1086.  
  1087.     term.setCursorPos(2, 2)
  1088.     write("Settings:")
  1089.  
  1090.     term.setCursorPos(4, 4)
  1091.     write("Change name | ")
  1092.     term.setTextColor(colors.orange)
  1093.     write(username)
  1094.  
  1095.     term.setCursorPos(4, 6)
  1096.     term.setTextColor(colors.yellow)
  1097.     write("Submit score | ")
  1098.     if (submitScore == "true") then
  1099.         term.setTextColor(colors.lime)
  1100.         write("Enabled")
  1101.     else
  1102.         term.setTextColor(colors.red)
  1103.         write("Disabled")
  1104.     end
  1105.  
  1106.     term.setCursorPos(4, 8)
  1107.     term.setTextColor(colors.yellow)
  1108.     write("Graphics | ")
  1109.     if (graphics == "Good") then
  1110.         term.setTextColor(colors.lime)
  1111.     else
  1112.         term.setTextColor(colors.red)
  1113.     end
  1114.     write(graphics.."   ")
  1115.     term.setTextColor(colors.yellow)
  1116.     write("(Toggle in-game using \"G\")")
  1117.  
  1118.     term.setTextColor(colors.orange)
  1119.     term.setCursorPos(4, 10)
  1120.     write("Save and close")
  1121. end
  1122.  
  1123. local function changeName()
  1124.     term.setBackgroundColor(colors.black)
  1125.     term.setTextColor(colors.yellow)
  1126.     term.clear()
  1127.  
  1128.     term.setCursorPos(2, 2)
  1129.     write("Enter a username (3-20 characters):")
  1130.  
  1131.     while true do
  1132.         term.setCursorPos(2, 4)
  1133.         term.clearLine()
  1134.         term.setCursorPos(2, 4)
  1135.         term.setTextColor(colors.red)
  1136.         write("Username: ")
  1137.         term.setTextColor(colors.orange)
  1138.         local usernameCandidate = read()
  1139.         if (string.len(usernameCandidate) >= 3 and string.len(usernameCandidate) <= 20) then
  1140.             username = usernameCandidate
  1141.             break
  1142.         else
  1143.             term.setBackgroundColor(colors.black)
  1144.             term.setTextColor(colors.red)
  1145.             term.setCursorPos(2, 6)
  1146.             write("Your new username must be at least 3 and at most\n 20 characters long!")
  1147.         end
  1148.     end
  1149.  
  1150.     term.setBackgroundColor(colors.black)
  1151.     term.setTextColor(colors.yellow)
  1152.     term.clear()
  1153.  
  1154.     term.setCursorPos(2, 2)
  1155.     write("Your new username is: ")
  1156.  
  1157.     term.setTextColor(colors.orange)
  1158.     write(username)
  1159. end
  1160.  
  1161. local function settingsMenu()
  1162.     loadSettings()
  1163.  
  1164.     drawSettings()
  1165.     local selected2 = 0
  1166.     while true do
  1167.         term.setTextColor(colors.yellow)
  1168.         term.setCursorPos(2, 4)
  1169.         if (selected2 == 0) then
  1170.             write(">")
  1171.         else
  1172.             write(" ")
  1173.         end
  1174.         term.setCursorPos(2, 6)
  1175.         if (selected2 == 1) then
  1176.             write(">")
  1177.         else
  1178.             write(" ")
  1179.         end
  1180.         term.setCursorPos(2, 8)
  1181.         if (selected2 == 2) then
  1182.             write(">")
  1183.         else
  1184.             write(" ")
  1185.         end
  1186.         term.setCursorPos(2, 10)
  1187.         if (selected2 == 3) then
  1188.             write(">")
  1189.         else
  1190.             write(" ")
  1191.         end
  1192.  
  1193.         local event, key = os.pullEvent()
  1194.         if event == "key" then
  1195.             if (key == keys.w or key == keys.up) then
  1196.                 selected2 = (selected2 - 1 + 4) % 4
  1197.             elseif (key == keys.s or key == keys.down) then
  1198.                 selected2 = (selected2 + 1) % 4
  1199.             elseif (key == keys.space or key == keys.enter) then
  1200.                 if (selected2 == 0) then
  1201.                     changeName()
  1202.                     drawSettings()
  1203.                 elseif (selected2 == 1) then
  1204.                     if (submitScore == "true") then
  1205.                         submitScore = "false"
  1206.                     else
  1207.                         submitScore = "true"
  1208.                     end
  1209.  
  1210.                     term.setCursorPos(4, 6)
  1211.                     term.setTextColor(colors.yellow)
  1212.                     write("Submit score | ")
  1213.                     if (submitScore == "true") then
  1214.                         term.setTextColor(colors.lime)
  1215.                         write("Enabled ")
  1216.                     else
  1217.                         term.setTextColor(colors.red)
  1218.                         write("Disabled")
  1219.                     end
  1220.                 elseif (selected2 == 2) then
  1221.                     if (graphics == "Good") then
  1222.                         graphics = "Bad"
  1223.                         blittleOn = false
  1224.                         ThreeDFrame:highResMode(blittleOn)
  1225.                     else
  1226.                         graphics = "Good"
  1227.                         blittleOn = true
  1228.                         ThreeDFrame:highResMode(blittleOn)
  1229.                     end
  1230.  
  1231.                     term.setCursorPos(4, 8)
  1232.                     term.setTextColor(colors.yellow)
  1233.                     write("Graphics | ")
  1234.                     if (graphics == "Good") then
  1235.                         term.setTextColor(colors.lime)
  1236.                     else
  1237.                         term.setTextColor(colors.red)
  1238.                     end
  1239.                     write(graphics.." ")
  1240.                 elseif (selected2 == 3) then
  1241.                     break
  1242.                 end
  1243.             end
  1244.         elseif event == "term_resize" then
  1245.             termWidth, termHeight = term.getSize()
  1246.             ThreeDFrame:setSize(1, 1, termWidth, termHeight)
  1247.             drawSettings()
  1248.         end
  1249.     end
  1250.  
  1251.     saveSettings()
  1252. end
  1253.  
  1254. local function mainMenu()
  1255.     drawMenu()
  1256.     selected = 0
  1257.     while true do
  1258.         term.setTextColor(colors.yellow)
  1259.         term.setCursorPos(19+(termWidth-51)/2, 10)
  1260.         if (selected == 0) then
  1261.             write(">")
  1262.         else
  1263.             write(" ")
  1264.         end
  1265.         term.setCursorPos(15+(termWidth-51)/2, 12)
  1266.         if (selected == 1) then
  1267.             write(">")
  1268.         else
  1269.             write(" ")
  1270.         end
  1271.         term.setCursorPos(20+(termWidth-51)/2, 14)
  1272.         if (selected == 2) then
  1273.             write(">")
  1274.         else
  1275.             write(" ")
  1276.         end
  1277.         term.setCursorPos(22+(termWidth-51)/2, 16)
  1278.         if (selected == 3) then
  1279.             write(">")
  1280.         else
  1281.             write(" ")
  1282.         end
  1283.  
  1284.         local event, key = os.pullEvent()
  1285.         if event == "key" then
  1286.             if (key == keys.w or key == keys.up) then
  1287.                 selected = (selected - 1 + 4) % 4
  1288.             elseif (key == keys.s or key == keys.down) then
  1289.                 selected = (selected + 1) % 4
  1290.             elseif (key == keys.space or key == keys.enter) then
  1291.                 if (selected == 0) then
  1292.                     newGame()
  1293.                 elseif (selected == 1) then
  1294.                     showHighscores()
  1295.                 elseif (selected == 2) then
  1296.                     settingsMenu()
  1297.                 elseif (selected == 3) then
  1298.                     term.setBackgroundColor(colors.black)
  1299.                     term.setTextColor(colors.yellow)
  1300.                     term.clear()
  1301.                     term.setCursorPos(14+(termWidth-51)/2, 10)
  1302.                     write("Thanks for playing Doom!")
  1303.                     sleep(1)
  1304.                     term.clear()
  1305.                     term.setCursorPos(1, 1)
  1306.                     break
  1307.                 end
  1308.                 drawMenu()
  1309.             end
  1310.         elseif event == "term_resize" then
  1311.             termWidth, termHeight = term.getSize()
  1312.             ThreeDFrame:setSize(1, 1, termWidth, termHeight)
  1313.             drawMenu()
  1314.         end
  1315.     end
  1316. end
  1317.  
  1318. local function resizing()
  1319.     while true do
  1320.         os.pullEvent("term_resize")
  1321.         termWidth, termHeight = term.getSize()
  1322.         ThreeDFrame:setSize(1, 1, termWidth, termHeight)
  1323.     end
  1324. end
  1325.  
  1326. parallel.waitForAny(mainMenu, resizing)
Add Comment
Please, Sign In to add comment