'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'   Scrolling Platforms routine (C) Shockwave 2001
'   This Routine Scrolls a map linked to the joypad
'   in a horizontal direction. the platforms are made from
'   64*32 pixel blocks.
'
'   It is a fast routine, and it can easily be used to
'   form the basis of an engine for a platform game.
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'
level=1
shots=50
face=1
colx=64
plx=64
ply=0
energy=100
score=0
groundenemys=6
airenemys=2

open window 400,400

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Dimension a 2 dimensional array and read in map from data
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fdl=0
mxbl=3
dim mh(10)
dim pbs(mxbl)
dim pbx(mxbl)
dim pby(mxbl)
dim pbd(mxbl)
dim gnd(groundenemys)
dim gnx(groundenemys)
dim gny(groundenemys)
dim gnc(groundenemys)
dim gna(groundenemys)
dim gsx(groundenemys)
dim gsy(groundenemys)
dim gss(groundenemys)
dim platforms(25,14)


gep=1

  arx=ran(640)
  ary=-ran(640)

for a=1 to 10
mh(a)=100+ran(300)
next a

for y=1 to 14
for x=1 to 25
   read platforms(x,y)
   if platforms(x,y)=5 then
   platforms(x,y)=0 
   gnx(gep)=(x-1)*64
   gny(gep)=(y*32)-16
   gnd(gep)=int(ran(2))+1
   gep=gep+1
   end if
next x
next y
difficulty=level*200
if difficulty<30 difficulty=30
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Main Loop
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cn=6
scrollx=0
 
xpos=0 : Rem starting left block!

repeat
   setdrawbuf dw
   dw=1-dw
   setdispbuf dw

gosub readjoypad
gosub updateblocks
if bdstf=0 and rbs=0 gosub energybar
gosub drawplbullets
gosub updateplayer
gosub updategroundenemys
gosub updateairenemys
gosub missiles
gosub radar
rbs=0
bdstf=0
until ((peek("port1")=8) or (energy=0) or cn=0)

exit

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Draw the small radar display
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label radar
x=100
setrgb 1,50,50,150
rectangle 255,2 to 420,85

for a=1 to groundenemys
setrgb 1,50,50,150

if gna(a)=0 then
fill rectangle 260+(gnx(a)/10)-2,(gny(a)/5)-2 to 260+(gnx(a)/10)+2,(gny(a)/5)+2
end if
setrgb 1,150,150,50
fill rectangle 260+(((xpos*64)+arx-scrollx)/10)-2,(ary/10)-2 to 260+(((xpos*64)+arx-scrollx)/10)+2,(ary/10)+2

setrgb 1,50,150,50
fill rectangle 260+(((xpos*64)+plx-scrollx)/10)-2,(ply/10)-2 to 260+(((xpos*64)+plx-scrollx)/10)+2,(ply/10)+2
next a

return

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Responsible for drawing of air based baddies!
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

label updateairenemys
rn=1+ran(255)

setrgb 1,100,50,50
for b=1 to mxbl
if pbs(b)=1 and pbx(b)>=arx-10 and pbx(b)<=arx+10 and pby(b)>=ary-10 and pby(b)<=ary+10 then
  rectangle arx-20,ary-20 to arx+20,ary+20
score=score+100
ary=ary-600
rd=rd+150
rbs=1
end if
next b

  fill rectangle arx-10,ary-10 to arx+10,ary+10
  setrgb 1,255-rn,50,50
  fill rectangle arx-5,ary-5 to arx+5,ary+5

if ary<ply ary=ary+1
if ary>ply ary=ary-1
if arx<plx arx=arx+.5
if arx>plx arx=arx-.5

if bdstf=0 and arx>plx-15 and arx<plx+15 and ary>ply-10 and ary<=ply+26 then
ary=ary-600
energy=energy-10
rd=200
gr=200
bl=100
rbs=1
end if
return

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Guided missiles for ground based enemys
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label missiles

for a=1 to groundenemys
if gss(a)>0 then
rbs=1
 if gsx(a)>plx gsx(a)=gsx(a)-(gsx(a)-plx)/40
 if gsx(a)<plx gsx(a)=gsx(a)+(plx-gsx(a))/40
 if gsy(a)>ply gsy(a)=gsy(a)-(gsy(a)-ply)/40
 if gsy(a)<ply gsy(a)=gsy(a)+(ply-gsy(a))/40
setrgb 1,255-gss(a),255-gss(a),255
 fill rectangle gsx(a)-5,gsy(a)-5 to gsx(a)+5,gsy(a)+5
 gss(a)=gss(a)+1.5
 if gss(a)>255 gss(a)=0
if gsx(a)>=plx-10 and gsx(a)<=plx+10 then
bdstf=1
if (gsy(a))>=ply-16 and (gsy(a))<=ply+16 then
 gss(a)=0
setrgb 1,255,200,200
 rectangle gsx(a)-25,gsy(a)-25 to gsx(a)+25,gsy(a)+25
 energy=energy-20
rd=100
end if
end if
end if
next a
return

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Update the platform based enemys!
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label updategroundenemys

for a=1 to mxbl
for b=1 to groundenemys
if gnc(b)>0 gnc(b)=gnc(b)-.3
xw=gnx(b)-((xpos*64)-scrollx)
if pbs(a)=1 and xw>=0 and xw<=656 then
if gna(b)=0 and pbx(a)>=xw-16 and pbx(a)<=xw+16 then
bdstf=1
if pby(a)>=gny(b) and pby(a)<=gny(b)+36 then
score=score+50
rbs=1
rd=rd+10
gr=gr+10
bl=bl+15
pbs(a)=0
gnc(b)=gnc(b)+30
if gnc(b)>250 gna(b)=1
fi
fi
fi
next b
next a

flash=ran(255)
for a=1 to groundenemys

if gna(a)>0 goto nxtendr
xw=gnx(a)-((xpos*64)-scrollx)
sg=ran(1300-difficulty)
if xw>=0 and xw<=656 then 
if sg<3 and gss(a)=0 then
bdstf=1
gss(a)=1
gsx(a)=xw
gsy(a)=gny(a)
end if

if xw>=plx-10 and xw<=plx+10 then
if (gny(a))>=ply-16 and (gny(a))<=ply+16 then
rn=ran(2)
setrgb 1,255,155,155
rectangle gnx(a)-((xpos*64)-scrollx)-20-rn,gny(a)-4-rn to gnx(a)+20+rn-((xpos*64)-scrollx),gny(a)+40+rn
rd=rd+10
energy=energy-1
end if
end if
  setrgb 1,155+gnc(a),155,255-gnc(a)
  fill rectangle gnx(a)-((xpos*64)-scrollx)-14,gny(a)+2 to gnx(a)+14-((xpos*64)-scrollx),gny(a)+34

setrgb 1,0,0,flash
  fill rectangle gnx(a)-((xpos*64)-scrollx)-14,gny(a)+30 to gnx(a)+14-((xpos*64)-scrollx),gny(a)+36

setrgb 1,flash,0,0
  fill rectangle gnx(a)-((xpos*64)-scrollx)-4,gny(a)+10 to gnx(a)+4-((xpos*64)-scrollx),gny(a)+20

end if
  xwork=int(gnx(a)/64)+1
  ywork=int(gny(a)/32)+2
if platforms(xwork,ywork)=0 or platforms(xwork,ywork)=2 then
  gnd(a)=gnd(a)+1
  if gnd(a)>2 gnd(a)=1
end if

if gnd(a)=2 gnx(a)=gnx(a)-1
if gnd(a)=1 gnx(a)=gnx(a)+1
label nxtendr
if gna(a)>0 and gna(a)<50 then
rd=rd+5
gr=gr+5
bl=bl+20
setrgb 1,255-gna(a)*3,255-gna(a)*3,255-gna(a)*3
 rectangle gnx(a)-((xpos*64)-scrollx)-16-gna(a),gny(a)-gna(a) to gnx(a)+16-((xpos*64)-scrollx)+gna(a),gny(a)+36+gna(a)

 gna(a)=gna(a)+10
 if gna(a)>50 then
