' OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO
' oOo Spaced Invaders Coded By Shockwave (C) Dec 2001 oOo
' OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO
' o                                                     o
' O   A nice little space invaders rip off!!!!!!!!!!!   O
' o  -===============================================-  o
' O              Greetings to everyone at:              O
' o                 WWW.Yabasic.co.uk                   o
' O                 ~~~~~~~~~~~~~~~~~                   O
' OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO
'=========================================================
'                       Here we go:
'=========================================================
goto setup
label begin
goto titlescreen
label titleout
'=========================================================
'               Set up the pre-game stuff.
'=========================================================
    score=0 : rem Player's score.
    lives=3 : rem Set amount of lives.
    cdt=200 : rem Invulnerable for 4 seconds.
    plx=320 : rem Player X position.
    ply=450 : rem Player Y position.
 shield=440 : rem Shield strength.
       xm=0 : rem Intertia set to 0.
     bspd=5 : rem Enemy shotspeed.
 efiremx=10 : rem how many 50/sec to wait before firing.
     for a=1 to bullets
        pbs(a)=0
     next a
gosub nextlevel
'---------------------------------------------------------
'                      Game Loop :
'---------------------------------------------------------
label game
     coll=coll+1
if coll>2 coll=1

   gosub buffer
   gosub enemy_shots
   gosub bullet
   gosub enemys
   gosub player1
   gosub scores
  if coll=1 gosub ply_en_collision
  if coll=2 gosub enemy_shot_handling
  if enc=24 gosub nextlevel
if lives>0 goto game
'---------------------------------------------------------
'                    End of Game Loop!
'---------------------------------------------------------

repeat
 gosub buffer
 text 320,236,"GAME UNDER","cc"
 text 320,256,"YOU SCORED:"+str$(score),"cc"
 text 320,276,"-=PRESS START TO CONTINUE=-","cc"
until (and(peek("port1"),8)<>0)

'---------------------------------------------------------
'   Go back to top after displaying Game over Message.
'---------------------------------------------------------
goto begin
exit

' OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO
' oOo The following parts of the program are all cal- oOo
' oOo -led with the GOSUB command.                    oOo
' OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO

'=========================================================
' This sub routine just draws and moves the enemy shots.
'=========================================================

label enemy_shots
setrgb 1,255,ran(255),ran(255)
for a=1 to enbullets
 if ebs(a)=1 then
  fill rect ebx(a)-2,eby(a)-7 to ebx(a)+2,eby(a)+7
  eby(a)=eby(a)+bspd
  if eby(a)>512 ebs(a)=0
  fi
next a
return

'=========================================================
'Fire enemy bullets and checks for collisions with player.
'=========================================================

label enemy_shot_handling

'=========================================================
'                   Fire new bullet.
'=========================================================

   if enfdelay>0 enfdelay=enfdelay-1
   r=int(ran(24))
   if ens(r)=1 and enfdelay=0 then 

      enfdelay=efiremax
       for a=1 to enbullets
       if ebs(a)=0 then
         ebx(a)=enx(r)+15
         eby(a)=eny(r)+10
         ebs(a)=1
         a=enbullets+1
           red=red+20
           grn=grn+20
           blu=blu+20
       fi
       next a
   fi

'=========================================================
'                      Collisions
'=========================================================

if cdt=0 then
  for a=1 to enbullets
  if ebs(a)=1 and eby(a)>ply and ebx(a)>plx-15 and ebx(a)<plx+15 then
     lives=lives-1
     red=red+1000
     grn=grn+1000
     blu=blu+1000
       cdt=200
       plx=320
       shield=440
       ebs(a)=0
   fi
   next a
fi
return

'=========================================================
'       Check For Player bullet/enemy collisions.
'=========================================================

label ply_en_collision
for a=1 to bullets
   if pbs(a)=1 and pby(a)<eny(24)+20 then
   for b=1 to 24
     if ens(b)=1 then
       if pby(a)>eny(b)-5 and pby(a)<eny(b)+30 and pbx(a)>enx(b)-5 and pbx(a)<enx(b)+35 then
       ens(b)=2
       pbs(a)=0
       red=red+100
       grn=grn+100
       blu=blu+100
       score=score+5+int(ran(3))
       edr=edr*1.08
       enof=enof+8
       fi
     fi
   next b
   fi
