`*****Snipe Invaders*****
randomize timer()
hide mouse
autocam off
sync on
`Create the skysphere data
 
 
    create bitmap 1, screen width(), screen height()
    set current bitmap 1
    ink rgb(150,150,255),0
    box 0,0,100,100
    ink rgb(200,200,200),0
    for x = 1 to 500
        circle rnd(100),rnd(100),1
    next x
    get image 1,0,0,100,100
    set current bitmap 0
 
make object sphere 51,500
set object cull 51, 0
texture object 51, 1    
 
`Create the first invader
invaderdata:
data 4,1, 8,1
data 3,2, 4,2, 5,2, 6,2, 7,2, 8,2, 9,2
data 2,3, 3,3, 5,3, 6,3, 7,3, 9,3, 10,3
data 1,4, 2,4, 3,4, 4,4, 5,4, 6,4, 7,4, 8,4, 9,4, 10,4, 11,4
data 1,5, 3,5, 4,5, 5,5, 6,5, 7,5, 8,5, 9,5, 11,5
data 1,6, 3,6, 9,6, 11,6
data 4,7, 5,7, 7,7, 8,7
 
data 4,3,  8,3,  6,6 
 
make object cube 1, 1
make mesh from object 1, 1
delete object 1
 
make object triangle 1, 0,0,0,0,0,0,0,0,0
restore invaderdata
for x = 1 to 47
    add limb 1,x,1
    read xo
    read yo
    offset limb 1, x, xo - 6, -yo + 4, 0
    if x = 47 then scale limb 1, x, 500,100,100
next x
hide limb 1, 45
hide limb 1, 46
hide limb 1, 47
 
dim mode$(30)
dim desty#(30)
dim tx#(30)
dim ty#(30)
dim tz#(30)
dim mx#(30)
dim my#(30)
dim mz#(30)
dim tmout#(30)
dim ptx#(30,44)
dim pty#(30,44)
dim ptz#(30,44)
dim pmx#(30,44)
dim pmy#(30,44)
dim pmz#(30,44)
for x = 1 to 20
    if x <> 1 then clone object x, 1
    position object x,  rnd(200) - 100, rnd(500) + 100, 0
    mode$(x) = "descend"
next x
 
 
`Create Ground
    create bitmap 1, screen width(), screen height()
    set current bitmap 1
    ink rgb(0,150,0),0
    box 0,0,100,100
    ink rgb(0,200,0),0
    for x = 1 to 500
        circle rnd(100),rnd(100),1
    next x
    get image 2,0,0,100,100
    set current bitmap 0
make matrix 1,500,500,30,30
position matrix 1, -250, -100, -250
prepare matrix texture 1,2,1,1
fill matrix 1, 0, 1
randomize matrix 1, 20
update matrix 1
 
make object sphere 52,.3
scale object 52, 100,100,100000
 
 
 
position camera 0,0,-200
fog on: fog distance 600: fog color rgb(100,100,200)
 
 
 
if file exist("highscore.DATA") = 0
    open to write 1, "highscore.DATA"
    highscore# = 0
    write float 1, highscore#
    close file 1
endif
 
if file exist("highscore.DATA") = 1
    open to read 1, "highscore.DATA"
    read float 1, highscore#
    close file 1
endif
 
make object box 53, 2,2,7
hide object 53
 
newgame:
spd# = .7
score = 0
do
 
    tmr = timer()
 
    pick screen mousex(), mousey(), 20000000000
    mx# = get pick vector x()
    my# = get pick vector y()
    mz# = get pick vector z()
 
