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

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              var dimx = 64,
  dimy = dimx;
var field = new field2D(dimx, dimy);

// turn it white:
field.set(1);

// define an ant:
function ant(x, y, spin) {
  this.x = x;
  this.y = y;
  this.direction = 0;
  // which direction the ant should turn when it finds an active cell:
  this.spin = spin;
}

// make some ants!
var ants = [
  new ant(32, 20, 1),
  new ant(32, 21, 1),
  new ant(64, 40 + random(3), random(2) ? 1 : -1),
  new ant(64, 40 + random(3), random(2) ? 1 : -1),
  new ant(94 + random(3), 20, random(2) ? 1 : -1),
  new ant(94 + random(3), 20, random(2) ? 1 : -1),
];

// how to render the scene
function draw(ctx) {
  // draw the field (by default it fills the canvas):
  field.draw();

  // scale context to field pixel size:
  ctx.scale(1 / dimx, 1 / dimy);

  for (var j = 0; j < ants.length; j++) {
    var ant = ants[j];
    if (ant.spin == 1) {
      ctx.fillStyle = "red";
    } else {
      ctx.fillStyle = "green";
    }
    ctx.fillRect(ant.x, ant.y, 1, 1);
  }
}

function update(dt) {
  for (var j = 0; j < ants.length; j++) {
    var ant = ants[j];

    // move the ant.
    if (ant.direction == 0) {
      // North
      ant.y = (ant.y + 1) % field.height;
    } else if (ant.direction == 1) {
      // West
      ant.x = (ant.x - 1) % field.width;
    } else if (ant.direction == 2) {
      // South
      ant.y = (ant.y - 1) % field.height;
    } else {
      // East
      ant.x = (ant.x + 1) % field.width;
    }

    // apply the rule.
    var state = field.get(ant.x, ant.y);
    if (state == 1) {
      // change the cell state:
      field.set(0, ant.x, ant.y);
      // turn in the preferred direction:
      ant.direction = wrap(ant.direction + ant.spin, 4);
    } else {
      // change the cell state:
      field.set(1, ant.x, ant.y);
      // turn in the opposite direction:
      ant.direction = wrap(ant.direction - ant.spin, 4);
    }
  }
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console