next a
return

'=========================================================
'              Enemy Drawing And Movements.
'=========================================================

label enemys
  legs=5*sin(mm/2)
  setrgb 1,20,40,20
  for a=1 to 24
  if ens(a)=1 then
    fill triangle enx(a),eny(a) to enx(a)+legs,eny(a)+20 to enx(a)+15,eny(a)
    fill triangle enx(a)+30,eny(a) to enx(a)+30-legs,eny(a)+20 to enx(a)+15,eny(a)
  fi

  if ens(a)>1 then
     setrgb 1,55,55,ens(a)*5
     rect enx(a)+15-ens(a),eny(a)+15-ens(a) to enx(a)+15+ens(a),eny(a)+15+ens(a)
     ens(a)=ens(a)+4

    if ens(a)>30 then ens(a)=0 : enc=enc+1 : fi
    blu=blu+20
    setrgb 1,20,40,20
  fi
  next a

   setrgb 1,55,5,255
   setrgb 2,55,5,255
   setrgb 3,255,255,255

   for a=1 to 24
   if ens(a)=1 then
      gtriangle enx(a),eny(a) to enx(a)+30,eny(a) to enx(a)+15,eny(a)-5
      gtriangle enx(a),eny(a) to enx(a)+30,eny(a) to enx(a)+15,eny(a)+5
   fi
   next a

   revf=0
   for a=1 to 24
     if enof>0 eny(a)=eny(a)+1
     enx(a)=enx(a)+edr
   next a
   for a=1 to 24
     if ens(a)=1 and (enx(a)>610 or enx(a)<0) and revf=0 then revf=1: edr=-edr : a=31 : fi
   next a
   if enof>0 enof=enof-1
return

'=========================================================
'                Draw The Player's Bullets.
'=========================================================

label bullet
setrgb 1,ran(255),ran(255),255
for a=1 to bullets
 if pbs(a)=1 then
     fill rect pbx(a)-3,pby(a)-3 to pbx(a)+3,pby(a)+3
     pby(a)=pby(a)-10
     if pby(a)<0 pbs(a)=0
 fi
next a
return

'=========================================================
'                    Draw Score Table.
'=========================================================

label scores

  setrgb 1,100,155,100
  fill rect 101,26 to 100+shield,35
    setrgb 1,55,255,255
    rectangle 100,5 to 540,25
    rectangle 100,5 to 540,35
       text 101,19,"SCORE :"+str$(score)
       text 451,19,"LIVES :"+str$(lives)
return

'=========================================================
'        Update Player 1 Graphic And Controller.
'=========================================================

label player1

   if firedelay>0 firedelay=firedelay-1
   sv=3+3*sin(mm)
   mm=mm+.6
   setrgb 1,ran(100),ran(155),0
   fill triangle plx-10,ply+20 to plx+10,ply+20 to plx,ply+40+ran(40)

    setrgb 1,255-cdt,100,100
    setrgb 3,255-cdt,200,100
    setrgb 2,0,0,0
      gtriangle plx,ply to plx-20,ply+40 to plx,ply+25
      gtriangle plx,ply to plx+20,ply+40 to plx,ply+25
      setrgb 1,255,255,255
      fill triangle plx,ply+15 to plx+5,ply+25 to plx-5,ply+25

    c=peek("port1")

      if xm>0 xm=xm-.1
      if xm<=0 xm=xm+.1
      if xm<=.2 and xm>=-.1 xm=0

         if and(c,128)<>0 xm=xm-.4
         if and(c,32)<>0 xm=xm+.4
         if and(c,8)<>0 then
         repeat
               gosub buffer
               gosub scores
               setrgb 1,ran(255),ran(255),ran(255)
               mm=mm+.1
               text 320,256+10*sin(mm),"GAME PAUSED","cc"
         until (peek("port1")<>0 and (and(peek("port1"),8)=0))
         fi
         if and(c,16384)<>0 and firedelay=0 then
            for a=1 to bullets
              if pbs(a)=0 then
                pbx(a)=plx
                pby(a)=ply
                pbs(a)=1
                a=bullets+1
                firedelay=15
                red=red+50
                grn=grn+20
                blu=blu+20
                ply=ply+5 
                fill rect plx-6,ply-6 to plx+6,ply+6
               fi
            next a
         fi
         if cdt>0 cdt=cdt-1
         if and(c,2048)<>0 and shield>0 then
              setrgb 1,55,155+ran(100),55
              line plx,ply-5-sv to plx+20+sv,ply+36 +sv
              line plx,ply-5-sv to plx-20-sv,ply+36+sv
                shield=shield-2
                if shield<=0 shield=0
                cdt=cdt+1
         fi

         plx=plx+xm    

    if plx<20 then plx=20 : xm=0 :fi
    if plx>620 then plx=620 : xm=0 : fi