gna(a)=50
cn=cn-1
end if
end if
next a
return
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Draw Energy bars And Score Pannel for the player
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label energybar
setrgb 1,255,255,255
rectangle 50,499 to 590,511
rectangle 50,479 to 590,490
setrgb 1,10*shots,shots,shots
fill rectangle 51,500 to 52+10.75*shots,511
setrgb 1,energy,10*energy,energy
fill rectangle 51,480 to 52+5.38*energy,490
return


label drawplbullets
if fdl=3 then
rbs=1
setrgb 1,255,255,0
rd=rd+20
gr=gr+20
if face=1 fill rectangle plx+10,ply-8 to plx+20,ply+5
if face=2 fill rectangle plx-12,ply-8 to plx-22,ply+5

end if
setrgb 1,90,100,90
for a=1 to mxbl
 if pbs(a)=1 then
  bdstf=1
  fill rectangle pbx(a)-2,pby(a)-2 to pbx(a)+2,pby(a)+2
  if pbd(a)=1 pbx(a)=pbx(a)+15
  if pbd(a)=2 pbx(a)=pbx(a)-15
  if pbx(a)<0 or pbx(a)>640 pbs(a)=0
  pby(a)=pby(a)+.5
 end if
next a
return

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Read The Joypad!
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label updateplayer
setrgb 1,100,100,200
fill rectangle plx-8,ply-8 to plx+8,ply+4
setrgb 1,155,155,155
fill rectangle plx-6,ply-16 to plx+6,ply-9

if face=1 then
setrgb 1,155,155,255
fill rectangle plx-5,ply+4 to plx+5-wlks,ply+13

setrgb 1,50,50,250
fill rectangle plx-5,ply+13 to plx+8,ply+18
setrgb 1,255,155,155
fill rectangle plx-6,ply-9 to plx-12,ply+2
setrgb 1,0,0,0
line plx+2+wlks,ply+4 to plx,ply+13
fill rectangle plx+6,ply-15 to plx,ply-11
setrgb 1,255,255,255
fill rectangle plx+7,ply-2 to plx+16,ply-4
setrgb 1,20,20,155
fill rectangle plx-3,ply-8 to plx+1,ply+2
fill rectangle plx+1,ply+2 to plx+10,ply-3
if thrust=1 then
setrgb 1,155,155+ran(100),255
fill rectangle plx-6,ply  to plx-12,ply+5+ran(10)
end if
end if

if face=2 then
setrgb 1,155,155,255
fill rectangle plx-5-wlks,ply+4 to plx+5,ply+13
setrgb 1,50,50,250
fill rectangle plx+5,ply+13 to plx-8,ply+18
setrgb 1,255,155,155
fill rectangle plx+6,ply-9 to plx+12,ply+2
setrgb 1,0,0,0
line plx-2+wlks,ply+4 to plx,ply+13
fill rectangle plx-6,ply-15 to plx,ply-11
setrgb 1,255,255,255
fill rectangle plx-7,ply-2 to plx-16,ply-4
setrgb 1,20,20,155
fill rectangle plx+3,ply-8 to plx-1,ply+2
fill rectangle plx-1,ply+2 to plx-10,ply-3
if thrust=1 then
setrgb 1,155,155+ran(100),255
fill rectangle plx+6,ply  to plx+12,ply+5+ran(10)
end if
end if

return

label readjoypad

scf=0
plo=plx
thrust=0
if grav<5 grav=grav+.1
if ((and(peek("port1"),16384)<>0) and (ply>16)) then
ply=ply-3
thrust=1
if grav>0 grav=grav-.2
end if
if ply>445 ply=445

if (and(peek("port1"),32)<>0) then 
face=1
if ground=1 wlks=1*sin(ws*2)

if plx<400 plx=plx+2
if xpos=15 and plo=plx plx=plx+2
colx=colx+2
if plx>600 colx=colx-2
if plx>600 plx=600
end if

if (and(peek("port1"),128)<>0) then
face=2
if ground=1 wlks=1*sin(ws*2)

if plx>200 plx=plx-2
if xpos=0 and plo=plx plx=plx-2
colx=colx-2
if plx<40 colx=colx+2
if plx<40 plx=40
end if

