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 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.

            
              #cc
  max-width 1536px
  margin 0 auto
            
          
!
            
              var w = 1536;
var h = 864;
var canvas;
var bugs = [];
var wires = [];
var pins = [];
var epoch = 0;
var pindiam = 32;
var bugs = [];
var updated;
var speed = .1;
var spawned;
var mincooldown = 4000;
var target;
var cmd = 0;
var collide = .05;

function setup() {
  canvas = createCanvas(w,h);
  canvas.parent("cc");
  textFont("Montserrat");
  rectMode(CENTER);
  ellipseMode(CENTER);
  updated = millis();
}

function draw() {
  let delta = millis() - updated;
  updated = millis();
  strokeWeight(16);
  stroke(0);
  fill(255);
  rect(w/2, h/2, w, h);
  switch (epoch) {
    case 0:
      textAlign(CENTER, CENTER);
      noStroke();
      fill(0);
      textSize(144);
      text("Traverse!", w/2, h/2);
      textSize(36);
      text("Press any key to continue...", w/2, h*5/8);
      break;
    case 1:
      strokeWeight(4);
      for (let i in wires) {
        let a = pins[wires[i][0]];
        let b = pins[wires[i][1]];
        line(px(a), py(a), px(b), py(b));
      }
      for (let i in pins) {
        fill(255);
        if (i == target) fill(0);
        ellipse(px(pins[i]), py(pins[i]), pindiam, pindiam);
      }
      let playerx, playery;
      for (let i in bugs) {
        let a = pins[wires[bugs[i][1]][bugs[i][2]]];
        let b = pins[wires[bugs[i][1]][1-bugs[i][2]]];
        let traj = createVector(px(b)-px(a), py(b)-py(a));
        bugs[i][3] += speed*delta;
        traj.normalize();
        traj.mult(bugs[i][3]);
        if (traj.mag() > Math.hypot(px(a)-px(b), py(a)-py(b))) {
          if (i == 0) {
            if (wires[bugs[i][1]][1-bugs[i][2]] == target)
              lvlup();
            else
              epoch = 2;
            break;
          } else {
            bugs.splice(i, 1);
            genbug();
            continue;
          }
        }
        let bugx = px(a)+traj.x;
        let bugy = py(a)+traj.y;
        if (i == 0) {
          playerx = bugx;
          playery = bugy;
        } else if (Math.hypot(bugx-playerx, bugy-playery) < pindiam) {
          epoch = 2;
          break;
        }
        push();
        translate(bugx, bugy);
        rotate(traj.heading());
        drawbug(bugs[i][0]);
        pop();
      }
      let a = pins[wires[bugs[0][1]][bugs[0][2]]];
      let b = pins[wires[bugs[0][1]][1-bugs[0][2]]];
      for (let j in wires) {
        let c = pins[wires[j][0]];
        let d = pins[wires[j][1]];
        if (Math.hypot(playerx-px(c), playery-py(c)) + Math.hypot(playerx-px(d), playery-py(d)) - Math.hypot(px(c)-px(d), py(c)-py(d)) < collide && cmd && bugs[0][1] != j) {
          console.log(j);
          bugs[0][1] = int(j);
          bugs[0][3] = Math.hypot(playerx-px(c), playery-py(c));
          if ((a < b) ^ (cmd > 0) ^ (a < c) ^ (b < c))
            bugs[0][2] = 0;
          else {
            bugs[0][2] = 1;
            bugs[0][3] = Math.hypot(px(c)-px(d), py(c)-py(d)) - bugs[0][3];
          }
          cmd = 0;
        }
      }
      if (wires.length > bugs.length && millis() > spawned+mincooldown) {
        genbug();
        spawned = millis();
      }
      break;
    case 2:
      noStroke();
      fill(0);
      textSize(144);
      text("Game Over", w/2, h/2);
      textSize(36);
      text("You got entangled in a mesh of "+wires.length+" wires.", w/2, h*5/8);
      break;
  }
}

function drawbug(t) {
  if (!t)
    fill(64);
  else
    fill(192)
  strokeWeight(4);
  rect(0, 0, pindiam*3/2, pindiam, pindiam/4);
}

function keyPressed() {
  if (epoch != 1) {
    epoch = 1;
    bugs = [];
    wires = [];
    pins = [];
    genwire(0);
    genwire(1);
    genbug();
    target = Math.floor(Math.random()*pins.length);
    spawned = millis();
  } else {
    if (keyCode == LEFT_ARROW) {
      cmd = -1;
    } else if (keyCode == RIGHT_ARROW) {
      cmd = 1;
    } else if (keyCode == DOWN_ARROW) {
      cmd = 0;
      bugs[0][2] = 1-bugs[0][2];
      let a = pins[wires[bugs[0][1]][bugs[0][2]]];
      let b = pins[wires[bugs[0][1]][1-bugs[0][2]]];
      bugs[0][3] = Math.hypot(px(a)-px(b), py(a)-py(b)) - bugs[0][3];
    } else if (keyCode == UP_ARROW) {
      cmd = 0;
    }
  }
}

function px(coor) {
  if (coor < w) return coor;
  if (coor < w+h) return w;
  if (coor < 2*w+h) return 2*w+h-coor;
  return 0;
}

function py(coor) {
  if (coor < w) return 0;
  if (coor < w+h) return coor-w;
  if (coor < 2*w+h) return h;
  return 2*(w+h)-coor;
}

function genwire(orientation) {
  let wireps;
  if (orientation < .5) {
    wireps = [genpin(0), genpin(2)];
  } else {
    wireps = [genpin(1), genpin(3)];
  }
  wires.push(wireps);
  console.log(wires);
}

function genpin(edge) {
  let peri = [w, h, w, h];
  let base = [0, w, w+h, 2*w+h]
  let pincoor = Math.random()*peri[edge]+base[edge];
  let pini = -1;
  for (let i in pins) {
    if (Math.abs(pincoor-pins[i]) < pindiam) {
      return i;
    }
  }
  pini = pins.length;
  pins.push(pincoor);
  console.log(pins);
  return pini;
}

function genbug() {
  let t = bugs.length;
  let w = Math.floor(Math.random()*wires.length);
  let p = Math.floor(Math.random()*2);
  let bugarr = [t, w, p, 0];
  bugs.push(bugarr);
  console.log(bugs);
}

function lvlup() {
  genwire(Math.random());
  target = Math.floor(Math.random()*pins.length);
  bugs = [];
  genbug();
  spawned = millis();
}
            
          
!
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