'Bats in the lab (C) Christopher Rankine aKa Doctor.
'===================================================


gosub setup
gosub loop
exit

'************************************
'*** Initialise Program Variables ***
'************************************
label setup
open window 640, 512
energy=100
mxtime=3000
score=0
face=1
walk=1
rdrop=50
dim rainx(rdrop)
dim rainy(rdrop)
dim rains(rdrop)
for a = 1 to rdrop
rainx(a)=1+ran(640)
rainy(a)=1+ran(510)
rains(a)=20+ran(20)
next a

plg=1
bats = 5
dim x(bats)
dim y(bats)
dim s(bats)
xpos=0
ypos=0
for a = 1 to bats
x(a)=xpos+int(ran(640))
y(a)=ypos+int(ran(500))
next a
d=0
pl1bul=5
dim bulx(pl1bul)
dim buly(pl1bul)
dim buls(pl1bul)
dim buld(pl1bul)
plat=8
dim e(plat)
dim f(plat)
dim g(plat)
dim hilly(20)
dim hillx(20)
for a = 1 to 10
hilly(a)=200+ran(100)
hillx(a)=1+ran(640)
next a
for a = 11 to 20
hilly(a)=300+ran(100)
hillx(a)=1+ran(640)
next a
xinc=640/plat
yinc=450/plat
xpos=0
ypos=50
psinc=2
for a = 1 to plat
e(a)=xpos+ran(40)
f(a)=ypos+ran(20)
g(a)=psinc
xpos=xpos+xinc
ypos=ypos+yinc
psinc=psinc+1
if psinc>4 psinc=2
next a
plx=e(1)
ply=f(1)-70
return

'*****************
'*** Main Loop ***
'*****************
label loop
repeat
setdrawbuf d
d=1-d
setdispbuf d
if energy<=0 dead=1
gosub bk
gosub hills
gosub rain
gosub platcollision
gosub control
gosub plat
gosub bats
gosub playerbullets
gosub man
gosub energy
gosub timer
if mxtime >0 then
setrgb 1,255,255,255
text 40,50, "Time remaining : " + str$(int(mxtime/50))
text 440,60, "Score : " + str$(score)
fi
if mxtime<0 then
mxtime=0
clear window
fi
if ply>512 dead=1
if dead=1 then
close window
pause 0.1
gosub death
fi
if mxtime=0 then
close window
pause 0.1
gosub complete
fi
until ((and(c,1)>0) or dead=1)
return

'*************
'*** DEATH ***
'*************
label death
open window 640,512
beep
repeat
c=peek("port1")
setrgb 1, 100, 0, 0
fill circle ran(640), ran(512), ran(25)
setrgb 1, 125, 125, 125
text 200,256, "You are dead! Press select to end"
until (and(c,1)<>0)
return

'****************
'*** COMPLETE ***
'****************
label complete
open window 640,512
beep
setrgb 1, 125,125,125
text 30,50,"Congratulations! You survived!"
text 30,100, "You got " + str$(score) + " points!"
text 30,150,"Post your score on the boards!"
text 30,200,"Post your score on www.YABASIC.co.uk. Thanks for playing!" 
repeat
c=peek("port1")
until (and(c,1)<>0)
return

'**************************
'*** Scrolling backdrop ***
'**************************
label hills
for a = 1 to 10
setrgb 1,20-lad,20-lad,20
fill triangle hillx(a),hilly(a) to hillx(a)-100,512 to hillx(a)+100,512
hillx(a)=hillx(a)-.5
if hillx(a)<-100 hillx(a)=hillx(a)+800+ran(10)
next a

for a = 11 to 20
setrgb 1,30-lad,30-lad,30
fill triangle hillx(a),hilly(a) to hillx(a)-100,512 to hillx(a)+100,512
hillx(a)=hillx(a)-1
if hillx(a)<-100 hillx(a)=hillx(a)+800+ran(10)
next a
return
 
