⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 floy.java

📁 Floy的人工生命模拟
💻 JAVA
字号:
//
//	This code was written by Ariel Dolan
//	Site	http://www.aridolan.com
//	Email	aridolan@netvision.net.il
//
//	You are welcome to do whatever you wish with this code, as long as you
//	add appropriate credits.
//

import java.awt.*;

public class Floy {

	int revdist;//最小的碰撞的距离
	int numnb;//邻居的个数
	int sleep;//CPU间歇的时间
	int margin;//初始化时候的边距
	float acc;//苍蝇的加速度
	float acctomid;//苍蝇对中心的加速度
	float maxspeed;//允许的最大速度
	float bouncespeed;//反弹到边界时候的速度
	float v0;//初速度
	float x;//苍蝇的X位置
	float y;//苍蝇的Y位置
	float xtail;//尾巴的位置也就是上一个时间的X坐标
	float ytail;//尾巴的位置也就是上一个时间Y坐标
	float vx;//X方向的速度
	float vy;//Y方向的速度
	int width;//屏幕的宽度
	int height;//高度
	int num;//
	Floy neighbors[];//当前苍蝇的邻居
	Color color;
	int energy;//能量值(只有当Type=1的时候才有效)
	int type;//类型0为普通类型,1为异己
	float xc;//邻居中心点的位置
	float yc;//邻居中心点的位置


	public Floy(String cr, int n, int w, int h, int t, int e, Color c) {


		width = w;
		height = h;
		num = n;
		type = t;
		energy = e;
		color = c;
		
		revdist = Floys.REVDIST;
		acc = Floys.ACC;
		acctomid = Floys.ACCTOMID;
		//acctomid =0;
		maxspeed = Floys.MAXSPEED;
		bouncespeed = Floys.BOUNCESPEED;
		v0 = Floys.V0;
		sleep = Floys.SLEEP;
		margin = Floys.MARGIN;
		numnb = Floys.NUMNB;

		/*
		if ((cr != null) && (cr != ""))	{
			revdist = (int) cr.charAt(0) - 64;
			revdist = revdist*10;
			numnb = (int) cr.charAt(1) - 64;
			acc = (float) cr.charAt(2) - 64;
			acc = acc/10;
			acctomid = (float) cr.charAt(3) - 64;
			acctomid = acctomid/10;
			maxspeed = (float) cr.charAt(4) - 64;
			maxspeed = maxspeed/10;
			bouncespeed = (float) cr.charAt(5) - 64;
			bouncespeed = bouncespeed/10;
		}
		*/

		/*
		revdist = 200;
		acctomid = 0.1f;
		acc = 0.3f;
		maxspeed = 5f;
		bouncespeed = 0.8f;
		numnb = 2;
		*/

		x = (float) Math.random()*(width-margin*2) + margin;
		y = (float) Math.random()*(height-margin*2) + margin;
		xtail = x;
		ytail = y;

		vx = (float) Math.random()*v0-v0/2;
		vy = (float) Math.random()*v0-v0/2;

		neighbors = new Floy[numnb];

	}


	public int dist(Floy ng)
	{
		int d,d1,d2;
		d1=(int)(x-ng.x);
		d2=(int)(y-ng.y);
		d=d1*d1+d2*d2;
		return(d);
	}
	


	public void GetNeighbors()
	{
		//找到最近邻
		int i,j,k;
		Floy ng;

		for (i=0;i<numnb;i++) {
			ng=neighbors[i];;
			for (j=0;j<numnb;j++) {
				if (dist(ng.neighbors[j]) < dist(ng))
					neighbors[i]=ng.neighbors[j];
				if (ng.neighbors[j].type == 1)
					neighbors[i]=ng.neighbors[j];
			}
		}

		xc = 0;
		yc = 0;
		for (k=0;k<numnb;k++) {
			if (neighbors[k] == this) neighbors[k]=neighbors[k].neighbors[0];
			xc += neighbors[k].x;
			yc += neighbors[k].y;
		}
		xc = xc/numnb;
		yc = yc/numnb;
	}


	public void Process() {
	
		int rev;
		int i;
		int d;

		xtail = x;
		ytail = y;
		rev = -1;
		
		for (i=0;i<numnb;i++) {
			
			d = dist(neighbors[i]);
			if (d == 0)
				rev = 0;
			else if (d < revdist) {
				if (neighbors[i].type != type) {
					if (type == 1) {
						energy--;
						rev = -30;
					}
					else
						rev = 30;
				}
				else
				   rev = -1;
			}
			else  {
				if (type == 1){
					if(neighbors[i].type==1)
						rev = 1;
					else
						rev=0;
				}
				else if (neighbors[i].type == 1) 
					rev = 20;
				else
					rev = 1;
			}

			if (x < neighbors[i].x)
				vx += acc*rev;
			else
				vx -= acc*rev;

			if (y < neighbors[i].y)
				vy += acc*rev;
			else
				vy -= acc*rev;
		}
	

		if (vx > maxspeed) vx = maxspeed;
		if (vx < -maxspeed) vx = -maxspeed;
		if (vy > maxspeed) vy = maxspeed;
		if (vy < -maxspeed) vy = -maxspeed;

		if (x < 0) vx = bouncespeed;
		if (x > width) vx = -bouncespeed;
		if (y < 0) vy = bouncespeed;
		if (y > height) vy = -bouncespeed;

		
		if (type == 0) {
			if (x < width/2) vx += acctomid;
			if (x > width/2) vx -= acctomid;
			if (y < height/2) vy += acctomid;
			if (y > height/2) vy -= acctomid;
		}

		x += vx;
		y += vy;
		if (energy < 1)	{
			color = Color.green;
			type = 0;
			//Floys.beep.play();
		}
		
	}


	public void Draw(Graphics g) {

		g.setColor(color);
		g.drawLine((int) xtail, (int) ytail,(int) x,(int) y);
		g.fillOval((int) x, (int) y, 3, 3);

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -