gosub initialise
gosub main
exit

label main
 repeat
  gosub flipscreen
  if cb=1 gosub letters
  if cb=0 gosub ball
 until (1=2)
return

label ball
 clear window
 for b=1 to nb
  x=bx(b):y=by(b)
  x=x+bxd(b):y=y+byd(b)
  if x<40 or x>600 bxd(b)=-bxd(b)
  if y<40 or y>480 byd(b)=-byd(b)
  setrgb 1,0,ran(255),ran(255)
  gosub drawball
  bx(b)=x:by(b)=y
 next b
return

label letters
 for s=1 to ms
  x=sx(s):y=sy(s)
  setrgb 1,0,255,0
  gosub letter
  y=y-1
  setrgb 1,0,0,0
  fill rect x*20,y*20+8 to x*20+15,y*20-14
  setrgb 1,0,100,0
  gosub letter
  y=y-15
  setrgb 1,0,0,0
  fill rect x*20,y*20+8 to x*20+15,y*20-14
 next s
 for s=1 to ms
  if int(ran(2)+1)=1 sy(s)=sy(s)+1
  if sy(s)>my+16 then
   sy(s)=0
   sx(s)=int(ran(mx))
  fi
 next s
return

label letter
 if y<0 or y>my return
 c=scr(x,y)
 text x*20,y*20,chr$(c)
return

label flipscreen
 setdispbuf cb
 cb=1-cb
 setdrawbuf cb
return

label drawball
 ang=0
 repeat
  xo1=co(ang)*r:yo1=si(ang)*r
  xo2=co(ang+st)*r:yo2=si(ang+st)*r
  gtriangle x,y to x+xo1,y+yo1 to x+xo2,y+yo2
  gtriangle x,y to x-xo1,y+yo1 to x-xo2,y+yo2
  gtriangle x,y to x-xo1,y-yo1 to x-xo2,y-yo2
  gtriangle x,y to x+xo1,y-yo1 to x+xo2,y-yo2
  ang=ang+st
 until (ang>90)
return

label initialise
 open window 640,512

 mx=32:my=25
 dim scr(mx,my)
 for y=0 to my
  for x=0 to mx
   scr(x,y)=int(ran(96)+33)
  next x
 next y

 ms=20
 dim sx(ms),sy(ms)
 for a=1 to ms
  sx(a)=int(ran(mx))
  sy(a)=int(ran(my))
 next a

 dim co(360),si(360)
 f=pi/180
 for a=0 to 360
  si(a)=sin(a*f)
  co(a)=cos(a*f)
 next a
 nb=10
 dim bx(nb),by(nb),bxd(nb),byd(nb)
 for a=1 to nb
  bx(a)=ran(560)+40
  by(a)=ran(440)+40
  bxd(a)=int(ran(6)+4)
  byd(a)=int(ran(6)+4)
 next a
 setrgb 2,0,0,50:setrgb 3,0,0,50
 r=40:st=20

return