'******************
'*** Energy Bar ***
'******************
label energy
setrgb 1,155,155,155
rectangle 100,480 to 540,510
setrgb 1,70,energy,50
fill rectangle 101,481 to 101+(energy*4.4),509
return

'*****************************************************
'*** Draw the bats and make them track the player. ***
'*****************************************************
label bats
for a = 1 to bats
f1f1=f1f1+1
if f1f1=14 then
flap=-5+ran(10)
f1f1=1
fi
setrgb 1,0,0,0
fill rectangle x(a)-4,y(a)-4+flap to x(a)+4,y(a)+4+flap
fill triangle x(a),y(a)+flap to x(a)-20,y(a)-10 to x(a)-20,y(a)+10
fill triangle x(a),y(a)+flap to x(a)+20,y(a)-10 to x(a)+20,y(a)+10
x(a)=x(a)-ran(2)
if x(a)<-30 x(a)=x(a)+690
if x(a)>plx then
if y(a)<ply y(a)=y(a)+ran(1)
if y(a)>ply y(a)=y(a)-ran(1)
fi
'*****************************************
'*** Collision between bat and player! ***
'*****************************************
for z = 1 to bats
if x(z)>plx-10 and x(z)<plx+10 then
if y(z)>ply and y(z)<ply+75 then
plx=plx-30
energy=energy-5
x(z)=x(z)+30
fi
fi
next z
next a
return

'*********************
'*** Draw the rain ***
'*********************
label rain
setrgb 1,55,55,55
for a = 1 to rdrop
line rainx(a), rainy(a) to rainx(a),rainy(a)+rains(a)
rainy(a)=rainy(a)+rains(a)
if rainy(a)>512 rainy(a)=rainy(a)-512
next a
return

'************************************
'*** Random lightening background ***
'************************************
label bk
lad=lad-10
if lad<0 lad=0

xst=1+int(ran(230))
if xst<5 lad=100
setrgb 1,10,10,30+lad
setrgb 2,20,3,20+lad
setrgb 3,30,4,10+lad
gtriangle 0,0 to 640,512 to 0,512
gtriangle 0,0 to 640,512 to 640,0
if lad=100 then 
nx=1+ran(640)
nx2=1+ran(640)
fi
x=nx
x2=nx2

if lad>60 then
setrgb 1,100,100,255
for y=-40 to 512 step 20
line x,y to x2,y+20
x=x2
x2=x2-40+ran(80)
next y
fi
return

'**************************
'*** Draw the platforms ***
'**************************
label plat
for a = 1 to plat
if onplat<> a dip=0
if onplat=a dip=1
setrgb 1,60,150,60+lad
fill rectangle e(a),f(a)+dip to e(a)+150,f(a)+10+dip
setrgb 1,60,50,0+lad
fill rectangle e(a),f(a)+10+dip to e(a)+150,f(a)+20+dip
if e(a)<10 e(a)=e(a)-g(a)*2
if f(a)<206 e(a)=e(a)-g(a)/2
if f(a)>=206 e(a)=e(a)-g(a)
if e(a)<-150 e(a)=e(a)+790
if plx<200 then
if ply>206 then
if f(a)<206 then
e(a)=e(a)-g(a)
fi
fi
fi
next a
return

'********************************************************
'*** Detect collisions between platforms and player 1 ***
'********************************************************
label platcollision
onplat=0
if grav>10 grav=10
for a = 1 to plat
if ((jump>0) and (grav>0)) or fall=1 then
if plx>=(e(a)-16) and plx<=e(a)+150 then
if ply+70>=f(a) and ply+70<=f(a)+10 then
ply=f(a)-70
jump=0
fall=0
fi
fi
fi
if plx>=(e(a)-16) and plx<=e(a)+150 then
if ply+70>=f(a) and ply+70<=f(a)+10 then
onplat=a
jump=0
fall=0
grav=0
ply=f(a)-70
fi
fi
if onplat=0 then
fall=1
fi
next a
return

