Turtles Advanced

Size
4,040 Kb
Views
18,216

How do I make an turtles advanced?

What is a turtles advanced? How do you make a turtles advanced? This script and codes were developed by Scott R McGann on 11 November 2022, Friday.

Turtles Advanced Previews

Turtles Advanced - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Turtles Advanced</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <canvas id="canvas"></canvas> <script src="js/index.js"></script>
</body>
</html>

Turtles Advanced - Script Codes CSS Codes

html,body{	margin:0px;	width:100%;	height:100%;	overflow:hidden;
}
#canvas{	width:100%;	height:100%;
}

Turtles Advanced - Script Codes JS Codes

function initVars(){	pi=Math.PI;	canvas=document.querySelector("#canvas");	ctx=canvas.getContext("2d");	canvas.width=document.body.clientWidth;	canvas.height=document.body.clientHeight;	cx=canvas.width/2;	cy=canvas.height/2;	window.addEventListener("resize",function(){	canvas.width=document.body.clientWidth;	canvas.height=document.body.clientHeight;	cx=canvas.width/2;	cy=canvas.height/2;	});	turtles=[];	food=[];	initialFood=20;	frames=0;	seed=11;	res=8;	cols=16*res;	rows=8*res;	tileWidth=cx*2/cols;	tileHeight=cy*2/rows;	turtleSpeed=2.5/2;	turnRadius=2.5*2;	foodFreq=3.5/5;	maxPop=120;	trails=.75;	envSpeed=1.5/2;
}
function Turtle(x,y,size,theta){	this.x=x, this.y=y, this.size=size, this.theta=theta, this.health=1;
}
function Food(x1,y1,x2,y2,weight){	this.x1=x1, this.y1=y1, this.x2=x2, this.y2=y2, this.weight=weight;	this.size=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
function rand(){	seed+=.1234;	return parseFloat('0.'+Math.sin(seed).toString().substr(6));
}
function loadControlPoints(){	points=25;	controls=new Array(points);	for(i=0;i<points;++i){	controls[i]=new Object();	controls[i].x=parseInt(rand()*cols);	controls[i].y=parseInt(rand()*rows);	controls[i].vx=.5-rand()/1;	controls[i].vy=.5-rand()/1;	}
}
function loadTerrain(){	terrain=new Array(cols);	max=0;	for(i=0;i<cols;++i){	terrain[i]=new Array(rows);	for(j=0;j<rows;++j){	terrain[i][j]=new Object();	d1=d2=0;	for(k=0;k<points;++k){	d1+=Math.sqrt((i-controls[k].x)*(i-controls[k].x)+(j-controls[k].y)*(j-controls[k].y));	}	c=1;	for(k=0;k<points;++k){	d2=Math.sqrt((i-controls[k].x)*(i-controls[k].x)+(j-controls[k].y)*(j-controls[k].y));	c*=1-(1-(d2/d1));	}	if(c>max)max=c;	terrain[i][j].color=c;	}	}	for(i=0;i<cols;++i){	for(j=0;j<rows;++j){	terrain[i][j].color/=max;	}	}	for(i=0;i<cols;++i){	for(j=0;j<rows;++j){	cell=terrain[i][j];	l=i?i-1:cols-1;	u=j?j-1:rows-1;	r=i<cols-1?i+1:0;	d=j<rows-1?j+1:0;	left=terrain[l][j];	up=terrain[i][u];	right=terrain[r][j];	down=terrain[i][d];	upleft=terrain[l][u];	upright=terrain[r][u];	downright=terrain[r][d];	downleft=terrain[l][d];	c=parseInt(cell.color*15);	cell.differentNeighbors=(c!=parseInt(up.color*15)?1:0)+ (c!=parseInt(down.color*15)?1:0)+ (c!=parseInt(left.color*15)?1:0)+ (c!=parseInt(right.color*15)?1:0)+ (c!=parseInt(upleft.color*15)?1:0)+ (c!=parseInt(upright.color*15)?1:0)+ (c!=parseInt(downleft.color*15)?1:0)+ (c!=parseInt(downright.color*15)?1:0);	}	}
}
function doLogic(){	for(i=0;i<controls.length;++i){	vx=controls[i].vx*envSpeed;	vy=controls[i].vy*envSpeed;	if(controls[i].x+vx>=cols || controls[i].x+vx<0)controls[i].vx*=-1;	if(controls[i].y+vy>=rows || controls[i].y+vy<0)controls[i].vy*=-1;	controls[i].x+=vx;	controls[i].y+=vy;	}	loadTerrain();	for(i=0;i<cols;++i){	for(j=0;j<rows;++j){	if(parseInt(terrain[i][j].color*15)==2){	for(k=0;k<turtles.length;++k){	x=i*tileWidth+tileWidth/2;	y=j*tileHeight+tileHeight/2;	d=Math.sqrt((turtles[k].x-x)*(turtles[k].x-x)+(turtles[k].y-y)*(turtles[k].y-y));	if(d<turtles[k].size*2){	p=Math.atan2(x-turtles[k].x,y-turtles[k].y);	s=turtles[k].size*2-d;	turtles[k].x-=Math.sin(p)*s;	turtles[k].y-=Math.cos(p)*s;	}	}	for(k=0;k<food.length;++k){	x1=i*tileWidth+tileWidth/2;	y1=j*tileHeight+tileHeight/2;	x2=(food[k].x1+food[k].x2)/2;	y2=(food[k].y1+food[k].y2)/2;	d=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));	if(d<food[k].size){	p=Math.atan2(x1-x2,y1-y2);	s=food[k].size-d;	food[k].x1-=Math.sin(p)*s;	food[k].y1-=Math.cos(p)*s;	food[k].x2-=Math.sin(p)*s;	food[k].y2-=Math.cos(p)*s;	}	}	}	}	}	if(turtles.length){	if(rand()<.01+foodFreq-.2){	weight=10+rand()*20;	x1=100+rand()*(cx*2-200);	y1=100+rand()*(cy*2-200);	p=pi*2*rand();	x2=x1+Math.sin(p)*weight;	y2=y1+Math.cos(p)*weight;	food.push(new Food(x1,y1,x2,y2,weight));	}	}	for(i=0;i<turtles.length;++i){	turtles[i].x-=Math.sin(turtles[i].theta)*(4+turtles[i].size/6)*turtleSpeed;	turtles[i].y-=Math.cos(turtles[i].theta)*(4+turtles[i].size/6)*turtleSpeed;	if(turtles[i].x<-turtles[i].size)turtles[i].x+=cx*2;	if(turtles[i].y<-turtles[i].size)turtles[i].y+=cy*2;	if(turtles[i].x>cx*2+turtles[i].size)turtles[i].x-=cx*2;	if(turtles[i].y>cy*2+turtles[i].size)turtles[i].y-=cy*2;	mind=10000000;	t=-1;	for(j=0;j<food.length;++j){	x=(food[j].x1+food[j].x2)/2;	y=(food[j].y1+food[j].y2)/2;	d=(turtles[i].x-x)*(turtles[i].x-x)+(turtles[i].y-y)*(turtles[i].y-y);	if(d<mind){	mind=d;	t=j;	}	if(d<turtles[i].size*turtles[i].size*2){	if(turtles[i].size<50){	turtles[i].size+=food[j].weight/10+turtles[i].size/350;	}else{	for(k=0;k<=3;++k){	p=pi*2/3*k+turtles[i].theta+pi;	s=turtles[i].size*(!k||k==3?2:1);	x2=x1;	y2=y1;	x1=turtles[i].x+Math.sin(p)*s;	y1=turtles[i].y+Math.cos(p)*s;	if(k){	for(m=0;m<12;++m){	x1a=x1+(x2-x1)/12*m;	y1a=y1+(y2-y1)/12*m;	x2a=x1+(x2-x1)/12*(m+1);	y2a=y1+(y2-y1)/12*(m+1);	food.push(new Food(x1a,y1a,x2a,y2a,1+turtles[i].size/8));	}	}	}	for(k=0;k<2+turtles[i].health*6;++k){	if(turtles.length<maxPop)turtles.push(new Turtle(turtles[i].x,turtles[i].y,10,rand()*pi*2));	}	t=-1;	turtles.splice(i,1);	break;	}	t=-1;	food.splice(j,1);	turtles[i].health=1;	}	}	if(t!=-1){	while(turtles[i].theta>pi*2)turtles[i].theta-=pi*2;	while(turtles[i].theta<0)turtles[i].theta+=pi*2;	x=(food[t].x1+food[t].x2)/2;	y=(food[t].y1+food[t].y2)/2;	p=Math.atan2(turtles[i].x-x,turtles[i].y-y);	d=Math.abs(p-turtles[i].theta);	if(d>pi){	p+=pi*2*(p>turtles[i].theta?-1:1);	}	d=Math.abs(p-turtles[i].theta);	if(p>turtles[i].theta){	turtles[i].theta+=d/(1+turnRadius);	}else{	turtles[i].theta-=d/(1+turnRadius);	}	}	if(turtles[i].health>0){	turtles[i].health-=.005;	}else{	for(k=0;k<=3;++k){	p=pi*2/3*k+turtles[i].theta+pi;	s=turtles[i].size*(!k||k==3?2:1);	x2=x1;	y2=y1;	x1=turtles[i].x+Math.sin(p)*s;	y1=turtles[i].y+Math.cos(p)*s;	if(k){	for(m=0;m<12;++m){	x1a=x1+(x2-x1)/12*m;	y1a=y1+(y2-y1)/12*m;	x2a=x1+(x2-x1)/12*(m+1);	y2a=y1+(y2-y1)/12*(m+1);	food.push(new Food(x1a,y1a,x2a,y2a,turtles[i].size/8));	}	}	}	turtles.splice(i,1);	}	if(i>maxPop)turtles.splice(i,1);	}
}
function colorString(arr){	var r = parseInt(arr[0]*15);	var g = parseInt(arr[1]*15);	var b = parseInt(arr[2]*15);	return "#"+r.toString(16)+g.toString(16)+b.toString(16);
}
function interpolateColors(RGB1,RGB2,degree){	w1=degree;	w2=1-w1;	return colorString([w1*RGB1[0]+w2*RGB2[0],w1*RGB1[1]+w2*RGB2[1],w1*RGB1[2]+w2*RGB2[2]]);
}
function rgb(col){	col+=.000001;	var r = parseInt((.5+Math.sin(col)*.5)*16);	var g = parseInt((.5+Math.cos(col)*.5)*16);	var b = parseInt((.5-Math.sin(col)*.5)*16);	return "#"+r.toString(16)+g.toString(16)+b.toString(16);
}
function draw(){	ctx.closePath();	ctx.globalAlpha=1/(1+trails);	ctx.fillStyle="#000";	ctx.fillRect(0,0,cx*2,cy*2);	for(i=0;i<cols;++i){	for(j=0;j<rows;++j){	if(parseInt(terrain[i][j].color*15)==2){	x=i*tileWidth;	y=j*tileHeight;	ctx.globalAlpha=1;	col1=[terrain[i][j].color,terrain[i][j].color,terrain[i][j].color];	c=.75/(1+terrain[i][j].differentNeighbors/2.5);	ctx.fillStyle=colorString([c,c,c]);	ctx.fillRect(x,y,tileWidth+1,tileHeight+1);	}	}	}	if(turtles.length){	ctx.globalAlpha=1;	ctx.strokeStyle="#ff0";	for(i=0;i<food.length;++i){	ctx.lineWidth=food[i].weight;	ctx.beginPath();	ctx.moveTo(food[i].x1,food[i].y1);	ctx.lineTo(food[i].x2,food[i].y2);	ctx.stroke();	}	for(i=0;i<turtles.length;++i){	ctx.lineWidth=1+turtles[i].size/20;	ctx.strokeStyle=interpolateColors([0,1,0],[1,0,0],turtles[i].health);	ctx.fillStyle=ctx.strokeStyle;	for(j=0;j<=3;++j){	p=pi*2/3*j+turtles[i].theta+pi;	s=turtles[i].size*(!j||j==3?2:1);	x=turtles[i].x+Math.sin(p)*s;	y=turtles[i].y+Math.cos(p)*s;	if(!j){	ctx.beginPath();	ctx.moveTo(x,y);	}else{	ctx.lineTo(x,y);	if(j==3){	ctx.globalAlpha=1;	ctx.stroke();	ctx.globalAlpha=.4;	ctx.fill();	}	}	}	}	}
}
function frame(){	doLogic();	draw();	requestAnimationFrame(frame);
}
function setupBoard(){	for(i=0;i<initialFood;++i){	weight=10+rand()*30;	x1=100+rand()*(cx*2-200);	y1=100+rand()*(cy*2-200);	p=pi*2*rand();	x2=x1+Math.sin(p)*weight;	y2=y1+Math.cos(p)*weight;	food.push(new Food(x1,y1,x2,y2,weight));	}	turtles.push(new Turtle(cx,cy*1.5,20,0));	loadControlPoints();	loadTerrain();
}
initVars();
setupBoard();
frame();
Turtles Advanced - Script Codes
Turtles Advanced - Script Codes
Home Page Home
Developer Scott R McGann
Username cantelope
Uploaded November 11, 2022
Rating 4
Size 4,040 Kb
Views 18,216
Do you need developer help for Turtles Advanced?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Scott R McGann (cantelope) Script Codes
Name
3D layers
Road
Modulation
Road2
Background
Vortex
Recursive Trees
Pulse
Jiggleballs
3D Pong
Create amazing Facebook ads with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!