if ply>450 ply=ply-.5
return

'=========================================================
'  Set everything that needs setting for the new level!
'=========================================================

label nextlevel

      edr=1 : rem Enemy initial direction.
 enfdelay=0 : rem Enemy fire delay.
      enc=0 : rem Used to check how many enemys are left.
x=0
y=-200
enof=250
for a = 1 to 24
 enx(a)=x
 eny(a)=y
 ens(a)=1
 x=x+50
 if x>=400 then
    x=0
    y=y+60
 fi
next a
for a=1 to enbullets
 ebs(a)=0
next a
bspd=bspd+1
efiremx=efiremx-1
if efiremx<5 efiremx=5
if bspd>10 bspd=10
shield=shield+80
if shield>440 shield=440
return

'=========================================================
' Double buffering, Light effects, Star background.
'=========================================================

label buffer
    setdrawbuf dw
    dw=1-dw
    setdispbuf dw

     setrgb 1,red,5,5
     setrgb 2,5,grn,5
     setrgb 3,5,5,blu

       gtriangle 0,0 to 640,512 to 0,512
       gtriangle 0,0 to 640,512 to 640,0

'============
'=Starfield;=
'============

setrgb 1,10,55,255
for a=1 to stars
   fill rect strx(a),stry(a) to strx(a)+strs(a),stry(a)+strs(a)
   stry(a)=stry(a)+strs(a)
   if stry(a)>512 then strx(a)=ran(640) : stry(a)=stry(a)-512 : fi
next a

'===========
'=Lighting;=
'===========

red=red-(red/10)
grn=grn-(grn/10)
blu=blu-(blu/10)

return


' OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO
' oOo The following parts of the program are all cal- oOo
' oOo -led with the GOTO command for speed purposes!! oOo
' oOo I have found this to be better than GOSUB.      oOo
' OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO

'=========================================================
'     This does the nice little title screen page!
'=========================================================

label titlescreen
                  tl=255 : rem Fade message colour.
                  mess=1 : rem Reset fade message.
                     p=1 : rem Scroll text pointer.
                  fd=255 : rem Fade in variable for text.
if score>high high=score : rem New high score?

repeat
  gosub buffer
   setrgb 1,255-fd,255-fd,255-fd
   text 320,40,"-= S P A C E D   I N V A D E R S =-","cc"
   text 320,80,"-= PROGRAMMED BY MR.SHOCKWAVE =-","cc"
   rect 100,20 to 540,50
   rect 40,460 to 590,490
   rect 180,120 to 460,150
   text 320,140,"TODAYS HIGH SCORE:"+str$(high),"cc"

     text 320,220,"CONTROLS:","cc"
     text 320,240,"DPAD LEFT AND RIGHT TO MOVE YOUR SHIP","cc"
     text 320,260,"(R1) TO ACTIVATE SHIELD","cc"
     text 320,280,"(X) BUTTON TO FIRE","cc"
     text 320,300,"(START) TO PAUSE GAME","cc"
     text 320,320,"PRESS (X) TO START GAME!","cc"
     text scx,380,mid$(s$,p,67)

  scx=scx-1
  if scx<-10 then
    scx=scx+10
    p=p+1
    if p>len(s$) p=0
  fi

  setrgb 1,tl,tl,tl
  text 320,480,m$(mess),"cc"
  tl=tl-1
  if tl=0 then
    tl=255
    mess=mess+1
   if mess>10 mess=1
  fi
  if fd>0 fd=fd-1

until (and(peek("port1"),16384)<>0)
goto titleout

'=========================================================
'    Initial  game setup, Gfx screen arrays and data.
'=========================================================

label setup
   open window 640,512
     high=650 : rem Todays high score. 
        red=0 : rem Red lighting effect.
        grn=0 : rem Green lighting effect.
        blu=0 : rem Blue lighting effect.
     stars=35 : rem Number of stars to have.
    bullets=3 : rem Number of player bullets allowed.
  enbullets=5 : rem maximum amount of enemy bullets.
   dim enx(24) : rem Enemys' X position.
   dim eny(24) : rem Enemys' y position.
   dim ens(24) : rem Enemy status. 1=alive 2=dying 0 dead.

   dim strx(stars) : rem Star X position.
   dim stry(stars) : rem Star Y position.
   dim strs(stars) : rem Star Speed.

   dim pbx(bullets) : rem Bullet X position.
   dim pby(bullets) : rem Bullet Y position.
   dim pbs(bullets) : rem Bullet status. (1:in use/0:off)

   dim ebx(enbullets) : rem enemy bullet x-pos.
   dim eby(enbullets) : rem enemy bullet y-pos.
   dim ebs(enbullets) : rem enemy bullet state. 1:on,0:off

for a=1 to stars
   strx(a)=ran(640)
   stry(a)=ran(640)
   strs(a)=1+ran(4)
next a

'=========================================================
'         Text Fader message for title screen!
'=========================================================

dim m$(10)
m$(1)="THIS GAME WAS PROGRAMMED IN THE YABASIC LANGUAGE"
m$(2)="IT WAS WRITTEN IN THREE DAYS IN DECEMBER 2001"
m$(3)="BY MR.SHOCKWAVE  AKA  NICK SIMPSON"
m$(4)="IF YOU ARE INTERESTED IN PROGRAMMING"
m$(5)="OR YOU WANT TO GET IN TOUCH WITH ME THEN TRY:"
m$(6)="-= WWW.YABASIC.CO.UK =-"
m$(7)="AND REGISTER ON THE FORUMS PAGE OF THAT WEBSITE"
m$(8)="YABASIC.CO.UK - THE BEST YABASIC RESOURCE ON THE NET!"
m$(9)="LOADS OF DOWNLOADS, TUTORIALS AND CODING HELP!"
m$(10)="SEE YOU THERE!"

'=========================================================
'        Scrolling text message for title screen!
'=========================================================

p=0
scx=0
s$="                                                                  "
s$=s$+"A FEW PERSONAL HANDSHAKES FROM MR.SHOCKWAVE : "
s$=s$+"ADAM LAY (WEBMASTER AT WWW.YABASIC.CO.UK) FOR A "
s$=s$+"GREAT SITE.    DOCTOR (CHRISTOPHER RANKINE) FOR "
s$=s$+"THE NICE EMAILS AND COOL PROGRAMS.     XALTHORN "
s$=s$+"(MARC GALE) FOR THE NICE PROGRAMS AND CONSTRUCTIVE"
s$=s$+" COMMENTS.     JACOB BUSBY FOR THE NICE COMMENTS "
s$=s$+"AND GREAT PROGRAMS (SETTLERS OF CATAN LOOKS ACE!) "
s$=s$+"     "
s$=s$+"RAFRYER (RICHARD FRYER) FOR THE AMAZING 3D RACER "
s$=s$+"AND INSPIRING THIS GAME, AFTER I SAW YOURS I KNEW "
s$=s$+"I'D HAVE TO DO A SPACE INVADERS TYPE GAME MYSELF!!"
s$=s$+"    PSI-2 MAGAZINE FOR PUBLISHING MY PUK MAN GAME "
s$=s$+"    JINX (GARETH BAILEY),      JO MORROW,      "
s$=s$+"MASTER TONBERRY,     MONKEYPANTS,     SEPHIROTH,"
s$=s$+"     GOTHI,     TREV,     PHILL PACKMAN,     "
s$=s$+"PAUL DUNLOP,     VICKI (MY SWEETHEART!)     "
s$=s$+"AND ALL AT WWW.YABASIC.CO.UK'S FORUMS.          "
s$=s$+"AND THAT'S ABOUT ALL I CAN REMEMBER, CHECK OUT THE"
s$=s$+" FORUMS AT YABASIC.CO.UK... IT'S A WRAP!"
goto begin





