REM *** Space Invaders by Fryer
REM *** Final version
REM *** Thanks to Shockwave for the .xpo

open window 640,512
dim as(40),sx(100),sy(100),dropx(20),dropy(20),drop(20)
setup()
bc=0
label loop
    setdispbuf ss
    clear window
    j=peek("port1")
    if os=0 then
        xmax=0
        xmin=640
        count=0
        setrgb 1,200,0,0
        setrgb 2,200,0,200
        setrgb 3,200,0,200
        for y=1 to 4
            if y=2 setrgb 1,0,0,200
            for x=1 to 8
                xy=x+y*8
                c=as(xy)
                if c>0 then
                    ax=60*x+xoffset
                    if c>1 then
                        as(xy)=as(xy)+10
                        mad=12
                        if y=1 then
                            setrgb 1,200-c,0,0
                        else setrgb 1,0,0,200-c
                        end if
                        setrgb 2,200-c,0,200-c
                        setrgb 3,200-c,0,200-c
                    else mad=4
                        xmax=max(ax,xmax)
                        xmin=min(ax,xmin)
                    end if
                    ax=ax+ran(mad)
                    ay=40*y+yoffset+ran(mad)
                    if diver=xy then
                        bomb=int(ran(40))
                        ay=ay+dive
                        ax=ax+dxoff
                        if ax<sx dxoff=dxoff+1
                        if ax>sx dxoff=dxoff-1
                        if dxoff>0 dxoff=min(dxoff,512-dive)
                        if dxoff<0 dxoff=max(dxoff,dive-512)
                        if ay>512 ay=ay-512
if abs(sx-ax)<10 and ay>400 and ay<450 and shipstatus=0 shipstatus=2
                    else ay=ay-c
                        bomb=int(ran(200))
                        if shipstatus=0 and lives>0 then
if (abs(sx-ax)<10 and ay>400) or ay>450 then 
                                shipstatus=2
                                lives=1
                            end if
                        end if
                    end if
if abs(mx-ax)<20 and abs(my-ay)<6 and mf=1 and c=1 then
                        mf=0
                        my=400
                        as(xy)=2
                        oldscore=score
                        if y=1 score=score+20
                        if y>1 score=score+10
                        if diver=xy score=score+20 
if int(score/1000)>int(oldscore/1000) lives=lives+1
                    end if 
gtriangle ax,ay-10 to ax-20,ay to ax+20,ay
gtriangle ax-20,ay to ax-15,ay to ax-legs,ay+10
gtriangle ax+20,ay to ax+15,ay to ax+legs,ay+10
                    if c>1 then
                        if y=1 then
                            setrgb 1,200,0,0
                            else setrgb 1,0,0,200
                        end if
                        setrgb 2,200,0,200
                        setrgb 3,200,0,200
                    end if
                    if c>150 as(xy)=0
                    count=count+1
                    dv=int(ran(1000))
if diver=0 and dv=50 and score>420 then
                        dxoff=0
                        diver=xy
                        dive=0
                    end if
if bomb=20 and drop(drop)=0 and shipstatus=0 and ay>0 then
                        drop(drop)=1
                        dropx(drop)=ax
                        dropy(drop)=ay+10
                        drop=drop+1
                        if drop>dropmax drop=1
                    end if
                end if
            next x
        next y
        if diver>0 dive=dive+ymove
        if dive>512 diver=0
        if xmin<40 or xmax>600 then 
            xmove=-xmove
            if lives>0 yoffset=yoffset+ymove
            if count<20 and yoffset=34 and bonus=0 bonus=1
        end if
        xoffset=xoffset+xmove
        if yoffset<0 yoffset=yoffset+ymove
        legs=legs+legmove
        if legs=25 or legs=10 legmove=-legmove
        setrgb 1,255,255,255
        for i=1 to dropmax
            if drop(i)=1 then
                 dx=dropx(i):dy=dropy(i)
if dy>400 and dy<450 and abs(sx-dx)<10 and shipstatus=0 then
                     drop(i)=0
                     shipstatus=2
                end if
                line dx,dy to dx,dy-10
                dy=dy+6
                if dy>500 then
                    drop(i)=0
                    else dropy(i)=dy
                end if
            end if
        next i
    end if
    for i=1 to 100
        sy(i)=sy(i)+8
        if sy(i)>512 sy(i)=0
        dot sx(i),sy(i)
    next i
    if bonus>0 then
        setrgb 1,255*ss,255*ss,255*ss
