'Asteroid Lab X (C) Christopher Rankine aKa Doctor.
'==================================================

open window 400,400
red=0
grn=0 
blu=1 
delay=8 
ystart=256 
menubars=4 
mchoices=1 
dim mtext$(mchoices)

mtext$(1)="              BEGIN GAME"

mtbar$="       ASTEROID LAB X by Christopher Rankine"

dw=0
choice=1
if menubars>20 menubars=20
if delay<1 delay=1
if red=1 red=255
if grn=1 grn=255
if blu=1 blu=255
if mchoices>10 mchoices=10
portflag=0

repeat
setdispbuf dw
dw=0-dw
setdrawbuf dw
clear window
gosub drawmenu
until (j=16384)

if choice= 1 then
pause .25 
close window
goto game
fi

label drawmenu

setrgb 1,0,0,0
setrgb 2,red-230,grn-230,blu-230
setrgb 3,red-230,grn-230,blu-230
gtriangle 0,0 to 640,510 to 0,510
setrgb 2,0,0,0
gtriangle 0,0 to 640,0 to 640,510

setrgb 1,red/10,grn/10,blu/10
fill rectangle 60,50 to 600,80
setrgb 1,red-150,grn-150,blu-150
fill rectangle 50,40 to 590,70
setrgb 1,0,0,0
text 62,60,mtbar$
setrgb 1,255,255,255
text 61,57,mtbar$

setrgb 1,55,55,55
text 240,135,"PRESS X TO SELECT"

yp=ystart

for menua=1 to mchoices

setrgb 1,0,0,0

if menua=choice setrgb 1,red/10,grn/10,blu/10
fill rectangle 125,yp+5 to 540,yp+25
setrgb 1,red-200,grn-200,blu-200
if choice=menua setrgb 1,red-150,grn-150,blu-150 

fill rectangle 120,yp to 535,yp+20
setrgb 1,0,0,0
text 130,yp+17,mtext$(menua)
setrgb 1,255,255,255
text 130,yp+14,mtext$(menua)
yp=yp+30

next menua

j=0
portflag=portflag+1
if portflag>delay portflag=0
if portflag=0 j=peek("port1")
if j=16 choice=choice-1
if j=64 choice=choice+1
if choice>mchoices choice=mchoices
if choice<1 choice=1

return

label game
gosub setup
gosub briefing
pause .5
gosub loop
exit

label briefing
text 10,100, "Welcome. Your mission is to protect the planet earth from" 
text 10,120,  "destruction. The planet is going yo be hit by an asteroid"
text 10,140,"field. The planet's defences can withstand upto 40 asteroids,"
text 10,160, "but no more. You must prevent more than 40 asteroids getting"
text 10,180, "passed you. Your ship is equiped only with a simple gun because"
text 10,200, "of the short notice. There will be powerups in the form of "
text 10,220, "white rectangles. Pick thes up from the white energy port on " 
text 10,240, "the front of the ship. If you don't hit it exactly there then"
text 10,260, "you will miss it. Press X to shoot. Remember the fate of"
text 10,280, "the entire planet rests on your shoulders. Good luck."
text 10,350, "Press X to begin or select to quit."
repeat
c=peek("port1")
if and(c,1)>0 end
until (and(c,16384)>0)
return

label setup
open window 640, 512
escape=0
score=0
count=0
astnum=14
pl1bul=5
health=1
plx = 100 
ply = 100 
dim x(astnum)
dim y(astnum)
dim s(astnum)
dim c(astnum)
dim bulx(pl1bul)
dim buly(pl1bul)
dim buls(pl1bul)
dim healthx(health)
dim healthy(health)
dim healths(health)
d=0
energy=100
mxtime=3000

for a = 1 to astnum
x(a)=1+ran(640)
y(a)=1+ran(640)
s(a)=1+ran(6)
next a

for a = 1 to health
healthx(a)=1+ran(640)
healthy(a)=1+ran(640)
healths(a)=1+ran(5)
next a
return

