' +++++++++++++++++++++++++++++++++++++++++
' Butterfly Demo V1.5 - Code by Xone (c)2k8
' +++++++++++++++++++++++++++++++++++++++++
' USER SETTINGS
' ==============================================================
Butterflies = 60 rem Number of BFs
M = 1 rem Ball Mode (0/1), [1]Def
T = 1 rem 2 Colours (0/1), [1]Def
A = 0.5 rem BF Activity bias (0 to 1), [0.5]Def
C = 0.5 rem Speed the BFs Chase the ball [0.5]Def
g = 1 rem Gravity, [1]Def
' ==============================================================
BF=Butterflies: TmS=1
gosub X1: open window 640, 512
setrgb 0,0,0,0: Label Lp
setdrawbuf Cbu: Cbu = 1-Cbu
setdispbuf Cbu: clear window
if M=1 gosub bj
for i = 1 to BF
Tk(i)=Tk(i)+A
if Tk(i)>50 and KO(i)<=0 then:Tk(i)=0: rch=ran(10)
if rch<=7-(A*5) and t(i,1)=1 then:t(i,1)=0:r(i,2)=0
elsif rch>9.8 and t(i,1)=0 then:t(i,1)=1:fi
elsif KO(i)>0 then:t(i,1)=0:r(i,2)=0:fi
if t(i,1)=0 and w(i,2)>=490 then:r(i,1)=0:r(i,2)=0:w(i,2)=490
elsif t(i,1)=0 and w(i,2)<490 then:r(i,2) = 0
elsif t(i,1)=1 and w(i,2)<490 then
Fp=int(ran(20)):rl=int(ran(299)+1):Hd=int(ran(2)):Vd=int(ran(2))
if r(i,2)>=0 rk=int(ran(399)+1)
if r(i,2)<0 rk=int(ran(5999)+1)
if rl=Hd r(i,1)=-r(i,1)
if rk=Vd r(i,2)=-r(i,2)
if Fp<5 then:e(i,1)=(ran(8)-4)*(A*2):e(i,2)=(ran(12)-8)*(A*2):fi
elsif t(i,1)=1 and w(i,2)>=490 then:r(i,2)=-3.6:w(i,2)=488:fi
if r(i,1)<e(i,1) then:r(i,1)=r(i,1)+0.15
elsif r(i,1)>e(i,1) then:r(i,1)=r(i,1)-0.15:fi
if r(i,2)<e(i,2) then:r(i,2)=r(i,2)+0.15
elsif r(i,2)>e(i,2) then:r(i,2)=r(i,2)-0.15:fi
if M=1 then
if C<>0 and KO(i)<=0 then
if w(i,1)<n-lS r(i,1)=r(i,1)+C/8:if w(i,1)>n+lS r(i,1)=r(i,1)-C/8
if w(i,2)<m-lS r(i,2)=r(i,2)+C/8:if w(i,2)>m+lS r(i,2)=r(i,2)-C/8
fi
if KO(i)>0 KO(i)=KO(i)-0.01:if KO(i)>0 and w(i,2)<489 r(i,2)=r(i,2)+0.3
if n-lS<w(i,1) and n+lS>w(i,1) and m-lS<=w(i,2) and m+lS>=w(i,2) gosub re
fi
for jj=1 to BF:RSt=ran(10):if jj<>i and jj<=BF and RSt>9.3 gosub dl:next
if w(i,2)<490 then:w(i,1)=w(i,1)+(r(i,1)*1.2):w(i,2)=w(i,2)+(r(i,2)*1.2):
elsif w(i,2)>=490 then:t(i,1)=0:w(i,2)=490:fi
if r(i,2) >0 w(i,2)=w(i,2)+g/1.5
if r(i,2)<=0 w(i,2)=w(i,2)+(g*4)*0.6
if w(i,2)>=490 fh(i)=fh(i)-0.01
if w(i,2)<490 fh(i)=fh(i)+0.04
if fh(i)<=0 fh(i)=0:if fh(i)>=1 fh(i)=1
if r(i,1)<0 fx=-r(i,1):if r(i,1)>0 fx=r(i,1)
h(i)=h(i)+(((-r(i,2)+fx)/6)+0.4333)*fh(i)
ch=cos(h(i)): fl(i)=ch*fp(i): lf=fl(i)
if w(i,1)<=20 then:w(i,1)=20:r(i,1)=-r(i,1)*0.8:
elsif w(i,1)>=620 then:w(i,1)=620:r(i,1)=-r(i,1)*0.8:fi
if w(i,2)<=10 then:w(i,2)=10:r(i,2)=-r(i,2)*0.8:
elsif w(i,2)>=490 then:w(i,2)=490:fi
setrgb 1, 110,110,140: line 0,490 to 640,490
if m(i)>0 then: setrgb 1, 220,220,60: setrgb 2, 180,90,60: setrgb 3, 180,90,60
else: setrgb 1, 60,170,220: setrgb 2, 60,90,180: setrgb 3, 60,90,180: fi
gtriangle w(i,1),w(i,2)-1.4 to w(i,1)-(4.2)-lf,w(i,2)-7.7 to w(i,1)-(4.2)-lf,w(i,2)+4.2
gtriangle w(i,1),w(i,2)-1.4 to w(i,1)+(4.2)+lf,w(i,2)-7.7 to w(i,1)+(4.2)+lf,w(i,2)+4.2
line w(i,1),w(i,2)-4.2 to w(i,1),w(i,2)+4.2
line w(i,1),w(i,2)-4.2 to w(i,1)-2.8,w(i,2)-8.4
line w(i,1),w(i,2)-4.2 to w(i,1)+2.8,w(i,2)-8.4
next
Goto Lp
label X1
for p = 1 to BF
dim m(p): m(p)=TmS: if T=1 TmS=-TmS
dim KO(p): KO(p)=0: dim Tk(p): Tk(p)=0
dim fp(p): fp(p)=(3.5): dim fl(p): fl(p)=0
dim h(p): h(p)=0: dim fh(p): fh(p)=0
dim t(p,2): t(p,1)=1: t(p,2)=2
dim w(p,2): w(p,1)=ran(600)+20: w(p,2)=420
dim r(p,2): r(p,1)=ran(4): r(p,2)=-4
dim e(p,2): e(p,1)=ran(0): e(p,2)=ran(0)
next
n=320:u=0:lS=12:m=490-lS+1:q=0
return
label bj
q=q/1.01505:u=u+(0.7*g):n=n+(q*0.5):m=m+(u*0.5)
if n<10+lS then:n=10+lS:q=-q/1.6:
elsif n>630-lS then:n=630-lS:q=-q/1.6:fi
if m<10+lS then:m=10+lS:u=-u/1.6:
elsif m>490-lS then:m=490-lS:u=-u/1.3:fi
setrgb 1,180,30,60: fill circle n,m,lS
setrgb 1,255,50,60: fill circle (n*1.002),(m/1.002),lS/1.3
setrgb 1,255,140,130: fill circle (n*1.005),(m/1.005),lS/3
return
label re
if t(i,1)=1 then
qb=((w(i,1)-n)/2)*0.6:o=((w(i,2)-m)/2)*0.6
if w(i,1)<n then:w(i,1)=n-lS:r(i,1)=qb/1.5:
else:w(i,1)=n+lS:r(i,1)=qb/1.5:fi
if w(i,2)<m then:w(i,2)=m-lS:r(i,2)=o:
else:w(i,2)=m+lS:r(i,2)=o::fi
KO(i)=2:e(i,1)=0:e(i,2)=0:t(i,1)=0
if n>w(i,1) q=ran(5)+10:if n<w(i,1) q=ran(5)-15
if m>w(i,2) u=ran(3)+10:if m<w(i,2) u=ran(4)-20
elsif t(i,1)=0 and w(i,2)>=490 then
t(i,1)=1:r(i,2)=-3.6:w(i,2)=489:e(i,1)=-3
fi
return
label dl
if w(i,1)-(40*A)<=w(jj,1) and w(i,1)+(40*A)>=w(jj,1) and t(i,1)=0 and w(jj,2)>=490-20 then
r(i,2)=-3.6:t(i,1)=1
if w(i,2)>=490 w(i,2)=489:e(i,2)=(-8*A)
if w(i,1)<=w(jj,1) e(i,1)=-5*A
if w(i,1)>w(jj,1) e(i,1)=5*A
elsif w(i,2)-(40*A)<=w(jj,2) and w(i,2)+(40*A)>=w(jj,2) and w(i,1)-(40*A)<=w(jj,1) and w(i,1)+(40*A)>=w(jj,1) and t(i,1)=1 then
if w(i,1)<=w(jj,1) then:e(i,1)=-5*A:e(jj,1)=5*A:
elsif w(i,1)>w(jj,1) then:e(i,1)=5*A:e(jj,1)=-5*A:fi
fi
return
The original version of this has a score system where both team
colours play against each other in a tournament.
This is the watchable demo version.
you can turn off Ball mode if you just want to watch the Butterflies.
Set the chase speed to 3 or more if you want a laugh.

Likewise with the activity system, you can make them more active by
raising this.
The Butterflies have a number of ai routines too, so they may rest on the floor
or choose not to kick or chase the ball. They also disturb each other.
If a butterfly is resting, another butterfly may shift it for example. But if that
butterfly is stubborn, then it may refuse to move from its resting position.