set cursor 0,0
        `Control Aliens
        spd# = spd# + .00025
        for x = 1 to 20
            if mode$(x) = "descend"
                position object x, object position x(x), object position y(x) - spd#, object position z(x)
                if object position y(x) < 50
                    mode$(x) = "right"
                    desty#(x) = 50
                endif
            endif
            if mode$(x) = "right"
                position object x, object position x(x)+ spd#, curvevalue(desty#(x),object position y(x),10), object position z(x)
                rotate object x, 0,curveangle(0,object angle y(x), 10),0
                if object position x(x) > 100
                    mode$(x) = "left"
                    desty#(x) = desty#(x) - 10
                    rotate object x, 0,object angle y(x) - 10,0
                endif
                if object position y(x) < get ground height(1,object position x(x) + 250, object position z(x) + 250) - 100 then goto youlose
                if rnd(2000) = 1 and object visible(53) = 0
                    position object 53, object position x(x), object position y(x) - 3, object position z(x)
                    point object 53, camera position x(), camera position y() - 5, camera position z()
                    show object 53
                endif            
            endif
            if mode$(x) = "left"
                position object x, object position x(x)- spd#, curvevalue(desty#(x),object position y(x),10), object position z(x)
                rotate object x, 0,curveangle(180,object angle y(x), 10),0
                if object position x(x) < -100
                    mode$(x) = "right"
                    desty#(x) = desty#(x) - 10
                    rotate object x, 0,object angle y(x) + 10,0                  
                endif
                if object position y(x) < get ground height(1,object position x(x) + 250, object position z(x) + 250) - 100 then goto youlose
 
                if rnd(2000) = 1 and object visible(53) = 0
                    position object 53, object position x(x), object position y(x) - 3, object position z(x)
                    point object 53, camera position x(), camera position y() - 5, camera position z()
                    show object 53
                endif
            endif
            if mode$(x) = "fall"
                position object x, object position x(x) + mx#(x), object position y(x) + my#(x), object position z(x) + mz#(x)
                rotate object x, object angle x(x) + tx#(x), object angle y(x) + ty#(x), object angle z(x) + tz#(x)
                my#(x) = my#(x) - .3
                if object position y(x) < get ground height(1,object position x(x) + 250, object position z(x) + 250) - 100
                    for p = 1 to 44
                        ptx#(x,p) = rnd(10) - 5
                        pty#(x,p) = rnd(10) - 5
                        ptz#(x,p) = rnd(10) - 5
                        pmy#(x,p) = rnd(10) +3:pmy#(x,p) = pmy#(x,p) * .5
                        pmx#(x,p) = rnd(10) - 5:pmx#(x,p) = pmx#(x,p) * .5
                        pmz#(x,p) = rnd(10):pmz#(x,p) = pmz#(x,p) * .5
                    next p
                    mode$(x) = "explode"
                    tmout#(x) = 200
                    rotate object x, 0,0,0
                    score = score + 100
                endif
            endif
            if mode$(x) = "explode"
                for p = 1 to 44
                    offset limb x,p, limb offset x(x,p) + pmx#(x,p), limb offset y(x,p) + pmy#(x,p), limb offset z(x,p) + pmz#(x,p)
                    rotate limb x,p, limb angle x(x,p) + ptx#(x,p), limb angle y(x,p) + pty#(x,p), limb angle y(x,p) + pty#(x,p)
                    pmy#(x,p) = pmy#(x,p) - .3
                next p
                tmout#(x) = tmout#(x) - 1
                if tmout#(x) < 0
                    gosub rebuildship
                    mode$(x) = "descend"
                    position object x,  rnd(200) - 100, rnd(300) + 100, 0
                endif
            endif
            if intersect object(x, object position x(52), object position y(52), object position z(52), ox#,oy#,oz#) > 0
                mode$(x) = "fall"
                tx#(x) = rnd(10) - 5
                ty#(x) = rnd(10) - 5
                tz#(x) = rnd(10) - 5
                my#(x) = rnd(10) +3:my#(x) = my#(x) * .1
                mx#(x) = rnd(10) - 5:mx#(x) = mx#(x) * .1
                mz#(x) = rnd(10):mz#(x) = mz#(x) * .1
            endif
            for a = 1 to 20
                if desty#(a) = desty#(x) and object collision(a,x) = 1
                    dst# = object position x(a) - object position x(x)
                    position object x, object position x(x) - (dst# / 10), object position y(x), object position z(x)
                    position object a, object position x(a) + (dst# / 10), object position y(a), object position z(a)             
                endif
            next a
 
 
        next x
 
        if mouseclick() = 0 and object position z(52) > 0 then msrd = 1
        if msrd = 1 and mouseclick() = 1
            position object 52, camera position x(), camera position y()-2, camera position z()
            point object 52, mx#, my# + 3, mz#
            msrd = 0
        endif
        ox# = object position x(52):oy# = object position y(52):oz# = object position z(52)
        move object 52, 50
 
        move object 53, 3
        roll object left 53, 20
        if object position z(53) < -200 and object visible(53) = 1
            hide object 53
            cls rgb(255,255,255)
            for x = 1 to 20
                desty#(x) = desty#(x) - 20
            next x
        endif
        if intersect object(53, object position x(52), object position y(52), object position z(52),ox#,oy#,oz#) > 0
            hide object 53
            turn object left 52, rnd(10) - 5
            pitch object up 52, rnd(10) - 5
            move object 52, 300
        endif
 
        turn object right 51, .2
 
        ink rgb(255,0,0),0
        circle mousex(), mousey(),2
        circle mousex(), mousey(),1
        circle mousex(), mousey(),0
 
        set cursor 0,0
        if score > highscore# then highscore# = score
        print "SCORE: " +  str$(score)
        print " HIGH: " + str$(highscore#)
 
    sync
    while timer() - tmr < 30
    endwhile
loop
 
 
rebuildship:
    restore invaderdata
    for p = 1 to 44
        read xo
        read yo
        offset limb x, p, xo - 6, -yo + 4, 0
        rotate limb x,p,0,0,0
    next p
return
 
 
youlose:
    do
        hide object 53
        tmr = timer()
        sync
            for x = 1 to 20
                position object x, object position x(x), object position y(x), object position z(x) - 2
                roll object left x, 3
            next x
            center text screen width()/2, screen height()/2, "GAME OVER"
            if score >= highscore# then center text screen width()/2, screen height()/2 + 15, "NEW HIGH SCORE!"
            center text screen width()/2, screen height()/2 + 30, "PRESS SPACE TO CONTINUE"
            if spacekey() = 1
                for x = 1 to 20
                    position object x,  rnd(200) - 100, rnd(500) + 100, 0
                    rotate object x,0,0,0
                    mode$(x) = "descend"
                    desty#(x) = 50
                    gosub rebuildship
                next x
                if score >= highscore#
                    delete file "highscore.DATA"
                    open to write 1, "highscore.DATA"
                    write float 1, highscore#
                    close file 1
                endif
                goto newgame
            endif
        while timer() - tmr < 30
        endwhile
    loop