label loop
repeat
c=peek("port1")
setdispbuf d
d=1-d
setdrawbuf d
gosub cw
gosub asteroids
gosub collision
gosub control
gosub ship
gosub drawbullets
gosub bulletcollision
gosub energy
gosub health
gosub timer
setrgb 1, 225, 225, 225
text 400,500, "Time remaining : " + str$(int(mxtime/50))
text 500,50, "Score : " + str$(score)
if escape >40 then
beep
close window 
gosub worldcut
fi
if energy<-1 then
beep
close window 
gosub deathcut
fi
if mxtime<0 then
if energy>0 then
if escape<150 then
beep
close window
gosub complete
fi
fi
fi
until (and(c,1)>0)
return

label worldcut
open window 640,512
x(a)=640
y(a)=230
repeat
setrgb 1,255,255,255
text 250,40, "You failed"
text 100,80, "SCORE : " + str$(score)
text 100,120, "You destroyed " + str$(count) + " asteroids."
text 100,160, str$(escape) + " asteroids escaped."
c=peek("port1")
setdispbuf d
d=1-d
setdrawbuf d
clear window
setrgb 1,0,100,0
fill circle 300,240,30
setrgb 1,100,50,0
fill rectangle x(a),y(a) to x(a)+50,y(a)+50
if x(a)>300 then
x(a)=x(a)-5
fi
if and(c,1)>0 then
end
fi
until (x(a)=300 and y(a)=240)
return

label deathcut 
open window 640,512
plx=320
ply=256
x(a)=640
y(a)=250
repeat
setrgb 1,255,255,255
text 250,40, "You were destroyed!"
text 100,80, "SCORE : " + str$(score)
text 100,120, "You destroyed " + str$(count) + " asteroids."
text 100,160, str$(escape) + " asteroids escaped."
c=peek("port1")
setdispbuf d
d=1-d
setdrawbuf d
clear window
gosub ship
setrgb 1,100,50,0
fill rectangle x(a),y(a) to x(a)+50,y(a)+50
if x(a)>plx then
x(a)=x(a)-5
if and(c,1)>0 then
end
fi
fi
until (x(a)=300 and y(a)=240)
return

label complete
open window 640,512
plx=320
ply=356
repeat
setrgb 1,0,100,0
fill circle 300,240,30

setrgb 1,255,255,255
text 100,40, "You saved the planet. Well done!"
text 100,80, "SCORE : " + str$(score)
text 100,120, "You destroyed " + str$(count) + " asteroids."
text 100,160, str$(escape) + " asteroids escaped."
c=peek("port1")
setdispbuf d
d=1-d
setdrawbuf d
clear window
gosub ship
plx=plx+5 
if plx>640 then
plx=0
fi
if and(c,1)>0 end
until (and(c,1)>0) 
return

label health
for a = 1 to health
if energy<50 then
setrgb 1,255,255,255            
fill rectangle healthx(a), healthy(a) to healthx(a)+(healths(a)*3), healthy(a)+(healths(a)*3)
fi
healthx(a)=healthx(a)-healths(a)
if healthy(a)>512 healthy(a)=-50
if healthx(a)<=-50 then
healthx(a)=healthx(a)+720
fi
next a
return

label result
open window 640,512
text 100,100, "SCORE : " + str$(score)
text 100,120, "You destroyed " + str$(count) + " asteroids."
text 100,140, str$(escape) + " asteroids escaped."
repeat 
c=peek("port1")
until (and(c,1)>0)
return

label bulletcollision
for a = 1 to pl1bul

for b = 1 to astnum
if c(b)>0 c(b)=c(b)-2
if buls(a)=1 then
if bulx(a)>=x(b) and bulx(a)<= x(b)+s(b)*3 then 
if buly(a)>=y(b) and buly(a)<= y(b)+s(b)*3 then 
buls(a)=0
dest=1
c(b)=255
if dest =1 then
x(b)=660
c(b)=0
score=score+5
count=count+1
if score>=100 then
if energy<50 then
energy=energy+50
fi
fi
fi
fi
fi
fi
next b
next a
return

label drawbullets
for a = 1 to pl1bul

if buls(a)=1 then
setrgb 1, 100+ran(150),100,150
fill rectangle bulx(a)-2,buly(a)-2 to bulx(a)+2,buly(a)+2
bulx(a)=bulx(a)+10
buly(a)=buly(a)+1+ran(3)
if bulx(a)>640 buls(a)=0
fi
next 
return

label cw
red=20+(20*sin(mxtime/9))
grn=20+(20*sin(mxtime/10))
blu=20+(20*sin(mxtime/11))
setrgb 1,red+rad,0,0
setrgb 2,0,grn+gad,0
setrgb 3,0,0,blu+bad
gtriangle 0,0 to 640,512 to 0,512
gtriangle 0,0 to 640,512 to 640,0
return

