home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1999 February
/
VPR9902A.BIN
/
FFILLY
/
BOID
/
boid3.tfy
< prev
next >
Wrap
Text File
|
1997-11-23
|
4KB
|
222 lines
#info IART "もりきゅう,VZF07161@niftyserve.or.jp"
#info ICMT "Boid 3"
#info INAM "Boid 3"
#info ICOP "(c)Kazuhiro.Yoshida"
#info ISBJ "Boid 3"
#info VIDO "640x480; 256"
#info GRPC "もりきゅう,VZF07161@niftyserve.or.jp"
#include "local.tfy"
int distance( int nPx1, int nPy1, int nPx2, int nPy2 )
{
int nDistance;
real rPx1, rPy1, rPx2, rPy2, rDistance;
rPx1 = InttoReal(nPx1);
rPy1 = InttoReal(nPy1);
rPx2 = InttoReal(nPx2);
rPy2 = InttoReal(nPy2);
rDistance = Sqrt( Pow( rPx2 - rPx1, 2.0 ) + Pow( rPy2 - rPy1, 2.0 ) );
nDistance = RealtoInt( rDistance );
return nDistance;
}
main()
{
int i,j;
int x,y;
int sum; // 物体(岩と鳥)の数
int in; // ここまで岩。ここから鳥。
int wMain;
int pBase,pBack,pBoid,pRock;
int cBack;
int nTransColor; // 透明色
int cBoid[];
int bw2,bh2;
int nx[],ny[]; // 位置
int vx[],vy[]; // 速度
int ax[],ay[]; // 加速度
pBase = LoadPic("base.bmp");
pBoid = LoadPic("boid.bmp");
pRock = LoadPic("rock.bmp");
pBack = copyPic(pBase);
nTransColor = GetColor(pBoid,0,0);
cBack = PutCast(pBack,pBase);
wMain = OpenWin(pBase);
// ボイド表示修正用
bw2 = PicWidth(pBoid) / 2;
bh2 = PicHeight(pBoid) / 2;
sum = 20;
in = 10;
// 岩の状態
for( i=0 ; i<in ; i=i+1 )
{
nx[i] = PicWidth(pBase)/2;
ny[i] = PicHeight(pBase)/2;
vx[i] = vy[i] = 0;
ax[i] = ay[i] = 0;
cBoid[i] = PutCast(pRock,pBase,nx[i]-bw2,ny[i]-bh2,nTransColor);
}
// 鳥の状態
for( i=in ; i<sum ; i=i+1 )
{
nx[i] = Random(PicWidth(pBase));
ny[i] = Random(PicHeight(pBase));
vx[i] = Random(11)-5;
vy[i] = Random(11)-5;
ax[i] = ay[i] = 0;
cBoid[i] = PutCast(pBoid,pBase,nx[i]-bw2,ny[i]-bh2,nTransColor);
}
int mesTIME;
mes(TIME)
{
step(5)
{
// 質量中心を求める
x=0;
y=0;
for(i=0;i<sum;i=i+1)
{
x = x + nx[i];
y = y + ny[i];
}
x = x/sum;
y = y/sum;
// 規則1
// 鳥は質量中心に向かう
for(i=in;i<sum;i=i+1)
{
if ( nx[i] < x )
{
ax[i] = 1;
}
else if ( nx[i] > x )
{
ax[i] = -1;
}
if ( ny[i] < y )
{
ay[i] = 1;
}
else if ( ny[i] > y )
{
ay[i] = -1;
}
}
// 規則2
// 鳥は物体と一定の間隔を空ける
for(i=in;i<sum;i=i+1)
{
for(j=0;j<sum;j=j+1)
{
if ( j==i )
{
break;
}
if ( distance(nx[i],ny[i],nx[j],ny[j]) < 60 )
{
if ( nx[i] < nx[j] )
{
ax[i] = -1;
}
else if ( nx[i] > nx[j] )
{
ax[i] = 1;
}
if ( ny[i] < ny[j] )
{
ay[i] = -1;
}
else if ( ny[i] > ny[j] )
{
ay[i] = 1;
}
}
}
}
// 規則3
// 鳥は周りの鳥と速度を合わせる
for(i=in;i<sum;i=i+1)
{
for(j=in;j<sum;j=j+1)
{
if ( j==i )
{
break;
}
if ( distance(nx[i],ny[i],nx[j],ny[j]) < 60 )
{
if ( vx[i] < vx[j] )
{
ax[i] = 1;
}
else if ( vx[i] > vx[j] )
{
ax[i] = -1;
}
if ( vy[i] < vy[j] )
{
ay[i] = 1;
}
else if ( vy[i] > vy[j] )
{
ay[i] = -1;
}
}
}
}
// 鳥の状態を変化
for(i=in;i<sum;i=i+1)
{
vx[i] = vx[i] + ax[i];
vy[i] = vy[i] + ay[i];
nx[i] = nx[i] + vx[i] / 2;
ny[i] = ny[i] + vy[i] / 2;
}
// 移動
for(i=in;i<sum;i=i+1)
{
MoveCast(cBoid[i],pBoid,nx[i]-bw2,ny[i]-bh2);
}
}
}
mesTIME = GetMesNo(0);
int pause;
pause = 0;
mes(LBUP)
{
if ( pause == 0 ) {
FreezeMes(mesTIME);
pause = 1;
} else {
ActivateMes(mesTIME);
pause = 0;
}
}
}