if shots>0 and fdl=0 and and(peek("port1"),2048)<>0 then
for a=1 to mxbl
  if pbs(a)=0 then
  if face=1 pbx(a)=plx+20
  if face=2 pbx(a)=plx-20
  pby(a)=ply-3
  pbs(a)=1
  pbd(a)=face
  a=mxbl+1
  fdl=12
  shots=shots-1
  end if
next a
end if
if fdl>0 fdl=fdl-1
'32
if (plx>=400) and (xpos<15) then
scrollx=scrollx-2
plx=plx-2
if scrollx<=0 then
xpos=xpos+1
scrollx=scrollx+64
fi

fi
if (plx<=200) and (xpos>0) then
scrollx=scrollx+2
plx=plx+2
if scrollx>=64 then
xpos=xpos-1
scrollx=scrollx-64
fi
fi
if xpos<0  xpos=0 
if xpos>15 xpos=15
xwork=int(1+((colx)/64))
ywork=int((ply)/32)
ground=0
pwk=platforms(xwork,ywork+1)
if pwk=0 or platforms(xwork,ywork+1)=2 ply=ply+grav
if (pwk=1 or pwk=3 or pwk=4) and thrust=0 then 
ply=ywork*32
ground=1
if grav>=5 then 
energy=energy-5
rd=rd+90
rbs=1
end if
grav=0
end if
if pwk=2 then
energy=energy-.5
rd=rd+10
end if
if pwk=3 energy=energy+.5
if pwk=4 shots=shots+.2
if energy<0 energy=0
if energy>100 energy=100
if shots>50 shots=50
return

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Draw the platforms on the screen!
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label updateblocks
if rd>0 rd=rd-5
if gr>0 gr=gr-5
if bl>0 bl=bl-5
ws=ws+.1
wws=2+2*sin(ws)
if ws<0 ws=32
setrgb 1,0+rd,0+gr,50+bl
fill rectangle 0,469 to 640,512
setrgb 2,50+rd,150+gr,100+bl
setrgb 3,130+rd,30+gr,100+bl
gtriangle 0,0 to 640,468 to 0,468
gtriangle 0,0 to 640,468 to 640,0
b=1
setrgb 1,0,0,0
setrgb 2,40,20,10
setrgb 3,10,40,10

for a=1 to 720 step 72
 gtriangle a-70,440 to a,mh(b) to a+100,440
b=b+1
next a
setrgb 1,255,255,255
drawy=20
for y=1 to 14
drawx=scrollx-64
for x=xpos to xpos+10
if platforms(x,y)=0 goto got
 if platforms(x,y)=1 then
 setrgb 1,50,15,50
 fill rectangle drawx,drawy to drawx+64,drawy+32
 setrgb 1,100,200,150
 fill rectangle drawx,drawy to drawx+62,drawy+30
 end if
 if platforms(x,y)=1 goto got 
 if platforms(x,y)=2 then
 setrgb 1,100,100,255
 fill rectangle drawx,drawy+wws to drawx+64,drawy+32
 setrgb 1,200,200,255
 line drawx,drawy+wws to drawx+64,drawy+wws
 end if
 if platforms(x,y)=2 goto got 
 if platforms(x,y)=3 then
 setrgb 1,ran(255),255,255
 fill rectangle drawx,drawy to drawx+64,drawy+32
 setrgb 1,50,155,155
 fill rectangle drawx+2,drawy+2 to drawx+62,drawy+30
 end if
 if platforms(x,y)=4 then
 setrgb 1,255,ran(255),255
 fill rectangle drawx,drawy to drawx+64,drawy+32
 setrgb 1,155,50,155
 fill rectangle drawx+2,drawy+2 to drawx+62,drawy+30
 end if
label got
 drawx=drawx+64
next x
drawy=drawy+32
next y
return


'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' The Map:
' 0 signifies empty space
' 1 signifies a platform
' the screen is 640 pixels wide so use the screen markers
' (*) below to help when defining 'levels'
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'                   One Screen        Two screens
'                      *                   *
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0
data 0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,1,4,1,0
data 0,0,0,0,0,0,5,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0
data 0,0,0,0,4,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0
data 0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0
data 0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0
data 1,3,2,2,2,2,1,1,1,2,1,2,2,3,2,2,1,1,1,1,2,2,2,1,1















