Advertisement
MrGamerDu29SurTel

Protector

May 15th, 2021
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 31.52 KB | None | 0 0
  1. function initVariables()
  2.     w,h =term.getSize()  -- 51,  19
  3.     shipYPos = 10
  4.     shipXPos = 24
  5.     shipFacingRight=true
  6.     pressedKey=0
  7.     killedState = false
  8.     lives=3
  9.     score =0
  10.     aliens=10
  11.     killedDelay=0
  12.     running=true
  13.     moveLeft=false
  14.     moveRight=true
  15.     moveUp=false
  16.     moveDown=false
  17.     human1x = 3
  18.     human1y = 18
  19.     human2x = 15
  20.     human2y = 18
  21.     human3x = 40
  22.     human3y = 18
  23.     human4x = 60
  24.     human4y = 18
  25.     human5x = 70
  26.     human5y = 18
  27.     human6x = 85
  28.     human6y = 18
  29.     human1 = true
  30.     human2 = true
  31.     human3 = true
  32.     human4 = true
  33.     human5 = true
  34.     human6 = true
  35.     human1Abducted=false
  36.     human2Abducted=false
  37.     human3Abducted=false
  38.     human4Abducted=false
  39.     human5Abducted=false
  40.     human6Abducted=false
  41.     humansLeft=6
  42.     bulletXPos=0
  43.     bulletYPos=0
  44.     bulletState=false
  45.     bulletGoingRight=true
  46.     alien1 = false
  47.     alien1y = 2
  48.     alien1x =84
  49.     alien1Abduct=false
  50.     alien1Carry=false
  51.     alien1Step=2
  52.     stepValue=0.1 --0.1
  53. end
  54.  
  55. function clear()
  56.     term.clear()
  57.     term.setCursorPos(1,1)
  58.     term.setBackgroundColour(colours.black)
  59.     term.setTextColour(colours.white)
  60. end
  61.  
  62. function drawGrass()
  63.     term.setCursorPos(1,h)
  64.     term.setBackgroundColour(colours.green)
  65.     write(string.rep(" ",w))
  66.     term.setCursorPos(1,1)
  67.     term.setBackgroundColour(colours.black)
  68. end
  69.  
  70. function drawShip(yPos)
  71.     if shipFacingRight==true then
  72.         term.setCursorPos(24,yPos)
  73.         term.setBackgroundColour(colours.orange)
  74.         print(" ")
  75.         term.setCursorPos(25,yPos)
  76.         term.setBackgroundColour(colours.white)
  77.         print("  ")
  78.     else
  79.         term.setCursorPos(26,yPos)
  80.         term.setBackgroundColour(colours.orange)
  81.         print(" ")
  82.         term.setCursorPos(24,yPos)
  83.         term.setBackgroundColour(colours.white)
  84.         print("  ")
  85.     end
  86.     term.setBackgroundColour(colours.black)
  87. end
  88.  
  89. function delShip(yPos)
  90.     term.setCursorPos(24,yPos)
  91.     term.setBackgroundColour(colours.black)
  92.     print("   ")
  93. end
  94.  
  95. function drawAliens()
  96.     term.setBackgroundColour(colours.cyan)
  97.     if alien1==true then
  98.         term.setCursorPos(alien1x,alien1y)
  99.         write("   ")
  100.     end
  101.     term.setBackgroundColour(colours.black)
  102. end
  103.  
  104. function delAliens()
  105.     term.setBackgroundColour(colours.black)
  106.     if alien1==true then
  107.         term.setCursorPos(alien1x,alien1y)
  108.         write("   ")
  109.     end
  110. end
  111.  
  112. function drawHumans()
  113.     term.setBackgroundColour(colours.pink)
  114.     if human1==true then
  115.         term.setCursorPos(human1x,human1y)
  116.         write(" ")
  117.     end
  118.     if human2==true then
  119.         term.setCursorPos(human2x,human2y)
  120.         write(" ")
  121.     end
  122.     if human3==true then
  123.         term.setCursorPos(human3x,human3y)
  124.         write(" ")
  125.     end
  126.     if human4==true then
  127.         term.setCursorPos(human4x,human4y)
  128.         write(" ")
  129.     end
  130.     if human5==true then
  131.         term.setCursorPos(human5x,human5y)
  132.         write(" ")
  133.     end
  134.     if human6==true then
  135.         term.setCursorPos(human6x,human6y)
  136.         write(" ")
  137.     end
  138.     term.setBackgroundColour(colours.green)
  139.     term.setCursorPos(1,19)
  140.     write("   ")
  141.     term.setBackgroundColour(colours.black)
  142. end
  143.  
  144. function delHumans()
  145.     term.setBackgroundColour(colours.black)
  146.     if human1==true then
  147.         term.setCursorPos(human1x,human1y)
  148.         write(" ")
  149.     end
  150.     if human2==true then
  151.         term.setCursorPos(human2x,human2y)
  152.         write(" ")
  153.     end
  154.     if human3==true then
  155.         term.setCursorPos(human3x,human3y)
  156.         write(" ")
  157.     end
  158.     if human4==true then
  159.         term.setCursorPos(human4x,human4y)
  160.         write(" ")
  161.     end
  162.     if human5==true then
  163.         term.setCursorPos(human5x,human5y)
  164.         write(" ")
  165.     end
  166.     if human6==true then
  167.         term.setCursorPos(human6x,human6y)
  168.         write(" ")
  169.     end
  170. end
  171.  
  172. function drawBullet()
  173.     term.setBackgroundColour(colours.yellow)
  174.     term.setCursorPos(bulletXPos,bulletYPos)
  175.     write(" ")
  176.     term.setBackgroundColour(colours.black)
  177. end
  178.  
  179. function delBullet()
  180.     term.setBackgroundColour(colours.black)
  181.     term.setCursorPos(bulletXPos,bulletYPos)
  182.     write(" ")
  183. end
  184.  
  185. function newHighScoreTable()
  186.     name1="Dan" score1=1000
  187.     name2="Fred" score2=800
  188.     name3="Fred" score3=600
  189.     name4="Fred" score4=400
  190.     name5="Fred" score5=200
  191.     local highScoreTable = {{name1, score1}, {name2,score2}, {name3,score3}, {name4,score4}, {name5,score5}}
  192.     local newHighScoreStr = textutils.serialize(highScoreTable)
  193.     --print("new table "..newHighScoreStr)-- debug
  194.     fs.makeDir("protectordata")
  195.     local handle = fs.open("protectordata/pdata","w")
  196.     handle.write(newHighScoreStr)
  197.     handle.close()
  198. end
  199.  
  200. function printCent(xpos,text)
  201.     local ypos = w/2 - (string.len(text)/2)
  202.     term.setCursorPos(ypos,xpos)
  203.     write(text)
  204. end
  205.  
  206. function introHighScoreTable()
  207.     term.clear()
  208.     term.setCursorPos(35,1) write("SPACE WHEN READY")
  209.     if fs.exists("protectordata")==false then
  210.         newHighScoreTable()
  211.     end
  212.     local handle = fs.open("protectordata/pdata","r")
  213.     local dataStr = handle.readAll()
  214.     handle.close()
  215.     --print("dataStr "..dataStr)
  216.     highScoreData = textutils.unserialize(dataStr)
  217.     --print(highScoreData[2])
  218.     name1 = highScoreData[1][1] score1 = highScoreData[1][2]
  219.     name2 = highScoreData[2][1] score2 = highScoreData[2][2]
  220.     name3 = highScoreData[3][1] score3 = highScoreData[3][2]
  221.     name4 = highScoreData[4][1] score4 = highScoreData[4][2]
  222.     name5 = highScoreData[5][1] score5 = highScoreData[5][2]
  223.     term.setTextColour(colours.yellow)
  224.     printCent(5,"HIGH SCORES")
  225.     term.setTextColour(colours.white)
  226.     printCent(7,name1.." "..score1)
  227.     printCent(8,name2.." "..score2)
  228.     printCent(9,name3.." "..score3)
  229.     printCent(10,name4.." "..score4)
  230.     printCent(11,name5.." "..score5)
  231. end
  232.  
  233.  
  234. function printScore()
  235.     term.clear()
  236.     term.setTextColour(colours.yellow)
  237.     printCent(3,"HIGH SCORES")
  238.     term.setTextColour(colours.white)
  239.     printCent(5,name1.." "..score1)
  240.     printCent(6,name2.." "..score2)
  241.     printCent(7,name3.." "..score3)
  242.     printCent(8,name4.." "..score4)
  243.     printCent(9,name5.." "..score5)
  244.     playAgain()
  245. end
  246.  
  247. function rewriteScores()
  248.     if newScore > score1 then
  249.         name5=name4 score5=score4
  250.         name4=name3 score4=score3
  251.         name3=name2 score3=score2
  252.         name2=name1 score2=score1
  253.         name1= newName score1=newScore
  254.     elseif newScore > score2 then
  255.         name5=name4 score5=score4
  256.         name4=name3 score4=score3
  257.         name3=name2 score3=score2
  258.         name2=newName score2=newScore
  259.     elseif newScore > score3 then
  260.         name5=name4 score5=score4
  261.         name4=name3 score4=score3
  262.         name3=newName score3=newScore
  263.     elseif newScore > score4 then
  264.         name5=name4 score5=score4
  265.         name4=newName score4=newScore
  266.     elseif newScore > score5 then
  267.         name5=newName score5=newScore  
  268.     end
  269.     local highScoreTable = {{name1, score1}, {name2,score2}, {name3,score3}, {name4,score4}, {name5,score5}}
  270.     local newHighScoreStr = textutils.serialize(highScoreTable)
  271.     local handle = fs.open("protectordata/pdata","w")
  272.     handle.write(newHighScoreStr)
  273.     handle.close()
  274. end
  275.  
  276. function newHighScoreObtained()
  277.     clear()
  278.     term.setTextColour(colours.yellow)
  279.     printCent(8,"CONGRATULATIONS")
  280.     term.setTextColour(colours.white)
  281.     printCent(10,"You have a new high score!")
  282.     printCent(12,"Enter your name: ")
  283.     printCent(14,"  ")
  284.     local newNameStr = ""
  285.     local newNameStrLen = 0
  286.     while true do
  287.         local event,p1,p2,p3 = os.pullEvent()
  288.         if event=="char" and newNameStrLen < 9 then
  289.             newNameStr=newNameStr..p1
  290.             newNameStrLen=newNameStrLen+1
  291.             printCent(14,newNameStr.." ")
  292.            
  293.         elseif event=="key" and p1 == 14 and newNameStrLen>0 then
  294.             newNameStr=string.sub(newNameStr,1,string.len(newNameStr)-1)
  295.             newNameStrLen=newNameStrLen-1
  296.             term.setCursorPos(1,14)
  297.             write(string.rep(" ",w))
  298.             printCent(14,newNameStr.." ")
  299.         elseif event=="key" and p1== 28 then
  300.             newName = newNameStr
  301.             newScore = score
  302.             rewriteScores()
  303.             printScore()
  304.         end
  305.     end
  306.    
  307.  
  308.    
  309. end
  310.  
  311. function highScore()
  312.     if fs.exists("protectordata")==false then
  313.         newHighScoreTable()
  314.     end
  315.     local handle = fs.open("protectordata/pdata","r")
  316.     local dataStr = handle.readAll()
  317.     handle.close()
  318.     --print("dataStr "..dataStr)
  319.     highScoreData = textutils.unserialize(dataStr)
  320.     --print(highScoreData[2])
  321.     name1 = highScoreData[1][1] score1 = highScoreData[1][2]
  322.     name2 = highScoreData[2][1] score2 = highScoreData[2][2]
  323.     name3 = highScoreData[3][1] score3 = highScoreData[3][2]
  324.     name4 = highScoreData[4][1] score4 = highScoreData[4][2]
  325.     name5 = highScoreData[5][1] score5 = highScoreData[5][2]
  326.     if score>score5 then
  327.         newHighScoreObtained()
  328.     end
  329.     printScore()
  330.    
  331.    
  332. end
  333.  
  334.  
  335.  
  336.  
  337. function gameOver(gameOverMsg)
  338.     clear()
  339.     delShip(shipYPos)
  340.     term.setCursorPos(40,1)
  341.     write("Lives: "..lives.."  ")
  342.     term.setCursorPos(5,1)
  343.     if score<0 then score=0 end
  344.     write("Score: "..score.."  ")
  345.     term.setTextColour(colours.red)
  346.     term.setCursorPos( (w/2)-5 , h/2 -1)
  347.     print("GAME OVER")
  348.     term.setCursorPos( (w/2)-(string.len(gameOverMsg)/2) , (h/2)+1)
  349.     print(gameOverMsg)
  350.     term.setTextColour(colours.white)
  351.     running=false
  352.     sleep(1.5)
  353.     highScore()-- new
  354.     --playAgain
  355. end
  356.  
  357. function playAgain()
  358.     sleep(1)
  359.     printCent(12,"Play again (Y or N)")
  360.    
  361.     while true do
  362.         local event,p1,p2,p3 = os.pullEvent()
  363.         if event=="char" then
  364.             if string.lower(p1)=="y" then
  365.                 runGame()
  366.             elseif string.lower(p1)=="n" then
  367.                 os.shutdown()
  368.             end
  369.         end
  370.     end
  371.    
  372. end
  373.  
  374. function killPlayer()
  375.     moveLeft=false
  376.     moveRight=false
  377.     moveUp=false
  378.     moveDown=false
  379.     delShip(shipYPos)
  380.     lives=lives-1
  381.     if lives==0 then
  382.         gameOver("OUT OF LIVES")
  383.     end
  384.     killedState=true
  385.     --killedStr="true"
  386. end
  387.  
  388. function left()
  389.     delHumans()
  390.     delAliens()
  391.     human1x=human1x+1
  392.     human2x=human2x+1
  393.     human3x=human3x+1
  394.     human4x=human4x+1
  395.     human5x=human5x+1
  396.     human6x=human6x+1
  397.     alien1x=alien1x+1
  398.     if human1x>100 then human1x=0 end
  399.     if human2x>100 then human2x=0 end
  400.     if human3x>100 then human3x=0 end
  401.     if human4x>100 then human4x=0 end
  402.     if human5x>100 then human5x=0 end
  403.     if human6x>100 then human6x=0 end
  404.     if alien1x>100 then alien1x=0 end
  405.     shipFacingRight=false
  406.     checkShipCollision()
  407.     drawShip(shipYPos)
  408.     drawHumans()
  409.     drawAliens()
  410.     drawBorder()
  411.     moveRight=false
  412. end
  413.  
  414. function right()
  415.     delHumans()
  416.     delAliens()
  417.     human1x=human1x-1
  418.     human2x=human2x-1
  419.     human3x=human3x-1
  420.     human4x=human4x-1
  421.     human5x=human5x-1
  422.     human6x=human6x-1
  423.     alien1x=alien1x-1
  424.     if human1x<1 then human1x=100 end
  425.     if human2x<1 then human2x=100 end
  426.     if human3x<1 then human3x=100 end
  427.     if human4x<1 then human4x=100 end
  428.     if human5x<1 then human5x=100 end
  429.     if human6x<1 then human6x=100 end
  430.     if alien1x<1 then alien1x=100 end
  431.     shipFacingRight=true
  432.     checkShipCollision()
  433.     drawShip(shipYPos)
  434.     drawHumans()
  435.     drawAliens()
  436.     drawBorder()
  437.     moveLeft=false
  438. end
  439.  
  440. function up()
  441.     if shipYPos > 2 then
  442.         delShip(shipYPos)
  443.         shipYPos=shipYPos-1
  444.         checkShipCollision()
  445.         drawShip(shipYPos)
  446.     end
  447.     moveUp=false
  448.     moveDown=false
  449. end
  450.  
  451. function down()
  452.     if shipYPos<17 then
  453.         delShip(shipYPos)
  454.         shipYPos=shipYPos+1
  455.         checkShipCollision()
  456.         drawShip(shipYPos)
  457.     end
  458.     moveDown=false
  459.     moveUp=false
  460. end
  461.  
  462. function checkShipCollision()
  463.     if killedState==false then
  464.         if shipYPos == alien1y and alien1== true then
  465.             if alien1x >= 22 and alien1x <= 26 then
  466.                 alien1Hit()
  467.                 killPlayer()
  468.             end
  469.         elseif human1==true and human1y==shipYPos then
  470.             if human1x >=24 and human1x <= 26 then
  471.                 human1=false
  472.                 humanHitRoutine()
  473.             end
  474.         elseif human2==true and human2y==shipYPos then
  475.             if human2x >=24 and human2x <= 26 then
  476.                 human2=false
  477.                 humanHitRoutine()
  478.             end
  479.         elseif human3==true and human3y==shipYPos then
  480.             if human3x >=24 and human3x <= 26 then
  481.                 human3=false
  482.                 humanHitRoutine()
  483.             end
  484.         elseif human4==true and human4y==shipYPos then
  485.             if human4x >=24 and human4x <= 26 then
  486.                 human4=false
  487.                 humanHitRoutine()
  488.             end
  489.         elseif human5==true and human5y==shipYPos then
  490.             if human5x >=24 and human5x <= 26 then
  491.                 human5=false
  492.                 humanHitRoutine()
  493.             end
  494.         elseif human6==true and human6y==shipYPos then
  495.             if human6x >=24 and human6x <= 26 then
  496.                 human6=false
  497.                 humanHitRoutine()
  498.             end
  499.         end
  500.     end
  501. end
  502.  
  503. function alienGen()
  504.     if alien1==false then
  505.         local alienChance= math.random(1,10)
  506.         if alienChance==1 then
  507.             if human1==true then
  508.                 alien1 = true
  509.                 alien1y = 2
  510.                 alien1x = human1x - 1
  511.             end
  512.         elseif alienChance == 2 then
  513.             if human2==true then
  514.                 alien1 = true
  515.                 alien1y=2
  516.                 alien1x = human2x-1
  517.             end
  518.         elseif alienChance == 3 then
  519.             if human3==true then
  520.                 alien1 = true
  521.                 alien1y=2
  522.                 alien1x = human3x-1
  523.             end
  524.         elseif alienChance == 4 then
  525.             if human4==true then
  526.                 alien1 = true
  527.                 alien1y=2
  528.                 alien1x = human4x-1
  529.             end
  530.         elseif alienChance == 5 then
  531.             if human5==true then
  532.                 alien1 = true
  533.                 alien1y=2
  534.                 alien1x = human5x-1
  535.             end
  536.         elseif alienChance == 6 then
  537.             if human6==true then
  538.                 alien1 = true
  539.                 alien1y=2
  540.                 alien1x = human6x-1
  541.             end
  542.         end
  543.     end
  544. end
  545.  
  546. function alienDown()
  547.     if alien1==true and alien1Abduct==false and alien1y<19 then
  548.         alien1Step=alien1Step+stepValue
  549.         alien1y=math.floor(alien1Step)
  550.         if alien1==true and alien1Abduct==false and alien1y==17 then
  551.             alien1Abduct=true
  552.             alien1Carry=true
  553.             alien1Step=17
  554.         end
  555.     end
  556. end
  557.  
  558. function alienRoutine()
  559.     alien1=false
  560.     alien1Step=2
  561.     if alien1Carry==true then
  562.         score= score -50
  563.         humansLeft=humansLeft-1
  564.     end
  565.     alien1Abduct=false
  566.     alien1Carry=false
  567.     if humansLeft==0 then
  568.         gameOver("NO HUMANS LEFT")
  569.     end
  570.    
  571. end
  572.  
  573. function alienUp()
  574.     if alien1==true and alien1Abduct==true then
  575.         if alien1x+1 == human1x then
  576.             human1Abducted=true
  577.             alien1Step=alien1Step-stepValue
  578.             alien1y=math.floor(alien1Step)
  579.             human1y=math.floor(alien1Step)+1
  580.             human1x=alien1x+1
  581.             if human1y<=2 then
  582.                 alienRoutine()
  583.                 human1=false
  584.             end
  585.         elseif alien1x+1 == human2x then
  586.             human2Abducted=true
  587.             alien1Step=alien1Step-stepValue
  588.             alien1y=math.floor(alien1Step)
  589.             human2y=math.floor(alien1Step)+1
  590.             human2x=alien1x+1
  591.             if human2y<=2 then
  592.                 alienRoutine()
  593.                 human2=false
  594.             end
  595.         elseif alien1x+1 == human3x then
  596.             human3Abducted=true
  597.             alien1Step=alien1Step-stepValue
  598.             alien1y=math.floor(alien1Step)
  599.             human3y=math.floor(alien1Step)+1
  600.             human3x=alien1x+1
  601.             if human3y<=2 then
  602.                 alienRoutine()
  603.                 human3=false
  604.             end
  605.         elseif alien1x+1 == human4x then
  606.             human4Abducted=true
  607.             alien1Step=alien1Step-stepValue
  608.             alien1y=math.floor(alien1Step)
  609.             human4y=math.floor(alien1Step)+1
  610.             human4x=alien1x+1
  611.             if human4y<=2 then
  612.                 alienRoutine()
  613.                 human4=false
  614.             end
  615.         elseif alien1x+1 == human5x then
  616.             human5Abducted=true
  617.             alien1Step=alien1Step-stepValue
  618.             alien1y=math.floor(alien1Step)
  619.             human5y=math.floor(alien1Step)+1
  620.             human5x=alien1x+1
  621.             if human5y<=2 then
  622.                 alienRoutine()
  623.                 human5=false
  624.             end
  625.         elseif alien1x+1 == human6x then
  626.             human6Abducted=true
  627.             alien1Step=alien1Step-stepValue
  628.             alien1y=math.floor(alien1Step)
  629.             human6y=math.floor(alien1Step)+1
  630.             human6x=alien1x+1
  631.             if human6y<=2 then
  632.                 alienRoutine()
  633.                 human6=false
  634.             end
  635.         end
  636.     end
  637.     if alien1==false then alienGen() end
  638. end
  639.  
  640. function keyPress()  -- 200 UP, 208 DOWN, 203 LEFT,  205 RIGHT,  57 SPACE, 16 Q
  641.         if pressedKey==200 or pressedKey == 17 then -- up
  642.             moveUp=true
  643.             moveDown=false
  644.         elseif pressedKey==208 or pressedKey == 31 then -- DOWN
  645.             moveDown=true
  646.             moveUp=false
  647.         elseif pressedKey==203 or pressedKey == 30 then -- left
  648.             moveLeft=true  
  649.             moveRight=false
  650.         elseif pressedKey==205 or pressedKey == 32 then -- right
  651.             moveRight=true
  652.             moveLeft=false
  653.         elseif pressedKey==57 then -- space
  654.                 if bulletState==false then
  655.                     bulletYPos=shipYPos
  656.                     bulletState=true
  657.                     if shipFacingRight==true then
  658.                         bulletXPos=shipXPos+3
  659.                         bulletGoingRight=true
  660.                     else
  661.                         bulletXPos=shipXPos-1
  662.                         bulletGoingRight=false
  663.                     end
  664.                 end
  665.         elseif pressedKey==25 then -- q  (use 25 if p for quit)
  666.                 gameOver("YOU QUIT")
  667.         end
  668.        
  669.         --term.setCursorPos(30,1)
  670.         --write(pressedKey.."  ")
  671. end
  672.  
  673. function removeBullet()
  674.     if bulletGoingRight==true then
  675.         bulletXPos = 60
  676.     else
  677.         bulletXPos = -10
  678.     end
  679. end
  680.  
  681. function alien1Hit()
  682.     delAliens()
  683.     alien1=false
  684.     score=score+20
  685.     alien1Step=2
  686.     alien1Abduct=false
  687.     removeBullet()
  688.     drawAliens()
  689. end
  690.  
  691. function humanHitRoutine()
  692.     score=score-50
  693.     humansLeft=humansLeft-1
  694.     if humansLeft==0 then
  695.         gameOver("NO HUMANS LEFT")
  696.     end
  697.     if alien1Carry==true then alien1Carry=false end
  698. end
  699.  
  700.  
  701. function checkBulletCollision()
  702.     if alien1 == true and bulletYPos == alien1y then
  703.         if bulletXPos >= alien1x  and bulletXPos <= alien1x + 3 then
  704.             alien1Hit()
  705.         end
  706.     end
  707.     if human1 == true and bulletYPos == human1y and bulletXPos == human1x then human1=false humanHitRoutine()  end
  708.     if human2 == true and bulletYPos == human2y and bulletXPos == human2x then human2=false humanHitRoutine()  end
  709.     if human3 == true and bulletYPos == human3y and bulletXPos == human3x then human3=false humanHitRoutine()  end
  710.     if human4 == true and bulletYPos == human4y and bulletXPos == human4x then human4=false humanHitRoutine()  end
  711.     if human5 == true and bulletYPos == human5y and bulletXPos == human5x then human5=false humanHitRoutine()  end
  712.     if human6 == true and bulletYPos == human6y and bulletXPos == human6x then human6=false humanHitRoutine()  end
  713. end
  714.  
  715. function drawBorder()
  716.     term.setBackgroundColour(colours.black)
  717.     for i=1,h-2 do
  718.         term.setCursorPos(1,i+1)
  719.         write(" ")
  720.         term.setCursorPos(w,i+1)
  721.         write(" ")
  722.     end
  723. end
  724.  
  725. function dropHumans()
  726.     if alien1Abduct==false then
  727.         if human1y<18 then human1y = human1y+1 end
  728.         if human2y<18 then human2y = human2y+1 end
  729.         if human3y<18 then human3y = human3y+1 end
  730.         if human4y<18 then human4y = human4y+1 end
  731.         if human5y<18 then human5y = human5y+1 end
  732.         if human6y<18 then human6y = human6y+1 end
  733.     end
  734. end
  735.  
  736.  
  737. function gameControl()
  738.  
  739.     gameTimer=os.startTimer(0.1)
  740.        
  741.     while running do
  742.         local event,p1,p2,p3 = os.pullEvent()
  743.         if score<0 then score=0 end
  744.         term.setCursorPos(1,1)
  745.         term.setBackgroundColour(colours.yellow)
  746.         write(string.rep(" ",w))
  747.    
  748.    
  749.         term.setTextColour(colours.red)
  750.         term.setCursorPos(5,1)
  751.         write("Score: "..score.."  ")
  752.         term.setCursorPos(20,1)
  753.         write("Humans Left: "..humansLeft.."  ")
  754.         term.setCursorPos(40,1)
  755.         write("Lives: "..lives.."  ")
  756.    
  757.         term.setBackgroundColour(colours.black)
  758.         term.setTextColour(colours.white)
  759.        
  760.         local newStepValue = (score+0.1)/1000
  761.         if newStepValue > stepValue then stepValue= newStepValue end
  762.         if stepValue>0.4 then stepValue=0.4 end
  763.        
  764.        
  765.         --[[DEBUG
  766.         term.setCursorPos(2,2)
  767.         write("human1x "..human1x.."  ")
  768.         term.setCursorPos(2,3)
  769.         write("human2x "..human2x.."  ")
  770.         term.setCursorPos(2,4)
  771.         write("human3x "..human3x.."  ")
  772.         term.setCursorPos(2,5)
  773.         write("human4x "..human4x.."  ")
  774.         term.setCursorPos(2,6)
  775.         write("human5x "..human5x.."  ")
  776.         term.setCursorPos(2,7)
  777.         write("human6x "..human6x.."  ")
  778.         ]]--
  779.        
  780.                
  781.         if event=="timer" and gameTimer == p1 then
  782.             if killedState==true then
  783.                 delShip(shipYPos)
  784.                 delHumans()
  785.                 dropHumans()
  786.                 drawHumans()
  787.                 killedDelay = killedDelay + 1
  788.                 if killedDelay>20 then
  789.                     shipYPos = 10
  790.                     killedState = false
  791.                     term.setBackgroundColour(colours.black)
  792.                     for i = 2, h-2 do
  793.                         term.setCursorPos(1,i)
  794.                         write(string.rep(" ",w))
  795.                     end
  796.                     drawGrass()
  797.                     if shipFacingRight==true then
  798.                         moveRight=true
  799.                         moveLeft=false
  800.                     else
  801.                         moveLeft=true
  802.                         moveRight=false
  803.                     end
  804.                     killedDelay=0
  805.                 end
  806.             else
  807.                
  808.                 --alienGen()
  809.                 drawShip(shipYPos)
  810.                 delAliens()
  811.                
  812.                 delHumans()
  813.                 dropHumans()
  814.                 alienDown()
  815.                 alienUp()
  816.                 drawAliens()
  817.                 drawHumans()
  818.                 drawBorder()
  819.             end
  820.            
  821.             if bulletState==true then
  822.                 if bulletGoingRight==true then
  823.                     delBullet()
  824.                     bulletXPos=bulletXPos+1
  825.                     checkBulletCollision()
  826.                     if bulletXPos>45 then
  827.                         bulletState=false
  828.                     else
  829.                         if killedState==false then drawBullet() end
  830.                     end
  831.                 else
  832.                     delBullet()
  833.                     bulletXPos=bulletXPos-1
  834.                     checkBulletCollision()
  835.                     if bulletXPos<6 then
  836.                         bulletState=false
  837.                     else
  838.                         if killedState==false then drawBullet() end
  839.                     end
  840.                 end
  841.             end
  842.            
  843.             if moveLeft==true then
  844.                 left()
  845.             end
  846.             if moveRight==true then
  847.                 right()
  848.             end
  849.             if moveUp==true then
  850.                 up()
  851.             end
  852.             if moveDown==true then
  853.                 down()
  854.             end
  855.            
  856.             gameTimer=os.startTimer(0.1)
  857.        
  858.         elseif event=="key" and killedState==false then
  859.             pressedKey=p1
  860.             keyPress()
  861.         end
  862.        
  863.     end
  864.    
  865. end
  866.  
  867. function runGame()
  868.     initVariables()
  869.     clear()
  870.     drawGrass()
  871.     drawHumans()
  872.     alienGen()
  873.     gameControl()
  874. end
  875.  
  876.  
  877. function pix(xCo,yCo,text,col)
  878.     if col== nil then term.setBackgroundColour(colours.black)
  879.     elseif col =="white" then term.setBackgroundColour(colours.white)
  880.     elseif col =="green" then term.setBackgroundColour(colours.green)
  881.     elseif col =="pink" then term.setBackgroundColour(colours.pink)
  882.     elseif col =="orange" then term.setBackgroundColour(colours.orange)
  883.     elseif col =="cyan" then term.setBackgroundColour(colours.cyan)
  884.     elseif col =="yellow" then term.setBackgroundColour(colours.yellow)
  885.     end
  886.     term.setCursorPos(xCo,yCo)
  887.     write(text)
  888. end
  889.  
  890. function drawHumanPix()
  891.     drawGrass()
  892.     pix(23,humanPixY," ","pink")
  893.     pix(27,humanPixY," ","pink")
  894.     pix(24,humanPixY-1," ","pink")
  895.     pix(26,humanPixY-1," ","pink")
  896.     pix(25,humanPixY-2," ","pink")
  897.     pix(23,humanPixY-3,"     ","pink")
  898.     pix(25,humanPixY-4," ","pink")
  899.     pix(24,humanPixY-5,"   ","pink")
  900.     pix(24,humanPixY-6,"   ","pink")
  901. end
  902.  
  903. function delHumanPix()
  904.     pix(23,humanPixY," ")
  905.     pix(27,humanPixY," ")
  906.     pix(24,humanPixY-1," ")
  907.     pix(26,humanPixY-1," ")
  908.     pix(25,humanPixY-2," ")
  909.     pix(23,humanPixY-3,"     ")
  910.     pix(25,humanPixY-4," ")
  911.     pix(24,humanPixY-5,"   ")
  912.     pix(24,humanPixY-6,"   ")
  913. end
  914.  
  915. function drawAlienPix()
  916.     pix(19,alienPixY,"             ","cyan")
  917.     pix(17,alienPixY+1,"                 ","cyan")
  918.     pix(19,alienPixY+2,"             ","cyan")
  919. end
  920.  
  921. function delAlienPix()
  922.     pix(19,alienPixY,"             ")
  923.     pix(17,alienPixY+1,"                 ")
  924.     pix(19,alienPixY+2,"             ")
  925. end
  926.  
  927. function drawShipPix()
  928.     pix(shipPixX+3,3,"  ","white")
  929.     pix(shipPixX+3,4,"   ","white")
  930.     pix(shipPixX+3,5,"           ","white")
  931.     pix(shipPixX+3,6,"               ","white")
  932.     pix(shipPixX+3,7,"                 ","white")
  933.     pix(shipPixX+2,5," ","orange")
  934.     pix(shipPixX+2,6," ","yellow")
  935.     pix(shipPixX+2,7," ","orange")
  936.     pix(shipPixX,6,"  ","orange")
  937.     pix(shipPixX+14,5,"  ","cyan")
  938. end
  939.  
  940. function delShipPix()
  941.     pix(shipPixX+3,3,"  ")
  942.     pix(shipPixX+3,4,"   ")
  943.     pix(shipPixX+3,5,"           ")
  944.     pix(shipPixX+3,6,"               ")
  945.     pix(shipPixX+3,7,"                 ")
  946.     pix(shipPixX+2,5," ")
  947.     pix(shipPixX+2,6," ")
  948.     pix(shipPixX+2,7," ")
  949.     pix(shipPixX,6,"  ")
  950.     pix(shipPixX+14,5,"  ")
  951. end
  952.  
  953. function line1()
  954.     pix(8,4,"   ","white")
  955.     pix(12,4,"  ","white")
  956.     pix(16,4,"   ","white")
  957.     pix(20,4,"   ","white")
  958.     pix(24,4,"   ","white")
  959.     pix(28,4,"   ","white")
  960.     pix(32,4,"   ","white")
  961.     pix(36,4,"   ","white")
  962.     pix(40,4,"  ","white")
  963. end
  964.  
  965. function line2()
  966.     pix(8,5," ","white")
  967.     pix(10,5," ","white")
  968.     pix(12,5," ","white")
  969.     pix(14,5," ","white")
  970.     pix(16,5," ","white")
  971.     pix(18,5," ","white")
  972.     pix(21,5," ","white")
  973.     pix(24,5," ","white")
  974.     pix(28,5," ","white")
  975.     pix(33,5," ","white")
  976.     pix(36,5," ","white")
  977.     pix(38,5," ","white")
  978.     pix(40,5," ","white")
  979.     pix(42,5," ","white")
  980.    
  981. end
  982.  
  983. function line3()
  984.     pix(8,6,"   ","white")
  985.     pix(12,6,"   ","white")
  986.     pix(16,6," ","white")
  987.     pix(18,6," ","white")
  988.     pix(21,6," ","white")
  989.     pix(24,6,"   ","white")
  990.     pix(28,6," ","white")
  991.     pix(33,6," ","white")
  992.     pix(36,6," ","white")
  993.     pix(38,6," ","white")
  994.     pix(40,6,"   ","white")
  995. end
  996.  
  997. function line4()
  998.     pix(8,7," ","white")
  999.     pix(12,7,"  ","white")
  1000.     pix(16,7," ","white")
  1001.     pix(18,7," ","white")
  1002.     pix(21,7," ","white")
  1003.     pix(24,7," ","white")
  1004.     pix(28,7," ","white")
  1005.     pix(33,7," ","white")
  1006.     pix(36,7," ","white")
  1007.     pix(38,7," ","white")
  1008.     pix(40,7,"  ","white")
  1009. end
  1010.  
  1011. function line5()
  1012.     pix(8,8," ","white")
  1013.     pix(12,8," ","white")
  1014.     pix(14,8," ","white")
  1015.     pix(16,8,"   ","white")
  1016.     pix(21,8," ","white")
  1017.     pix(24,8,"   ","white")
  1018.     pix(28,8,"   ","white")
  1019.     pix(33,8," ","white")
  1020.     pix(36,8,"   ","white")
  1021.     pix(40,8," ","white")
  1022.     pix(42,8," ","white")
  1023. end
  1024.  
  1025.  
  1026. function startScreen()
  1027.  
  1028.     clear()
  1029.     term.setBackgroundColour(colours.green)
  1030.     term.setCursorPos(1,h)
  1031.     write(string.rep(" ",w))
  1032.     local screenStage=0
  1033.    
  1034.     screenTimer=os.startTimer(0.1)
  1035.     while true do
  1036.         local event,p1,p2,p3=os.pullEvent()
  1037.         if event=="key"and p1==57 then
  1038.             term.setBackgroundColour(colours.black)
  1039.             clear()
  1040.             runGame()
  1041.         elseif event=="timer" and screenTimer == p1 then
  1042.        
  1043.             --term.setCursorPos(1,1) write("screenStage: "..screenStage.."  ")
  1044.            
  1045.             term.setBackgroundColour(colours.black)
  1046.             term.setCursorPos(35,1) write("SPACE WHEN READY")
  1047.            
  1048.             if screenStage>0 and screenStage<0.5 then
  1049.                 humanPixY = 18
  1050.                 drawHumanPix()
  1051.             elseif screenStage>2 and screenStage<2.9 then
  1052.                 alienPixY = -2
  1053.                 drawAlienPix()
  1054.             elseif screenStage>3 and screenStage<3.9 then
  1055.                 alienPixY = -2
  1056.                 delAlienPix()
  1057.                 alienPixY = -1
  1058.                 drawAlienPix()
  1059.             elseif screenStage>4 and screenStage<4.9 then
  1060.                 alienPixY = -1
  1061.                 delAlienPix()
  1062.                 alienPixY = 0
  1063.                 drawAlienPix()  
  1064.             elseif screenStage>5 and screenStage<5.9 then
  1065.                 alienPixY = 0
  1066.                 delAlienPix()
  1067.                 alienPixY = 1
  1068.                 drawAlienPix()
  1069.             elseif screenStage>6 and screenStage<6.9 then
  1070.                 alienPixY = 1
  1071.                 delAlienPix()
  1072.                 alienPixY = 2
  1073.                 drawAlienPix()
  1074.             elseif screenStage>7 and screenStage<7.9 then
  1075.                 alienPixY = 2
  1076.                 delAlienPix()
  1077.                 alienPixY = 3
  1078.                 drawAlienPix()
  1079.             elseif screenStage>8 and screenStage<8.9 then
  1080.                 alienPixY = 3
  1081.                 delAlienPix()
  1082.                 alienPixY = 4
  1083.                 drawAlienPix() 
  1084.             elseif screenStage>8 and screenStage<9.9 then
  1085.                 alienPixY = 4
  1086.                 delAlienPix()
  1087.                 alienPixY = 5
  1088.                 drawAlienPix()
  1089.             elseif screenStage>10 and screenStage<10.4 then
  1090.                 pix(25,8," ","yellow")
  1091.                 pix(25,9," ","yellow")
  1092.                 pix(25,10," ","yellow")
  1093.                 pix(25,11," ","yellow")
  1094.                 pix(25,17," ","yellow")
  1095.                 pix(25,18," ","yellow")
  1096.             elseif screenStage>10.4 and screenStage<10.6 then
  1097.                 pix(25,8," ","yellow")
  1098.                 pix(25,9," ","yellow")
  1099.                 pix(25,10," ","yellow")
  1100.                 pix(24,11,"   ","yellow")
  1101.                 pix(24,12,"   ","yellow")
  1102.                 pix(24,13,"   ","yellow")
  1103.                 pix(24,14,"   ","yellow")
  1104.                 pix(23,15,"     ","yellow")
  1105.                 pix(23,16,"     ","yellow")
  1106.                 pix(23,17,"     ","yellow")
  1107.                 pix(23,18,"     ","yellow")
  1108.                 humanPixY = 18
  1109.                 drawHumanPix()
  1110.             elseif screenStage>10.6 and screenStage<10.8 then
  1111.                 pix(25,8," ","yellow")
  1112.                 pix(25,9," ","yellow")
  1113.                 pix(24,10,"   ","yellow")
  1114.                 pix(24,11,"   ","yellow")
  1115.                 pix(24,12,"   ","yellow")
  1116.                 pix(23,13,"     ","yellow")
  1117.                 pix(23,14,"     ","yellow")
  1118.                 pix(23,15,"     ","yellow")
  1119.                 pix(22,16,"       ","yellow")
  1120.                 pix(22,17,"       ","yellow")
  1121.                 pix(22,18,"       ","yellow")
  1122.                 humanPixY = 18
  1123.                 drawHumanPix() 
  1124.             elseif screenStage>10.8 and screenStage<11 then
  1125.                 pix(25,8," ","yellow")
  1126.                 pix(24,9,"   ","yellow")
  1127.                 pix(24,10,"   ","yellow")
  1128.                 pix(23,11,"     ","yellow")
  1129.                 pix(23,12,"     ","yellow")
  1130.                 pix(22,13,"       ","yellow")
  1131.                 pix(22,14,"       ","yellow")
  1132.                 pix(21,15,"         ","yellow")
  1133.                 pix(21,16,"         ","yellow")
  1134.                 pix(20,17,"           ","yellow")
  1135.                 pix(20,18,"           ","yellow")
  1136.                 humanPixY = 18
  1137.                 drawHumanPix()     
  1138.             elseif screenStage>11.9 and screenStage<12 then
  1139.                 pix(1,6,"  ","yellow")
  1140.             elseif screenStage>12 and screenStage<12.1 then
  1141.                 pix(1,6,"  ")
  1142.                 pix(3,6,"  ","yellow")
  1143.             elseif screenStage>12.1 and screenStage<12.2 then
  1144.                 pix(3,6,"  ")
  1145.                 pix(5,6,"  ","yellow") 
  1146.             elseif screenStage>12.2 and screenStage<12.3 then
  1147.                 pix(5,6,"  ")
  1148.                 pix(7,6,"  ","yellow")
  1149.             elseif screenStage>12.3 and screenStage<12.4 then
  1150.                 pix(7,6,"  ")
  1151.                 pix(9,6,"  ","yellow")
  1152.             elseif screenStage>12.4 and screenStage<12.5 then
  1153.                 pix(9,6,"  ")
  1154.                 pix(11,6,"  ","yellow")
  1155.             elseif screenStage>12.5 and screenStage<12.6 then
  1156.                 pix(11,6,"  ")
  1157.                 pix(13,6,"  ","yellow")
  1158.             elseif screenStage>12.6 and screenStage<12.7 then
  1159.                 pix(13,6,"  ")
  1160.                 pix(15,6,"  ","yellow")
  1161.             elseif screenStage>12.7 and screenStage<12.8 then
  1162.                 term.setBackgroundColour(colours.black)
  1163.                 for i= 5, h-1 do
  1164.                     term.setCursorPos(15,i)
  1165.                     write("                    ")
  1166.                 end
  1167.                 humanPixY=18
  1168.                 drawHumanPix()
  1169.             elseif screenStage>13 and screenStage<13.1 then
  1170.                 shipPixX= -16
  1171.                 drawShipPix()
  1172.             elseif screenStage>13 and screenStage<13.1 then
  1173.                 delShipPix()
  1174.                 shipPixX= -15
  1175.                 drawShipPix()  
  1176.             elseif screenStage>13.1 and screenStage<13.2 then  
  1177.                 delShipPix()
  1178.                 shipPixX= -12
  1179.                 drawShipPix()  
  1180.             elseif screenStage>13.2 and screenStage<13.3 then  
  1181.                 delShipPix()
  1182.                 shipPixX= -9
  1183.                 drawShipPix()
  1184.             elseif screenStage>13.2 and screenStage<13.3 then  
  1185.                 delShipPix()
  1186.                 shipPixX= -6
  1187.                 drawShipPix()
  1188.             elseif screenStage>13.3 and screenStage<13.4 then  
  1189.                 delShipPix()
  1190.                 shipPixX= -3
  1191.                 drawShipPix()
  1192.             elseif screenStage>13.4 and screenStage<13.5 then  
  1193.                 delShipPix()
  1194.                 shipPixX= 0
  1195.                 drawShipPix()
  1196.             elseif screenStage>13.6 and screenStage<13.7 then  
  1197.                 delShipPix()
  1198.                 shipPixX= 3
  1199.                 drawShipPix()
  1200.             elseif screenStage>13.8 and screenStage<13.9 then  
  1201.                 delShipPix()
  1202.                 shipPixX= 6
  1203.                 drawShipPix()
  1204.             elseif screenStage>13.9 and screenStage<14 then
  1205.                 delShipPix()
  1206.                 shipPixX= 9
  1207.                 drawShipPix()
  1208.             elseif screenStage>14.1 and screenStage<14.2 then  
  1209.                 delShipPix()
  1210.                 shipPixX= 12
  1211.                 drawShipPix()
  1212.             elseif screenStage>14.2 and screenStage<14.3 then  
  1213.                 delShipPix()
  1214.                 shipPixX= 15
  1215.                 drawShipPix()
  1216.             elseif screenStage>14.3 and screenStage<14.4 then  
  1217.                 delShipPix()
  1218.                 shipPixX= 18
  1219.                 drawShipPix()
  1220.             elseif screenStage>14.4 and screenStage<14.5 then  
  1221.                 delShipPix()
  1222.                 shipPixX= 21
  1223.                 drawShipPix()
  1224.             elseif screenStage>14.5 and screenStage<14.6 then  
  1225.                 delShipPix()
  1226.                 shipPixX= 24
  1227.                 drawShipPix()
  1228.             elseif screenStage>14.6 and screenStage<14.7 then  
  1229.                 delShipPix()
  1230.                 shipPixX= 27
  1231.                 drawShipPix()
  1232.             elseif screenStage>14.7 and screenStage<14.8 then  
  1233.                 delShipPix()
  1234.                 shipPixX= 30
  1235.                 drawShipPix()
  1236.             elseif screenStage>14.8 and screenStage<14.9 then  
  1237.                 delShipPix()
  1238.                 shipPixX= 33
  1239.                 drawShipPix()
  1240.             elseif screenStage>14.9 and screenStage<15 then
  1241.                 delShipPix()
  1242.                 shipPixX= 36
  1243.                 drawShipPix()
  1244.             elseif screenStage>15 and screenStage<15.1 then
  1245.                 delShipPix()
  1246.                 shipPixX= 39
  1247.                 drawShipPix()
  1248.             elseif screenStage>15.1 and screenStage<15.2 then  
  1249.                 delShipPix()
  1250.                 shipPixX= 41
  1251.                 drawShipPix()
  1252.             elseif screenStage>15.2 and screenStage<15.3 then  
  1253.                 delShipPix()
  1254.                 shipPixX= 44
  1255.                 drawShipPix()
  1256.             elseif screenStage>15.3 and screenStage<15.4 then  
  1257.                 delShipPix()
  1258.                 shipPixX= 47
  1259.                 drawShipPix()
  1260.             elseif screenStage>15.4 and screenStage<15.5 then  
  1261.                 delShipPix()
  1262.                 shipPixX= 50
  1263.                 drawShipPix()
  1264.             elseif screenStage>15.5 and screenStage<15.6 then  
  1265.                 delShipPix()
  1266.             elseif screenStage>16 and screenStage<16.9 then
  1267.                 humanPixY=18
  1268.                 delHumanPix()
  1269.                 line1()
  1270.                 line2()
  1271.                 line3()
  1272.                 line4()
  1273.                 line5()
  1274.             elseif screenStage>17 and screenStage<22 then
  1275.                 term.setCursorPos((w/2)-6,10)
  1276.                 write("by FredTHead")
  1277.                 term.setCursorPos((w/2)-13,12)
  1278.                 write("WSAD or arrow keys to move")
  1279.                 term.setCursorPos((w/2)-6,13)
  1280.                 write("SPACE to fire")
  1281.                 term.setCursorPos((w/2)-4,14)
  1282.                 write("P to quit")
  1283.                 term.setCursorPos((w/2)-8,16)
  1284.                 write("Fire when ready")
  1285.             elseif screenStage>22.1 and screenStage <27 then
  1286.                 introHighScoreTable()
  1287.             elseif screenStage>27 then 
  1288.                 term.setBackgroundColour(colours.black)
  1289.                 for i = 2,h-1 do
  1290.                     term.setCursorPos(1,i)
  1291.                     write(string.rep(" ",w-1))
  1292.                 end
  1293.                 screenStage=0
  1294.             end
  1295.            
  1296.             screenStage=screenStage+0.1
  1297.             screenTimer=os.startTimer(0.025)
  1298.         end
  1299.     end
  1300. end
  1301.  
  1302. w,h =term.getSize()
  1303. if term.isColour() and w==51 then
  1304.     initVariables()
  1305.     startScreen()
  1306. else
  1307.     term.clear()
  1308.     term.setCursorPos(1,1)
  1309.     print("I'm sorry, Protector requires an Advanced Computer to run")
  1310.     print(" ")
  1311. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement