Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body class="bg-inverse">
  <div class="container-fluid tall text-xs-center">
    <div class="row tall flex-items-xs-middle flex-items-xs-center m-0">
      <div id="mainCard" class="card card-block m-0 py-1 px-1">
        <div class="row flex-items-xs-middle flex-items-xs-center">
          <button id="up" type="button" class="gameBtn" disabled></button>
        </div>
        <div class="row flex-items-xs-middle flex-items-xs-center m-0">
          <button id="left" type="button" class="gameBtn" disabled></button>
          <div id="center" class="card card-block p-0">
            <div class="row flex-items-xs-middle flex-items-xs-center h-100 mx-0">
              <div class="col-xs-12">
                <h4 class="m-0"><code class="tag tag-default">--</code></h4>
              </div>
              <div class="col-xs-10">
                <button id="start" class="btn btn-primary btn-block btn-shadow"><span>Standard</span></button>
              </div>
              <div class="col-xs-12">
                <label class="mdl-switch mdl-js-switch ml-3" for="strict">
                  <input type="checkbox" id="strict" class="mdl-switch__input">
                </label>
              </div>
            </div>
          </div>
          <button id="right" type="button" class="gameBtn" disabled></button>
        </div>
        <div class="row flex-items-xs-middle flex-items-xs-center m-0">
          <button id="down" type="button" class="gameBtn" disabled></button>
        </div>
      </div>
    </div>
  </div>
</body>
              
            
!

CSS

              
                .tall {
  height: 100vh;
}

#up {
	width: 0;
	height: 0;
	border-left: 75px solid transparent;
	border-right: 75px solid transparent;
	border-bottom: 150px solid #9f0f17;
  padding: 0px 0px 0px 0px;
  background-color: transparent;
  border-top-color: transparent;
}
#up.light {
  border-bottom-color: #ff4c4c;
}
#up:active {
  outline: none;
}
#up:focus {
  outline: none;
}

#left {
	width: 0;
	height: 0;
	border-top: 75px solid transparent;
	border-right: 150px solid #00a74a;
	border-bottom: 75px solid transparent;
  padding: 0px 0px 0px 0px;
  background-color: transparent;
  border-left-color: transparent;
}
#left.light {
  border-right-color: #13ff7c;
}
#left:active {
  outline: none;
}
#left:focus {
  outline: none;
}

#center {
  width: 150px;
  height: 150px;
  margin: 0px;
}

#right {
	width: 0;
	height: 0;
	border-top: 75px solid transparent;
	border-left: 150px solid #094a8f;
	border-bottom: 75px solid transparent;
  padding: 0px 0px 0px 0px;
  background-color: transparent;
  border-right-color: transparent;
}
#right.light {
  border-left-color: #1c8cff;
}
#right:active {
  outline: none;
}
#right:focus {
  outline: none;
}

#down {
	width: 0;
	height: 0;
	border-left: 75px solid transparent;
	border-right: 75px solid transparent;
	border-top: 150px solid #cca707;
  padding: 0px 0px 0px 0px;
  background-color: transparent;
  border-bottom-color: transparent;
}
#down.light {
  border-top-color: #fed93f;
}
#down:active {
  outline: none;
}
#down:focus {
  outline: none;
}