if bonusflag=0 gtriangle bonus,40 to bonus-25,50 to bonus+25,50
        if bonusflag=1 text bonus-20,48,"100"
        bonus=bonus+3
        if bonus>640 then
            bonus=0
            bonusflag=0
        end if
if abs(mx-bonus)<25 and abs(my-50)<5 and bonusflag=0 then
            oldscore=score
            score=score+100
if int(score/1000)>int(oldscore/1000) lives=lives+1
            bonusflag=1
        end if
    end if
    if lives>0 then
        setrgb 3,100,0,0
        setrgb 2,100,0,0
        setrgb 1,0,0,200    
        for i=1 to lives-1
gtriangle 40+i*12,460 to 36+i*12,480 to 44+i*12,480
        next i
        if shipstatus>0 then
            setrgb 3,100-shipstatus,0,0
            setrgb 2,100-shipstatus,0,0
            shipstatus=shipstatus+2
        end if
        ox=sx
        msx=msx+and(j,32)/16-and(j,128)/64
        msx=max(min(msx,6),-6)
        if msx>0 msx=msx-.66
        if msx<0 msx=msx+.66
        if abs(msx)>.66 sx=sx+msx
        sx=max(min(sx,600),40)
        o=ox-sx
        sr=sr+1
        if sr>10 sr=sr-20
        setrgb 1,200,200,0
gtriangle sx,440 to sx-4-shipstatus,460+ss*6 to sx+4+shipstatus,460+ss*6
        setrgb 1,0,0,200-shipstatus
gtriangle sx,410+shipstatus to sx+15,450-o to sx-15,450+o
        setrgb 1,0,200-shipstatus*2,0
gtriangle sx,400+shipstatus to sx-5-abs(sr),420 to sx+5+abs(sr),420
            if shipstatus=100 then
                shipstatus=0
                lives=lives-1
                sx=320
            end if
        if count=0 level()
    end if
    setrgb 1,0,255,0
    if lives>0 and shipstatus=0 then
        if mf=0 and and(j,16384)>0 then
            mf=1
            mx=sx
            my=390
        end if
        if mf=0 line sx,400 to sx,390
    end if
    if mf=1 then
        my=my-8
        line mx,my to mx,my+10
        setrgb 1,0,120,0
        line mx,my+10 to mx,my+30
        setrgb 1,0,50,0
        line mx,my+30 to mx,my+50
        if my<8 mf=0
    end if
    if lives=0  then
        setrgb 1,255*ss,255*ss,255*ss
        if os=0 then
            text 250,104,"Space Invaders"
            text 150,250,"Press FIRE to play   START to exit"
        end if
        if and(j,16384)>0 then
            os=1
            if score>hiscore hiscore=score
            score=0
            h$=str$(hiscore)
        end if
        if and(j,16384)=0 and os=1 then
            os=0
            newgame()
        end if
    end if
    setrgb 1,255,255,0
    text 100,20,"SCORE:"+str$(score)
    text 420,20,"HI SCORE:"+h$
    setdrawbuf ss
    ss=1-ss
if and(j,8)=0 goto loop
text 200,300,"x - continue   o - exit"
rt=0
label option
    j=peek("port1")
    if and(j,16384)>0 rt=1
    if and(j,16384)=0 and rt=1 goto loop
    if and(j,8192)>0 end
goto option
sub newgame()
    lives=3
    sx=320
    mf=0
    reset()
    xmove=2
    xoffset=40
    yoffset=-180
    for i=1 to 20
        drop(i)=0
    next i
    shipstatus=0
    dropmax=2
    bonus=0
    bonusflag=0
    dive=0
    diver=0
end sub
sub level()
    reset()
    xmove=abs(xmove)+.5
    dropmax=min(dropmax+1,16)
    xoffset=40
    yoffset=-180
    diver=0
    if bonusflag=1 then
        bonusflag=0
        bonus=0
    end if
end sub
sub setup()
    reset()
    for i=1 to 100
        sx(i)=ran(640):sy(i)=ran(500)
    next i
    legs=20
    legmove=1
    xoffset=40
    yoffset=-180
    ymove=2
    xmove=2
    dropmax=0
    drop=1
    for i=1 to 20
        drop(i)=0
    next i
    score=0
    hiscore=0
    h$=str$(hiscore)
    lives=0
    sr=0
    ss=1
end sub
sub reset()
    for x=1 to 8
        for y=1 to 4
            xy=x+y*8
            as(xy)=1
        next y
    next x
    as(9)=0:as(12)=0:as(13)=0:as(16)=0
end sub












