label collision
for a = 1 to astnum
for b = 1 to health
l=energy
if ply>=y(a) and ply<=y(a)+s(a)*3 then
if plx>=x(a) and plx<=x(a)+s(a)*3 then
energy=energy-4
fi
fi
if ply-10>=y(a) and ply-10<=y(a)+s(a)*3 then
if plx>=x(a) and plx<=x(a)+s(a)*3 then
energy=energy-2
fi
fi
if ply+10>=y(a) and ply+10<=y(a)+s(a)*3 then
if plx>=x(a) and plx<=x(a)+s(a)*3 then
energy=energy-2
fi
fi
if ply>=y(a) and ply<=y(a)+s(a)*3 then
if plx-20>=x(a) and plx-20<=x(a)+s(a)*3 then
energy=energy-1
fi
fi
if ply>=healthy(b) and ply<=healthy(b)+healths(b)*3 then
if plx>=healthx(b) and plx<=healthx(b)+healths(b)*3 then
energy=100
fi
fi
if rad>0 rad=rad-1
if rad>0 gad=gad-1
if rad>0 bad=bad-1

if l>energy then
rad=150
gad=150
bad=150
plx=plx-5+(ran(10))
ply=ply-5+(ran(10))
x(a)=x(a)+10
fi
next b
next a
return

label asteroids
for a = 1 to astnum
setrgb 1, 200+(s(a)*2)+c(a), 40+c(a),20
fill rectangle x(a), y(a) to x(a)+(s(a)*3), y(a)+(s(a)*3)
setrgb 1, c(a), c(a), c(a)
fill rectangle x(a)+2, y(a)+2 to x(a)+2+s(a), y(a)+2+s(a)
fill rectangle x(a)+2, y(a)+5 to x(a)+2+s(a), y(a)+2+s(a)
fill rectangle x(a)+2, y(a)+9 to x(a)+2+s(a), y(a)+2+s(a)
x(a)=x(a)-s(a)/3
y(a)=y(a)-4+(ran(4))
if y(a)<-50 y(a)=520
if x(a)<=-50 then
escape=escape+1
x(a)=x(a)+720
y(a)=1+ran(510)
fi
next a
return

label control
c=peek("port1")
bdl=bdl+1
if bdl>10 bdl=1
if and(c,16)=16 plym=-5
if and(c,64)=64 plym=5
if and(c,128)=128 plxm=-5
if and(c,32)=32 plxm=5
if and(c,16384)=16384 and bdl=1 gosub newshot

if plxm<0 plxm=plxm+.5
if plxm>0 plxm=plxm-.5
if plym<0 plym=plym+.5
if plym>0 plym=plym-.5

plx=plx+plxm
ply=ply+plym
ply=ply+.1

if plx<10 plx=10
if ply<10 ply=10
if ply>500 ply=500
if plx>630 plx=630
return

label newshot
for a = 1 to pl1bul
if buls(a)=0 then
bulx(a)=plx
buly(a)=ply  
buls(a)=1
a=pl1bul+1
fi
next a
return

label ship
setrgb 1,255,50,50
fill triangle plx-20, ply-10 to plx-20,ply+10 to plx-10, ply
setrgb 1, 10, 10, 100
setrgb 2, 100, 100, 255
setrgb 3, 100, 100, 255
gtriangle plx-30, ply to plx, ply-10 to plx, ply+10
setrgb 1, 255, 255, 255
fill rectangle plx-5,ply-3 to plx+3,ply+3 
setrgb 1, 200, 200, 200
fill rectangle plx-5,ply-3 to plx-8,ply+3 
return

label energy
setrgb 1, 255, 255, 255
text 30,500, "Energy"
IF (energy>75) then
setrgb 1, 0, 255,0
fi
if (energy<=75) then
setrgb 1, 255, 255,0
fi
if (energy<50 ) then
setrgb 1, 255, 100,0
fi
IF (energy<25) then
setrgb 1, 255, 0,0
fi
fill rectangle 100,490 to 100+(energy*2),506
rectangle 100, 490 to 300, 506
return

label timer
mxtime=mxtime-1
if mxtime<0 beep
return