'******************************************
'*** Do gravity and draw our little man ***
'******************************************
label man
if fall=1 or jump=1 then
ply=ply+grav
grav=grav+.5
if grav>10 grav=10
end if
if wflag=5 then
wflag=0
walk=walk+1
if walk>4 walk=1
end if

'body:
setrgb 1,200,100,100
if face=1 then
fill circle plx,ply+10,10
setrgb 1,0,0,255
fill triangle plx,ply+20 to plx-4,ply+50 to plx+4,ply+50
setrgb 1,130,130,255
line plx,ply+25 to plx,ply+40
line plx,ply+40 to plx+10,ply+40
setrgb 1,0,0,0
fill rectangle plx+10,ply+40 to plx+24,ply+36
end if

if face=2 then
fill circle plx,ply+10,10
setrgb 1,0,0,255
fill triangle plx,ply+20 to plx-4,ply+50 to plx+4,ply+50
setrgb 1,130,130,255
line plx,ply+25 to plx,ply+40
line plx,ply+40 to plx-10,ply+40
setrgb 1,0,0,0
fill rectangle plx-10,ply+40 to plx-24,ply+36
end if
setrgb 1,50,100,50

'Legs:
if walk=1 then
fill triangle plx-5,ply+45 to plx+5,ply+45 to plx-15,ply+75
fill triangle plx-5,ply+45 to plx+5,ply+45 to plx+15,ply+75
fi

if walk=2 or walk=4 then
fill triangle plx-5,ply+45 to plx+5,ply+45 to plx-7,ply+75
fill triangle plx-5,ply+45 to plx+5,ply+45 to plx+7,ply+75
fi

if walk=3 then
fill triangle plx-5,ply+45 to plx+5,ply+45 to plx,ply+75
fill triangle plx-5,ply+45 to plx+5,ply+45 to plx,ply+75
fi
return

'***********************
'*** Read controller ***
'***********************
label control
c=peek("port1")
if and(c,128)>0 then
plx=plx-4
wflag=wflag+1
face=2
fi
if and(c,32)>0 then
plx=plx+4
wflag=wflag+1
face=1
fi
delay=delay+1
if delay>6 then
if and(c,8192)>0 gosub newshot
delay=0
fi
if and(c,8)>0 gosub paused

if and(c,16384)>0 and (jump=0) then
jump=1
grav=-15
fi
if plx<5 plx=5
if plx>635 plx=635
return

'******************************************
'*** Pause function, wait for keypress! ***
'******************************************
label paused
pause.2
repeat 
until (peek("port1")<>0)
return

'***************************
'*** Decrease the clock! ***
'***************************
label timer
mxtime=mxtime-1
if mxtime=0 beep
return

'**********************
'*** Add new bullet ***
'**********************
label newshot
for a = 1 to pl1bul
if buls(a)=0 then
bulx(a)=plx
buly(a)=ply+38  
buld(a)=face
buls(a)=1
a=11
if face=1 plx=plx-2
if face=2 plx=plx+2
fi
next a
return

label playerbullets
setrgb 1,255,255,255
'draw and move bullets
for a = 1 to pl1bul
if buls(a)>0 then
fill rectangle bulx(a)-1,buly(a)-1 to bulx(a)+1,buly(a)+1
if buld(a)=1 bulx(a)=bulx(a)+10
if buld(a)=2 bulx(a)=bulx(a)-10
if bulx(a)<0 or bulx(a)>640 buls(a)=0
fi
next a
for a = 1 to pl1bul
if buls(a)=1 then
for b = 1 to bats
if bulx(a)>=x(b)-20 and bulx(a)<x(b)+20 then 
if buly(a)>=y(b)-10 and buly(a)<y(b)+10 then 
buls(a)=0
score=score+10
x(b)=660
y(b)=1+ran(500)
fi
fi
next b
fi
next a
return





