Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

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

              
                <div id="container">
  <div id="header">
  <div id="start">Start</div>
  <div><input type="checkbox" id="strict"><label for="strict" id="strict-label">Strict</label></div>
  </div>
  <div id="buttons">
    <div id="button0" class="button disabled"></div>
    <div id="button1" class="button disabled"></div>
    <div id="button2" class="button disabled"></div>
    <div id="button3" class="button disabled"></div>
  </div>
  <div id="footer">
    <div id="count">0</div>
    <div id="status"></div>
  </div>
</div>
              
            
!

CSS

              
                #container {
  width: 50%;
  margin-left: 25%;
  text-align: center;
  font-size: 1.5em;
  font-family: Raleway, sans-serif;
}

#header, #footer {
  display: flex;
  justify-content: center;
}
#header div, #footer div {
  flex: 0 0 125px;
}

#status, #count {
  display: none;
}

#strict:checked + label {
  background: #D00;
  color: white;
  padding: 0 4px;
}

#strict {
  display: none;
}

#buttons {
/*   background: #222121; */
  margin-left: calc(50% - 200px);
  width: 400px;
  height: 400px;
}

#buttons div, label, #start, #strict {
  cursor: pointer;
  transition: all .1s;
}

#buttons div {
  float: left;
  margin: 25px;
  height: 150px;
  width: 150px;
  border-radius: 70px;
}

#buttons div.activated {
  transform: scale(1.05, 1.05);
  border-radius: 80px;
}

#buttons div.disabled {
  cursor: default;
}

#button0 {
  background: #0B0;
}

#button1 {
  background: #B00;
}

#button2 {
  background: #DD0;
}

#button3 {
  background: #00B;
}

#button0.activated {
  background: #0E0;
}

#button1.activated {
  background: #E00;
}

#button2.activated {
  background: #EFEF00;
}

#button3.activated {
  background: #00E;
}
              
            
!

JS

              
                // oscillator code courtesy of https://stackoverflow.com/questions/6343450/generating-sound-on-the-fly-with-javascript-html5
// one context per document
var context = new (window.AudioContext || window.webkitAudioContext)();

function playTone(x, type = 'square') {
  var osc = context.createOscillator();
  var vol = context.createGain();
  
  osc.type = type; // this is the default - also square, sawtooth, triangle
  osc.frequency.value = x * 75 + 400; // Hz
  osc.connect(vol); // connect it to the gainNode
  vol.connect(context.destination); // connect vol to context destination
  vol.gain.value = 0.01; // from 0 to 1, 1 full volume, 0 is muted
  osc.start(); 
  osc.stop(context.currentTime + .4) // stop after 400ms;
}


$(".button").on("mousedown", function() { 
  if ($(this).hasClass("disabled")) return;
  button(this.id); 
});

var usersTurn, sequence, userProgress, simonProgress, strictMode;

$("#start").click(init);

function init() {
  sequence = [];
  for (var i = 0; i < 20; i++) {
    sequence.push( "button" + Math.floor(Math.random() * 4) );
  }
  $("#count").show(800);
  strictMode = document.getElementById("strict").checked;
  usersTurn = false;
  userProgress = 0;
  simonProgress = 0;
  $(".button").addClass("disabled");
  simonSays();
}

function play(b) {
  $("#" + b).addClass("activated");
    window.setTimeout(function() {$(".button").removeClass("activated")}, 400);
    playTone(b[6]);
}

function button(b) {
  
  if( sequence[userProgress] == b ) {
    play(b);
    userProgress++;
    if (userProgress == 20) {
      $("#status").text("You win!");
      $("#status").show();
      $("#status").hide(4000);
      init();
    } else if (userProgress == simonProgress) {
     endTurn();
    }
  } else {
    if (strictMode) init();
    else {
      playTone(-4, 'sawtooth');
      simonProgress--;
      $("#status").text("Wrong!");
      $("#status").show();
      $("#status").hide(1200);
      endTurn();
    }
  }
}

function endTurn() {
  usersTurn = false;
  userProgress = 0;
  $(".button").addClass("disabled");
  window.setTimeout(simonSays, 1000);
}

function simonSays() {
  $("#count").text(simonProgress + 1);
  var delay = 800 - simonProgress * 35;
  say = 0;
  var simon = setInterval( function() {
    play(sequence[say]);
    if (say == simonProgress) {
      simonProgress++;
      usersTurn = true;
      $(".button").removeClass("disabled");
      clearInterval( simon );
      return;
    } 
    say++;
  }, delay);
}
              
            
!
999px

Console