.btn-shadow {
  box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
  transition: all 0.3s cubic-bezier(.25,.8,.25,1);
}
.btn-shadow:active {
  box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
.btn-shadow:active:focus {
  outline: none;
}
.btn-shadow:focus {
  outline: none;
}
              
            
!

JS

              
                var gameList = [];
var isStrict = false;
var currentIndex = 0;
var timeout = null;

var buttonMap = {
  "up": 0,
  "left": 1,
  "right": 2,
  "down": 3,
  "0": "up",
  "1": "left",
  "2": "right",
  "3": "down"
}

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var frequencies = [329.63,261.63,220,164.81];
var errOsc = audioCtx.createOscillator();
errOsc.type = 'triangle';
errOsc.frequency.value = 110;
errOsc.start(0.0);
var errNode = audioCtx.createGain();
errOsc.connect(errNode);
errNode.gain.value = 0;
errNode.connect(audioCtx.destination);
var ramp = 0.05;
var vol = 0.5;

var oscillators = frequencies.map(function(frq){
  var osc = audioCtx.createOscillator();
  osc.type = 'sine';
  osc.frequency.value = frq;
  osc.start(0.0); //delay optional parameter is mandatory on Safari
  return osc;
});

var gainNodes = oscillators.map(function(osc){
  var g = audioCtx.createGain();
  osc.connect(g);
  g.connect(audioCtx.destination);
  g.gain.value = 0;
  return g;
});

function playTone(num){
  gainNodes[num].gain
    .linearRampToValueAtTime(vol, audioCtx.currentTime + ramp);
};

function stopTones(){
  gainNodes.forEach(function(g){
    g.gain.linearRampToValueAtTime(0, audioCtx.currentTime + ramp);
  });
};

function playErrTone(){
  errNode.gain.linearRampToValueAtTime(vol, audioCtx.currentTime + ramp);
};

function stopErrTone(){
  errNode.gain.linearRampToValueAtTime(0, audioCtx.currentTime + ramp);
};

$(".gameBtn").mousedown(function(e) {
  btnPressed(e.currentTarget.id);
})
$(".gameBtn").mouseup(function(e) {
  stopTones();
  $(e.currentTarget).removeClass("light");
})

$(document).keydown(function(e) {
  var key = e.which;
  switch(key) {
    case 37: // left
      break;
    case 38: // up
      break;
    case 39: // right
      break;
    case 40: // down
      break;
    default: return; // exit this handler for other keys
  }
  e.preventDefault();
  if (key == 37 && !$("#left").is(":disabled")) {
    btnPressed("left");
  } else if (key == 38 && !$("#up").is(":disabled")) {
    btnPressed("up");
  } else if (key == 39 && !$("#right").is(":disabled")) {
    btnPressed("right");
  } else if (key == 40 && !$("#down").is(":disabled")) {
    btnPressed("down");
  }
});
$(document).keyup(function(e) {
  stopTones();
  var key = e.which;
  switch(key) {
    case 37: // left
      break;
    case 38: // up
      break;
    case 39: // right
      break;
    case 40: // down
      break;
    default: return; // exit this handler for other keys
  }
  e.preventDefault();
  if (key == 37 && !$("#left").is(":disabled")) {
    $("#left").removeClass("light");
  } else if (key == 38 && !$("#up").is(":disabled")) {
    $("#up").removeClass("light");
  } else if (key == 39 && !$("#right").is(":disabled")) {
    $("#right").removeClass("light");
  } else if (key == 40 && !$("#down").is(":disabled")) {
    $("#down").removeClass("light");
  }
})

function btnPressed(btn) {
  var id = buttonMap[btn];
  $("#"+btn).addClass("light");
  playTone(id);
  inputCheck(id);
}

$("#start").click(function() {
  gameStart();
})

$("#strict").change(function() {
    if(this.checked) {
      isStrict = true;
      $("#start").find("span").fadeOut(200, function() {
        $("#start").find("span").text("Strict").fadeIn(200);
      });
    } else {
      isStrict = false;
      $("#start").find("span").fadeOut(200, function() {
        $("#start").find("span").text("Standard").fadeIn(200);
      });
    }
});

function gameStart() {
  $(".gameBtn").prop("disabled", true);
  gameList = [];
  currentIndex = 0;
  timeout = null;
  $(".tag").text("--");
  displayBlink(0);
  setTimeout(function() {
    nextTurn();
  },1800)
}

function nextTurn() {
  $(".gameBtn").prop("disabled", true);
  var rand = Math.floor(Math.random() * 4);
  gameList.push(rand);
  $(".tag").text(("0"+String(gameList.length)).slice(-2));
  showTurn(0);
}

function showTurn(i) {
  $(".tag").text(("0"+String(gameList.length)).slice(-2));
  if (i<gameList.length) {
    var button = buttonMap[gameList[i]];
    setTimeout(function() {
      $("#"+button).addClass("light");
      playTone(gameList[i]);
      setTimeout(function() {
        $("#"+button).removeClass("light");
        stopTones();
        if (i+1<gameList.length) {
          showTurn(i+1);
        } else {
          currentIndex = 0;
          playerTurn();
        }
      },300);
    },300);
  }
}

function playerTurn() {
  $(".gameBtn").prop("disabled", false);
  timeout = setTimeout(function() { playerErr(); },5000);
}

function inputCheck(input) {
  clearTimeout(timeout);
  if (input == gameList[currentIndex]) {
    currentIndex++;
    if (currentIndex >= gameList.length) {
      setTimeout(function() {
        if (gameList.length >= 20) {
          playerWin();
        } else {
          nextTurn();
        }
      },300)
    } else {
      timeout = setTimeout(function() { playerErr() },5000);
    }
  } else {
    playerErr(buttonMap[input]);
  }
}

function playerErr(last) {
  $(".gameBtn").prop("disabled", true);
  if (Boolean(last)) {
    $("#"+last).addClass("light");
  }
  $(".tag").text("!!");
  stopTones();
  playErrTone();
  setTimeout(function() {
    stopErrTone();
    $(".gameBtn").removeClass("light");
  },600);
  displayBlink(0);
  setTimeout(function() {
    if (isStrict) {
      gameStart();
    } else {
      showTurn(0);
    }
  },1800);
}

function displayBlink(i) {
  if (i<3) {
    setTimeout(function() {
      $(".tag").css("color", "#818a91");
      setTimeout(function() {
        $(".tag").css("color", "white");
        displayBlink(i+1);
      },300)
    },300)
  }
}

function btnBlink(i, btn) {
  if (i<3) {
    setTimeout(function() {
      var id = buttonMap[btn];
      $("#"+btn).addClass("light");
      playTone(id);
      setTimeout(function() {
        $("#"+btn).removeClass("light");
        stopTones();
        btnBlink(i+1);
      },300)
    },300)
  }
}

function playerWin(last) {
  $(".tag").text("**");
  displayBlink(0);
  btnBlink(0, last);
  setTimeout(function() {
    gameStart();
  },1800);
}
              
            
!
999px

Console