Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- w,h=term.getSize() -- 51 19
- function monSign()
- monitor=peripheral.wrap("top")
- monitor.setTextScale(0.5)
- monW,monH=monitor.getSize() --w 15 h 10
- monitor.clear()
- monitor.setCursorPos(6,4)
- monitor.write("SPACE")
- monitor.setCursorPos(4,6)
- monitor.write("INVADERS!")
- end
- function initVariables()
- keyPress= 0
- curPos=w/2
- bullet=false
- hPos=h-4
- bulletCurPos=0
- gameTimer=0
- alienW = 5
- alienH = 2
- alienLeftStop=2
- alienRightStop=8
- lives=3
- killed= false
- killCounter=0
- noBombs=true
- bombTimer=0
- win=false
- score=0
- alien1 = true
- alien2 = true
- alien3 = true
- alien4 = true
- alien5 = true
- alien6 = true
- bomb1=false
- bomb2=false
- bomb3=false
- alienCount=6
- end
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColour(colours.black)
- term.setTextColour(colours.white)
- end
- function gameController()
- local alienMove=1
- local gameTimer = os.startTimer(0.1)
- repeat
- local event,p1,p2,p3 = os.pullEvent()
- if event=="key" and killed == false then
- keyPress=p1
- keyAction()
- elseif event=="timer" then
- if noBombs==true then
- bombTimer = bombTimer+1
- if bombTimer==30 then
- noBombs=false
- bombTimer=0
- end
- end
- bombs()
- if killed==true then
- killCounter=killCounter+1
- if killCounter==10 then
- killed=false
- curPos=w/2
- killCounter=0
- drawShip(curPos)
- end
- end
- term.setTextColour(colours.white)
- term.setCursorPos(1,1)
- --print("p1: "..p1.." ")
- term.setCursorPos(1,2)
- --print("gameTimer: "..gameTimer.." ")
- term.setCursorPos(1,3)
- --print("hPos: "..hPos.." ")
- if p1== gameTimer then
- gameTimer=os.startTimer(0.1)
- if bullet==true then
- delBullet()
- hPos=hPos-1
- if hPos==alienH or HPos==alienH+1 then
- --checkBulletCollision
- if alien1 == true and bulletCurPos >= alienW and bulletCurPos <= alienW+3 then
- delAlien(alienW,alienH)
- alien1=false
- alienCount=alienCount-1
- score=score+50
- bullet=false
- elseif alien2 == true and bulletCurPos >= alienW+8 and bulletCurPos <= alienW+11 then
- delAlien(alienW+8,alienH)
- alien2=false
- alienCount=alienCount-1
- score=score+50
- bullet=false
- elseif alien3 == true and bulletCurPos >= alienW+16 and bulletCurPos <= alienW+19 then
- delAlien(alienW+16,alienH)
- alien3=false
- alienCount=alienCount-1
- score=score+50
- bullet=false
- elseif alien4 == true and bulletCurPos >= alienW+24 and bulletCurPos <= alienW+27 then
- delAlien(alienW+24,alienH)
- alien4=false
- alienCount=alienCount-1
- score=score+50
- bullet=false
- elseif alien5 == true and bulletCurPos >= alienW+32 and bulletCurPos <= alienW+35 then
- delAlien(alienW+32,alienH)
- alien5=false
- alienCount=alienCount-1
- score=score+50
- bullet=false
- elseif alien6 == true and bulletCurPos >= alienW+40 and bulletCurPos <= alienW+43 then
- delAlien(alienW+40,alienH)
- alien6=false
- alienCount=alienCount-1
- score=score+50
- bullet=false
- end
- if alienCount==0 then
- win=true
- gameOver()
- end
- end
- if hPos<=1 then
- bullet=false
- else
- drawBullet()
- end
- end
- if score <0 then score = 0 end
- term.setCursorPos(w-10,h)
- term.setTextColour(colours.white)
- write("Score: "..score)
- delAlienFormation()
- alienW=alienW+alienMove
- if alien1==false then alienLeftStop = -6 end
- if alien1==false and alien2==false then alienLeftStop = -14 end
- if alien1==false and alien2==false and alien3==false then alienLeftStop = -22 end
- if alien1==false and alien2==false and alien3==false and alien4==false then alienLeftStop = -30 end
- if alien1==false and alien2==false and alien3==false and alien4==false and alien5 ==false then alienLeftStop = -38 end
- if alien6==false then alienRightStop = 16 end
- if alien6==false and alien5==false then alienRightStop = 24 end
- if alien6==false and alien5==false and alien4==false then alienRightStop = 32 end
- if alien6==false and alien5==false and alien4==false and alien3==false then alienRightStop = 40 end
- if alien6==false and alien5==false and alien4==false and alien3==false and alien2 ==false then alienRightStop = 48 end
- if alienW> alienRightStop and alienMove ==1 then alienMove= -1 end -- 8
- if alienW < alienLeftStop and alienMove == -1 then alienMove= 1 end -- 2
- drawAlienFormation()
- end
- end
- until event=="char" and p1==("x")
- end
- function getKey()
- input=read()
- print(input.." = "..keys.getName(input))
- end
- function keyAction()
- if keyPress == 30 or keyPress == 203 then
- --left - a or left arrow
- delShip(curPos)
- if curPos>5 then
- curPos=curPos-1
- end
- drawShip(curPos)
- elseif keyPress == 32 or keyPress == 205 then
- --right - d or right arrow
- delShip(curPos)
- if curPos < w-3 then
- curPos=curPos+1
- end
- drawShip(curPos)
- elseif keyPress == 57 then
- -- fire - space bar
- if bullet==false then
- hPos=h-4
- bullet=true
- bulletCurPos = curPos
- drawBullet()
- end
- elseif keyPress == 16 then
- quit()
- end
- end
- function quit()
- os.shutdown()
- end
- function drawShip(wPos)
- term.setTextColour(colours.white)
- term.setBackgroundColour(colours.white)
- term.setCursorPos(wPos,h-3)
- print("X")
- term.setCursorPos(wPos-1,h-2)
- print("XXX")
- term.setCursorPos(wPos-2,h-1)
- print("XXXXX")
- term.setBackgroundColour(colours.black)
- end
- function delShip(wPos)
- term.setTextColour(colours.red)
- term.setBackgroundColour(colours.black)
- term.setCursorPos(wPos,h-3)
- print(" ")
- term.setCursorPos(wPos-1,h-2)
- print(" ")
- term.setCursorPos(wPos-2,h-1)
- print(" ")
- --term.setBackgroundColour(colours.black)
- end
- function drawBullet()
- if bullet==true then
- term.setCursorPos(bulletCurPos,hPos)
- term.setTextColour(colours.orange)
- term.setBackgroundColour(colours.orange)
- print("I")
- term.setTextColour(colours.white)
- term.setBackgroundColour(colours.black)
- end
- end
- function delBullet()
- term.setCursorPos(bulletCurPos,hPos)
- term.setTextColour(colours.black)
- term.setBackgroundColour(colours.black)
- print(" ")
- term.setTextColour(colours.white)
- term.setBackgroundColour(colours.black)
- end
- function drawAlien(aW,aH,num)
- term.setCursorPos(aW,aH)
- term.setTextColour(colours.lime)
- term.setBackgroundColour(colours.lime)
- print(string.rep(num,3))
- term.setCursorPos(aW,aH+1)
- print(string.rep(num,1))
- term.setCursorPos(aW+2,aH+1)
- print(string.rep(num,1))
- term.setTextColour(colours.white)
- term.setBackgroundColour(colours.black)
- end
- function delAlien(aW,aH)
- term.setCursorPos(aW,aH)
- term.setTextColour(colours.black)
- term.setBackgroundColour(colours.black)
- print(" ")
- term.setCursorPos(aW,aH+1)
- print(" ")
- term.setCursorPos(aW+2,aH+1)
- print(" ")
- term.setTextColour(colours.white)
- term.setBackgroundColour(colours.black)
- end
- function drawAlienFormation()
- if alien1==true then drawAlien(alienW,alienH,1) end
- if alien2==true then drawAlien(alienW+8,alienH,2) end
- if alien3==true then drawAlien(alienW+16,alienH,3) end
- if alien4==true then drawAlien(alienW+24,alienH,4) end
- if alien5==true then drawAlien(alienW+32,alienH,5) end
- if alien6==true then drawAlien(alienW+40,alienH,6) end
- end
- function delAlienFormation()
- if alien1==true then delAlien(alienW,alienH) end
- if alien2==true then delAlien(alienW+8,alienH) end
- if alien3==true then delAlien(alienW+16,alienH) end
- if alien4==true then delAlien(alienW+24,alienH) end
- if alien5==true then delAlien(alienW+32,alienH) end
- if alien6==true then delAlien(alienW+40,alienH) end
- end
- function drawBomb(bombW,bombH)
- term.setCursorPos(bombW+1,bombH+2)
- term.setBackgroundColour(colours.yellow)
- term.setTextColour(colours.yellow)
- print("*")
- term.setBackgroundColour(colours.black)
- term.setTextColour(colours.white)
- end
- function delBomb(bombW,bombH)
- term.setCursorPos(bombW+1,bombH+2)
- term.setBackgroundColour(colours.black)
- term.setTextColour(colours.black)
- print(" ")
- term.setBackgroundColour(colours.black)
- term.setTextColour(colours.white)
- end
- function createBomb()
- chance = math.random(5)
- if chance==1 then
- return true
- else
- return false
- end
- end
- function selectAlien()
- selAlien= math.random(6)
- --term.setCursorPos(1,8)
- --term.setTextColour(colours.white)
- --print("aliens left "..alienCount)
- --term.setCursorPos(1,9)
- if selAlien==1 and alien1== true then bombW=alienW
- elseif selAlien==2 and alien2== true then bombW=alienW+8
- elseif selAlien==3 and alien3== true then bombW=alienW+16
- elseif selAlien==4 and alien4== true then bombW=alienW+24
- elseif selAlien==5 and alien5== true then bombW=alienW+32
- elseif selAlien==6 and alien6== true then bombW=alienW+40
- end
- end
- function bombs()
- if bomb1==false and createBomb() == true and noBombs == false then
- selectAlien()
- drawBomb(bombW,alienH)
- bomb1=true
- bomb1W= bombW
- bomb1H=alienH
- elseif bomb1 ==true then
- delBomb(bomb1W,bomb1H)
- bomb1H = bomb1H+1
- if bomb1H ==14 and bomb1W == curPos then
- bomb1=false
- shipHit()
- elseif bomb1H == 15 and bomb1W >= curPos-2 and bomb1W <= curPos then
- bomb1=false
- shipHit()
- elseif bomb1H == 16 and bomb1W >= curPos-4 and bomb1W <= curPos+1 then
- bomb1=false
- shipHit()
- elseif bomb1H > 16 then
- bomb1=false
- term.setCursorPos(1,10)
- print(" ")
- else
- drawBomb(bomb1W,bomb1H)
- end
- end
- if bomb2==false and createBomb() == true and noBombs == false then
- selectAlien()
- drawBomb(bombW,alienH)
- bomb2=true
- bomb2W= bombW
- bomb2H=alienH
- elseif bomb2 ==true then
- delBomb(bomb2W,bomb2H)
- bomb2H = bomb2H+1
- if bomb2H ==14 and bomb2W == curPos then
- bomb2=false
- shipHit()
- elseif bomb2H == 15 and bomb2W >= curPos-2 and bomb2W <= curPos then
- bomb2=false
- shipHit()
- elseif bomb2H == 16 and bomb2W >= curPos-4 and bomb2W <= curPos+1 then
- bomb2=false
- shipHit()
- elseif bomb2H > 16 then
- bomb2=false
- term.setCursorPos(1,10)
- print(" ")
- else
- drawBomb(bomb2W,bomb2H)
- end
- end
- if bomb3==false and createBomb() == true and noBombs == false then
- selectAlien()
- drawBomb(bombW,alienH)
- bomb3=true
- bomb3W= bombW
- bomb3H=alienH
- elseif bomb3 ==true then
- delBomb(bomb3W,bomb3H)
- bomb3H = bomb3H+1
- if bomb3H ==14 and bomb3W == curPos then
- bomb3=false
- shipHit()
- elseif bomb3H == 15 and bomb3W >= curPos-2 and bomb3W <= curPos then
- bomb2=false
- shipHit()
- elseif bomb3H == 16 and bomb3W >= curPos-4 and bomb3W <= curPos+1 then
- bomb3=false
- shipHit()
- elseif bomb3H > 16 then
- bomb3=false
- term.setCursorPos(1,10)
- print(" ")
- else
- drawBomb(bomb3W,bomb3H)
- end
- end
- end
- function shipHit()
- lives=lives-1
- delShip(curPos)
- killed=true
- noBombs=true
- score=score-10
- if score<0 then score =0 end
- if lives==0 then
- gameOver()
- else
- term.setCursorPos(2,h)
- term.setTextColour(colours.white)
- write("Lives: "..lives)
- term.setCursorPos(w-10,h)
- term.setTextColour(colours.white)
- write("Score: "..score)
- end
- end
- function gameOver()
- clear()
- if win==true then
- term.setCursorPos((w/2)-3, h/2)
- print("YOU WIN")
- win=false
- else
- term.setCursorPos((w/2)-4, h/2)
- print("GAME OVER")
- end
- local scoreStr= tostring(score)
- term.setCursorPos((w/2)-((7+#scoreStr))/2, (h/2)+2)
- print("Score: "..scoreStr)
- sleep(1)
- term.setCursorPos((w/2)-9, (h/2)+5)
- print("Play again (Y or N)")
- while true do
- local event,p1,p2,p3 = os.pullEvent()
- if event=="char" then
- if string.lower(p1)=="y" then
- runGame()
- elseif string.lower(p1)=="n" then
- os.shutdown()
- end
- end
- end
- end
- function runGame()
- clear()
- initVariables()
- drawShip(curPos)
- drawAlienFormation()
- term.setCursorPos(2,h)
- write("Lives: "..lives)
- term.setCursorPos(w-10,h)
- write("Score: "..score)
- gameController()
- end
- function line1(h)
- term.setCursorPos(12,h) write(" ")
- term.setCursorPos(18,h) write(" ")
- term.setCursorPos(24,h) write(" ")
- term.setCursorPos(30,h) write(" ")
- term.setCursorPos(36,h) write(" ")
- end
- function line2(h)
- term.setCursorPos(12,h) write(" ")
- term.setCursorPos(18,h) write(" ")
- term.setCursorPos(21,h) write(" ")
- term.setCursorPos(24,h) write(" ")
- term.setCursorPos(27,h) write(" ")
- term.setCursorPos(30,h) write(" ")
- term.setCursorPos(36,h) write(" ")
- end
- function line3(h)
- term.setCursorPos(12,h) write(" ")
- term.setCursorPos(18,h) write(" ")
- term.setCursorPos(24,h) write(" ")
- term.setCursorPos(30,h) write(" ")
- term.setCursorPos(36,h) write(" ")
- end
- function line4(h)
- term.setCursorPos(15,h) write(" ")
- term.setCursorPos(18,h) write(" ")
- term.setCursorPos(24,h) write(" ")
- term.setCursorPos(27,h) write(" ")
- term.setCursorPos(30,h) write(" ")
- term.setCursorPos(36,h) write(" ")
- end
- function line5(h)
- term.setCursorPos(12,h) write(" ")
- term.setCursorPos(18,h) write(" ")
- term.setCursorPos(24,h) write(" ")
- term.setCursorPos(27,h) write(" ")
- term.setCursorPos(30,h) write(" ")
- term.setCursorPos(36,h) write(" ")
- end
- function line6(h)
- term.setCursorPos(4,h) write(" ")
- term.setCursorPos(9,h) write(" ")
- term.setCursorPos(12,h) write(" ")
- term.setCursorPos(15,h) write(" ")
- term.setCursorPos(18,h) write(" ")
- term.setCursorPos(21,h) write(" ")
- term.setCursorPos(27,h) write(" ")
- term.setCursorPos(33,h) write(" ")
- term.setCursorPos(39,h) write(" ")
- term.setCursorPos(45,h) write(" ")
- end
- function line7(h)
- term.setCursorPos(5,h) write(" ")
- term.setCursorPos(9,h) write(" ")
- term.setCursorPos(12,h) write(" ")
- term.setCursorPos(15,h) write(" ")
- term.setCursorPos(18,h) write(" ")
- term.setCursorPos(21,h) write(" ")
- term.setCursorPos(24,h) write(" ")
- term.setCursorPos(27,h) write(" ")
- term.setCursorPos(30,h) write(" ")
- term.setCursorPos(33,h) write(" ")
- term.setCursorPos(39,h) write(" ")
- term.setCursorPos(42,h) write(" ")
- term.setCursorPos(45,h) write(" ")
- end
- function line8(h)
- term.setCursorPos(5,h) write(" ")
- term.setCursorPos(9,h) write(" ")
- term.setCursorPos(15,h) write(" ")
- term.setCursorPos(18,h) write(" ")
- term.setCursorPos(21,h) write(" ")
- term.setCursorPos(27,h) write(" ")
- term.setCursorPos(30,h) write(" ")
- term.setCursorPos(33,h) write(" ")
- term.setCursorPos(39,h) write(" ")
- term.setCursorPos(45,h) write(" ")
- end
- function line9(h)
- term.setCursorPos(5,h) write(" ")
- term.setCursorPos(9,h) write(" ")
- term.setCursorPos(11,h) write(" ")
- term.setCursorPos(15,h) write(" ")
- term.setCursorPos(18,h) write(" ")
- term.setCursorPos(21,h) write(" ")
- term.setCursorPos(24,h) write(" ")
- term.setCursorPos(27,h) write(" ")
- term.setCursorPos(30,h) write(" ")
- term.setCursorPos(33,h) write(" ")
- term.setCursorPos(39,h) write(" ")
- term.setCursorPos(42,h) write(" ")
- term.setCursorPos(48,h) write(" ")
- end
- function line10(h)
- term.setCursorPos(4,h) write(" ")
- term.setCursorPos(9,h) write(" ")
- term.setCursorPos(12,h) write(" ")
- term.setCursorPos(16,h) write(" ")
- term.setCursorPos(21,h) write(" ")
- term.setCursorPos(24,h) write(" ")
- term.setCursorPos(27,h) write(" ")
- term.setCursorPos(33,h) write(" ")
- term.setCursorPos(39,h) write(" ")
- term.setCursorPos(42,h) write(" ")
- term.setCursorPos(45,h) write(" ")
- end
- function whiteScreen()
- term.setBackgroundColour(colours.white)
- term.clear()
- term.setBackgroundColour(colours.black)
- end
- function splashScreen()
- whiteScreen()
- sleep(1)
- local scrollH = h+2
- while scrollH > 1 do
- whiteScreen()
- line1(scrollH)
- line2(scrollH+1)
- line3(scrollH+2)
- line4(scrollH+3)
- line5(scrollH+4)
- scrollH=scrollH-1
- sleep(0.1)
- end
- local scrollH = h+2
- while scrollH > 1 do
- whiteScreen()
- line1(2)
- line2(3)
- line3(4)
- line4(5)
- line5(6)
- line6(scrollH+6)
- line7(scrollH+7)
- line8(scrollH+8)
- line9(scrollH+9)
- line10(scrollH+10)
- scrollH=scrollH-1
- sleep(0.1)
- end
- term.setBackgroundColour(colours.white)
- term.setTextColour(colours.black)
- term.setCursorPos((w/2)-6,15)
- print("by fredTHead")
- term.setCursorPos((w/2)-15,17)
- print("Left and Right Arrows to move")
- term.setCursorPos((w/2)-6,18)
- print("Space to fire")
- term.setCursorPos((w/2)-10,19)
- write("Press fire when ready")
- while true do
- local event,p1,p2,p3 = os.pullEvent()
- if event=="char" then
- term.setBackgroundColour(colours.black)
- term.clear()
- runGame()
- end
- end
- end
- monSign()
- clear()
- term.setCursorPos(w/2-6 ,h/2)
- print("Press any key")
- while true do
- local event,p1,p2,p3 = os.pullEvent()
- if event=="char" then
- splashScreen()
- end
- end
Add Comment
Please, Sign In to add comment