cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              
//initialGeneDataBase
//H:Hair B:body E:Eye  M:mouth L:Leg 
var geneDataBase= "HBEML".split("");
var angle1 = Math.PI / 12;

function pick(arr) {
  return arr[random(arr.length)];
}

function geno_make() {
  var g = "";
  for (var i = 0; i < 25; i++) {
    g += pick(geneDataBase);
  }
  return g;
}

var pop = [];
for (var i = 0; i < 9; i++) {
  pop[i] = geno_make();
}

// agents get infro from geno
var creature = [];
function make_creature(code) {
  var a = {
    pos: new vec2(random(), random()),
    vel: vec2.random(0.001),
    size: (random() + 1) / 80,
    energy: random(),
    gene: code,
    head_amount: 0,
    head_shape: 0,
    head_pos:[],
    head_color_r: 0,
    head_color_g: 0,
    head_color_b: 0,
    body_amount: 0,
    body_shape: 0,
    body_pos:[],
    body_color_r: 0,
    body_color_g: 0,
    body_color_b: 0,
    eye_amount: 0,
    eye_shape: 0,
    eye_pos:[],
    eye_color_r: 0,
    eye_color_g: 0,
    eye_color_b: 0,
    mouth_amount: 0,
    mouth_shape: 0,
    mouth_pos:[],
    mouth_color_r: 0,
    mouth_color_g: 0,
    mouth_color_b: 0,
    leg_amount: 0,
    leg_shape: 0,
    leg_pos:[],
    leg_color_r: 0,
    leg_color_g: 0,
    leg_color_b: 0,
    leg2_amount: 0,
    leg2_shape: 0,
    leg2_pos:[],
    back_color_r: random(),
    back_color_g: random(),
    back_color_b: random(),
  };
  creatureGenerator(a);
  creature.push(a);
  return a;
}

  
//assign the gene to the creature
for (var i = 0; i <  pop.length; i++) {
   var code = pop[i];
   var a = make_creature(code);  
}

function cleanPrevious(a){
     a.head_amount = 0;
     a.body_amount = 0;
     a.eye_amount =0;
     a.mouth_amount = 0;
     a.leg_amount =0;
     a.leg2_amount =0;
}
function creatureGenerator(a){
    for (var i = 0; i < a.gene.length; i++) {
    var c = a.gene.substr(i, 1);
      if (c == "H") {//head
        // only one hand
         a.head_amount ++;
         a.head_shape = random(3);
         var temp =  new vec2(0.8, 0.3);
         temp.rotate(random()*1.2);
         a.head_pos.push(temp); 
         a.head_color_r = random();
         a.head_color_g = random();
         a.head_color_b = random();   
         a.back_color_r = random();
         a.back_color_g = random();
         a.back_color_b = random(); 
      }else if(c =="B"){//body
       a.body_amount ++;
        var temp =  new vec2(0.5, 0.4);
       temp.rotate(random()*-1);
       a.body_pos.push(temp); 
       a.body_shape = random(2);
       a.body_color_r = random();
       a.body_color_g = random();
       a.body_color_b = random();
     }else if(c =="E"){//Eye     
         a.eye_amount++;
         a.eye_shape = random(3);
         var temp = new vec2(0.3, 0.3);
         temp.rotate(random()*1.3);
         a.eye_pos.push(temp); 
         a.eye_color_r = random();
         a.eye_color_g = random();
         a.eye_color_b = random();        
     }else if(c =="M"){//mouth
         a.mouth_amount++;
         a.mouth_shape = random(3);
         var temp = new vec2(0.1, -0.2);
         temp.rotate(random()*0.6);
         a.mouth_pos.push(temp); 
         a.mouth_color_r = random();
         a.mouth_color_g = random();
         a.mouth_color_b = random();  
      }else if(c =="L"){//Leg
         a.leg_amount = a.leg_amount +2;
         a.leg2_amount= a.leg2_amount +2;
         a.leg_shape = random(3); 
         a.leg_color_r = random();
         a.leg_color_g = random();
         a.leg_color_b = random(); 
        for(var j =0;j<2;j++){
         var temp = new vec2(0.5, -0.2);
         temp.rotate(random()*-0.7);
         a.leg_pos.push(temp);
        var temp2 = new vec2(0.8,temp[1]-0.05);
         // temp2.rotate(random(-10,-10));
        //temp2.rotate(random()*-10);
        a.leg2_pos.push(temp2); 
       }
      }
     
   }
  
}

function mouse(e, p) {
 if (e == "down") {
    t = 0;
    // convert mouse position p to choice of biomorph:
    var col = Math.floor(p[0]*3);
    var row = Math.floor(p[1]*3);
    var choice = row*3 + col;
    var parent = creature[choice].gene;
    // make a new population:
    for (var i=0; i<9; i++) {
     
      var child = parent;
      // TODO: mutations;
      var cut = random(child.length);
      var a = child.slice(0, cut);
      var cut = random(child.length);
      var b = child.slice(cut+1);
      var c = pick(geneDataBase);
      child = b+c+a;    
      creature[i].gene = child;
      cleanPrevious(creature[i]);
      creatureGenerator(creature[i]);
    }   
  }
}

function update() {

}

function draw() {
  // draw each agent:
   for (var i = 0; i < creature.length; i++) {
    var col = i % 3;
    var row = Math.floor(i/3);
    var code = creature[i].gene;
   var a = creature[i];
    // console.log(creature[i].head_amount);
   // write(code);
     write(code);
      draw2D.push()
      .translate((col + 0.35)/3, (row + 0.35)/3)
      .scale(0.1);
     //background
        draw2D.color(a.back_color_r,a.back_color_g,a.back_color_b).rect(0.5,0.5, 3.5); 
       //fACE
        for(var j = 0; j < a.head_amount;j++ ){ 
            if(a.body_shape ==0)
            draw2D.color(a.body_color_r,a.body_color_g,a.body_color_b).rect(a.body_pos[j], 0.5);
           if(a.body_shape ==1)
            draw2D.color(a.body_color_r,a.body_color_g,a.body_color_b).circle(a.body_pos[j], 0.6);
        }
       //HAIR interesting to play with color
       draw2D.color(random(),random(),random());
       for(var j = 0; j < a.head_amount;j++ ){   
         if(a.head_shape ==0)
            draw2D.color(a.head_color_r,a.head_color_g,a.head_color_b).rect(a.head_pos[j], 0.4);
          //draw2D.rect(a.head_pos[j], 0.4);
          if(a.head_shape ==1)
            draw2D.color(a.head_color_r,a.head_color_g,a.head_color_b).circle(a.head_pos[j], 0.4);
         // draw2D.color(random(),random(),random()).circle(a.head_pos[j], 0.4);
            // draw2D.circle(a.head_pos[j], 0.4);
         if(a.head_shape ==2)
         //draw2D.color(a.head_color_r,a.head_color_g,a.head_color_b).triangle(a.head_pos[j], 0.4);
        // draw2D.color(random(),random(),random()).triangle(a.head_pos[j], 0.4);
         draw2D.triangle(a.head_pos[j], 0.4);
       }
    
      draw2D.push().translate(0.25,0.25).scale(0.5);
     //eye
     for(var j = 0; j < a.eye_amount;j++ ){       
        	if(a.eye_shape ==0)
         draw2D.color(a.eye_color_r,a.eye_color_g,a.eye_color_b).rect(a.eye_pos[j], 0.3);
          if(a.head_shape ==1)
         draw2D.color(a.eye_color_r,a.eye_color_g,a.eye_color_b).circle(a.eye_pos[j], 0.3);
         if(a.head_shape ==2)
         draw2D.color(a.eye_color_r,a.eye_color_g,a.eye_color_b).triangle(a.eye_pos[j], 0.3);
       }
       //mouse
        for(var j = 0; j < a.mouth_amount;j++ ){   
          if(a.mouth_shape == 0)
          draw2D.color(a.mouth_color_r,a.mouth_color_g,a.mouth_color_b).rect(a.mouth_pos[j], 0.3);
          if(a.mouth_shape ==1)
          draw2D.color(a.mouth_color_r,a.mouth_color_g,a.mouth_color_b).circle(a.mouth_pos[j], 0.3);
           if(a.mouth_shape ==2)
          draw2D.color(a.mouth_color_r,a.mouth_color_g,a.mouth_color_b).triangle(a.mouth_pos[j], 0.3);
       } //draw2D.color(0,0,0).rect(0.2,0.2,0.3);
     //Left Leg
      draw2D.pop();
       for(var j = 0; j < a.leg_amount;j++ ){   
         if(a.leg_shape ==0)
            draw2D.color(a.body_color_r,a.body_color_g,a.body_color_b).rect(a.leg_pos[j], 0.1);
          if(a.leg_shape ==1)
            draw2D.color(a.body_color_r,a.body_color_g,a.body_color_b).circle(a.leg_pos[j], 0.1);
         if(a.leg_shape ==2)
         draw2D.color(a.body_color_r,a.body_color_g,a.body_color_b).triangle(a.leg_pos[j], 0.1);
       }
     //right Leg
       for(var j = 0; j < a.leg2_amount;j++ ){   
         if(a.leg_shape ==0)
            draw2D.color(a.body_color_r,a.body_color_g,a.body_color_b).rect(a.leg2_pos[j], 0.1);
          if(a.leg_shape ==1)
            draw2D.color(a.body_color_r,a.body_color_g,a.body_color_b).circle(a.leg2_pos[j], 0.1);
         if(a.leg_shape ==2)
         draw2D.color(a.body_color_r,a.body_color_g,a.body_color_b).triangle(a.leg2_pos[j], 0.1);
       }
   draw2D.pop();
   } 
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console