open window 640,512
cb=1
mf=10
dim forkx(10)
dim forkox(10)
dim forkoy(10)
dim forky(10)
dim forki(10)
dim forkd(10)
dim forks(10)
dim forkb(10)

dim forks(10)
forkx(1)=300
forkox(1)=300
forky(1)=0
forkoy(1)=0
forki(1)=1
forkb(1)=1
forkd(1)=1
forks(1)=1

repeat
 setdrawbuf cb
 cb=1-cb
 setdispbuf cb
 clear window
 for a=1 to int(ran(20))+1
  wait 0.2
 next a
 dim forks(10)
 forkx(1)=int(ran(200))+300
 forkox(1)=forkx(1)
 forky(1)=0
 forkoy(1)=0
 forki(1)=1
 if forkx(1)>320 then
  forkd(1)=-1
 else
  forkd(1)=1
 fi
 forks(1)=1
 setrgb 0,255,255,255
 clear window
 setdispbuf 1-cb
 setrgb 0,0,0,0
 setdispbuf cb
 clear window
 gosub drawlightning
 setdrawbuf cb
 cb=1-cb
 setdispbuf cb
 wait 0.5
 clear window
until (1=2)

label drawlightning
repeat
 nf=0
 for a=1 to mf
  if forks(a)=1 then
   nf=nf+1
   b=forkb(a)
   setrgb 1,100*b,100*b,150*b
   line forkox(a)-2,forkoy(a) to forkx(a)-2,forky(a)
   line forkox(a)+2,forkoy(a) to forkx(a)+2,forky(a)
   setrgb 1,205*b,205*b,255*b
   line forkox(a),forkoy(a) to forkx(a),forky(a)
   forkox(a)=forkx(a)
   forkx(a)=forkx(a)+forkd(a)*ran(10)
   forkoy(a)=forky(a)
   forky(a)=forky(a)+10
   forki(a)=forki(a)-0.01
   if forkb(a)<1 then 
    forkb(a)=forkb(a)-0.05
   fi
   if forki(a)<0.9 and ran(forki(a))>(forki(a)/10)*9 then
    newf=0
    for b=1 to mf
     if forks(b)=0 then newf=b fi
    next b
    if newf>0 then
     forkox(newf)=forkx(a)
     forkx(newf)=forkx(a)
     forky(newf)=forky(a)
     forkoy(newf)=forky(a)
     forks(newf)=1
     forki(newf)=forki(a)/2
     forkd(newf)=-forkd(a)
     if int(ran(3))>1 then
      forkb(newf)=forkb(a)
     else
      forkb(newf)=forkb(a)-0.4
     fi
    fi
   fi
   if forky(a)>500 or forki(a)<0 or forkb(a)<0 then
    forks(a)=0
   fi
  fi
 next a
until (nf=0)
return
