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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="wrapper">
  <div id="green" class="colorButtons">
  </div><div id="red" class="colorButtons">
  </div><br><div id="yellow" class="colorButtons">
  </div><div id="blue" class="colorButtons">
  </div>
  <div id="controls">
    <div id="simonTitle">Simon<span>&reg;</span></div>
    <div id="mobileLabel"><i>MOBILE</i></div>
    <div id="counter" class="topRow"><p></p></div>
    <div id="startButton" class="topRow"><p>START</p></div>
    <div id="strictButton" class="topRow"><p>STRICT</p></div>
    <br>
    <button id="resetButton">RESET</button>
    <br>
    <p class="bottomRow">OFF</p><p id="switchOff" class="bottomRow"></p><p id="switchOn" class="bottomRow"></p><p class="bottomRow">ON</p>
  </div>
</div>
            
          
!
            
              #wrapper { margin-top: 60px;
           margin-bottom: 0px; 
           margin-left: auto;
           margin-right: auto;
           font-family: "MS Gothic", monospace;
           line-height: 0px;
           width: 645px;
           height: 660px;
           position: relative; }

#wrapper div { display: inline-block;
               height: 285px;
               width: 285px; }

#green, #red, #yellow, #blue {
  -webkit-box-shadow: 2px 2px 30px rgba(0, 0, 0, 1);
  -moz-box-shadow: 2px 2px 30px rgba(0, 0, 0, 1);
  box-shadow: 2px 2px 30px rgba(0, 0, 0, 1);
}

#green { background-color: green;
         border: 25px solid #222;
         border-radius: 320px 0 0 0; }

.green { background-color: #0C0 !important; }

#red { background-color: #A00;
       border-top: 25px solid #222;
       border-bottom: 25px solid #222;
       border-right: 25px solid #222;
       border-radius: 0 320px 0 0; }

.red { background-color: #F00 !important; }

#yellow { background-color: #BA8000;
          border-left: 25px solid #222;
          border-bottom: 25px solid #222;
          border-right: 25px solid #222;
          border-radius: 0 0 0 320px; }

.yellow { background-color: #DCBF00 !important; }

#blue { background-color: #0050AA;
        border-right: 25px solid #222;
        border-bottom: 25px solid #222;
        border-radius: 0 0 320px 0; }

.blue { background-color: #0060FF !important; }

#controls { background-color: white;
            color: #000;
            text-align: center;
            border: 15px solid #222;
            border-radius: 50%;
            font-size: 30px;
            position: absolute;
            transform: translate(-50%, -50%);
            top: 49.5%;
			      left: 50%; }

#wrapper #simonTitle { margin-top: 78px;
                       margin-bottom: 0px;
                       margin-left: 12px; 
                       font-size: 64px;
                       font-family: "Alfa Slab One";
                       height: 35px;
                       padding-bottom: 0px;
                       letter-spacing: 2px; }

#wrapper #mobileLabel {
  display: none;
}

span { font-size: 18px;
       padding-bottom: 0px; }

#wrapper #counter { margin-top: 15px;
                    width: 65px;
                    height: 40px;
                    font-size: 25px;
                    line-height: 35px;
                    letter-spacing: 1px;
                    font-family: "Orbitron";
                    background-color: #300;
                    color: #900;
                    border: 5px solid black;
                    border-radius: 10px; }

#wrapper #startButton, #wrapper #strictButton {
  margin-top: 15px;
  width: 60px;
  height: 38px;
  border: 5px solid black;
  border-radius: 50%;
  line-height: 14px;
  color: #FFF;
  font-size: 12px;
  font-weight: bold;
}

#wrapper #startButton { background-color: #400; }

#wrapper #strictButton { background-color: #EEB200; }

#wrapper #resetButton { background-color: black;
                        color: white;
                        width: 56px;
                        height: 22px;
                        border-radius: 6px;
                        margin-top: 14px; }

#switchOff, #switchOn {
  width: 15px;
  height: 15px;
  margin-bottom: 0px;
}

#switchOff {
  background-color: #05C;
  border: 5px solid black;
  margin-left: 10px;
}

#switchOn {
  background-color: #000;
  border: 5px solid black;
  margin-right: 10px;
}

.topRow { display: inline-block;
          margin-left: 8px;
          vertical-align: middle; }

.bottomRow { display: inline-block;
             font-size: 32px;
             margin-top: 17px; }

/*********************************************
*************** MEDIA QUERIES ****************
**********************************************
******* FOR SMALL LAPTOPS AND TABLETS ********/

@media screen and (min-width: 500px) and (max-width: 1500px){
  #wrapper {
    margin-top: 100px;
    margin-bottom: 0px;
    margin-left: auto;
    margin-right: auto;
    width: 445px;
    height: 600px;
  }
  
  #wrapper div {
    width: 200px;
    height: 200px;
    display: inline-block;
  }
  
  #green {
    border: 15px solid #222;
    border-radius: 250px 0 0 0;
  }
  
  #red {
    border-top: 15px solid #222;
    border-bottom: 15px solid #222;
    border-right: 15px solid #222;
    border-radius: 0 270px 0 0;
  }
  
  #yellow {
    border-left: 15px solid #222;
    border-bottom: 15px solid #222;
    border-right: 15px solid #222;
    border-radius: 0 0 0 250px;
  }
  
  #blue {
    border-right: 15px solid #222;
    border-bottom: 15px solid #222;
    border-radius: 0 0 250px 0;
  }
  
  #controls {
    border: 10px solid #222;
    border-radius: 50%;
    font-size: 15px;
    position: absolute;
    transform: translate(-50%, -50%);
    top: 38.0%;
    left: 50.0%; }
  
  #wrapper #simonTitle {
    margin-top: 55px;
    margin-bottom: 0px;
    margin-left: 6px; 
    font-size: 40px;
    font-family: "Alfa Slab One";
    height: 10px;
    padding-bottom: 0px;
    letter-spacing: 2px; }
  
  #wrapper #counter {
    margin-top: 15px;
    width: 40px;
    height: 25px;
    font-size: 15px;
    line-height: 22px;
    font-family: "Orbitron";
    background-color: #300;
    color: #900;
    border: 3px solid black;
    border-radius: 8px; }
  
  #wrapper #startButton, #wrapper #strictButton {
    margin-top: 15px;
    width: 40px;
    height: 25px;
    border: 3px solid black;
    border-radius: 50%;
    line-height: 6px;
    color: #FFF;
    font-size: 10px;
    font-weight: bold;
  }
  
  #wrapper #resetButton {
    background-color: black;
    color: white;
    width: 56px;
    height: 22px;
    font-size: 10px;
    border-radius: 6px;
    margin-top: 11px; }

  #switchOff, #switchOn {
    width: 9px;
    height: 9px;
    margin-bottom: 0px;
  }

  #switchOff {
    margin-left: 7px;
  }

  #switchOn {
    margin-right: 7px;
  }

  .topRow { display: inline-block;
            margin-left: 8px;
            vertical-align: middle; }

  .bottomRow { display: inline-block;
               font-size: 20px;
               margin-top: 12px; }
}

/************** FOR MOBILE PHONES **************/

@media screen and (max-width: 500px){
  #wrapper {
    margin-top: 20px;
    margin-bottom: 0px;
    margin-left: auto;
    margin-right: auto;
    width: 280px;
    height: 450px;
  }
  
  #wrapper div {
    width: 125px;
    height: 210px;
    display: inline-block;
  }
  
  #green, #red, #yellow, #blue {
    -webkit-box-shadow: 0px 0px 0px;
    -moz-box-shadow: 0px 0px 0px;
    box-shadow: 0px 0px 0px;
  }
  
  #green {
    border: 10px solid #222;
    border-radius: 40px 0 0 0;
  }
  
  #red {
    border-top: 10px solid #222;
    border-bottom: 10px solid #222;
    border-right: 10px solid #222;
    border-radius: 0 40px 0 0;
  }
  
  #yellow {
    border-left: 10px solid #222;
    border-bottom: 10px solid #222;
    border-right: 10px solid #222;
    border-radius: 0 0 0 40px;
  }
  
  #blue {
    border-right: 10px solid #222;
    border-bottom: 10px solid #222;
    border-radius: 0 0 40px 0;
  }
  
  #controls {
    border: 8px solid #222;
    border-radius: 20px 20px 20px 20px;
    font-size: 15px;
    background-color: #000;
    color: #FFF;
    position: absolute;
    transform: translate(-50%, -50%);
    top: 50.2%;
    left: 50.0%; }
  
  #wrapper #controls {
    width: 130px;
    height: 320px;
  }
  
  #wrapper #simonTitle {
    margin-top: 30px;
    margin-bottom: 0px;
    margin-left: 6px; 
    font-size: 25px;
    font-family: "Alfa Slab One";
    height: 10px;
    padding-bottom: 0px;
    letter-spacing: 2px; }
  
  #wrapper #mobileLabel {
    display: block;
    margin-top: 15px;
    margin-bottom: 0px; 
    font-size: 15px;
    font-family: "Alfa Slab One";
    height: 10px;
    padding-bottom: 0px;
    letter-spacing: 2px; }
  
  #wrapper #counter {
    margin-top: 15px;
    width: 70px;
    height: 30px;
    font-size: 17px;
    letter-spacing: 2px;
    line-height: 34px;
    font-family: "Orbitron";
    background-color: #300;
    color: #900;
    border: 2px solid #FFF;
    border-radius: 8px; }
  
  #wrapper #startButton, #wrapper #strictButton {
    margin-top: 15px;
    width: 70px;
    height: 35px;
    border: 2px solid #FFF;
    border-radius: 50%;
    line-height: 6px;
    color: #FFF;
    font-size: 15px;
    font-weight: bold;
  }
  
  #wrapper #resetButton {
    margin-top: 15px;
    background-color: black;
    color: white;
    width: 70px;
    height: 37px;
    font-size: 15px;
    padding-bottom: 3px;
    border-radius: 6px; }

  #switchOff, #switchOn {
    height: 20px;
    border: 2px solid #FFF;
  }

  #switchOff {
    margin-left: 2px;
    width: 35px;
  }

  #switchOn {
    margin-right: 2px;
    width: 28px;
  }

  .topRow { display: block;
            margin-left: 0px;
            vertical-align: middle; }

  .bottomRow {
    display: inline-block;
    font-size: 10px;
    margin-top: 15px;
    margin-bottom: 0px; }
}
            
          
!
            
              $(function(){
  var colorIDs = ["green", "red", "yellow", "blue"];
  var buttonSounds = ["https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/green.mp3",
                      "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/red.mp3",
                      "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/yellow.mp3",
                      "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/blue.mp3"];
  
  var errorSound = new Audio("https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/errorBuzzer.mp3");
  var winSound = new Audio("https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/win.mp3");
  var resetSound = new Audio("https://s3-us-west-2.amazonaws.com/s.cdpn.io/1321144/reset.mp3");
  
  var colorSequence = [];
  var aiSequence = [];
  var playerSequence = [];
  var sequenceCount = 0;
  var strictOn = false;
  var sequenceCorrect = true;
  var gameOn = false;
  
  var MAX_LEVEL = 20;
  
  $("#switchOn").click(function(){
    $("#switchOn").css("background-color", "#05C");
    $("#switchOff").css("background-color", "#000");
    counterDisplay("initialize");
    turnGameOn();
  });
  
  $("#switchOff").click(function(){
    turnGameOff();
  });
  
  function turnGameOff()
  {
    $("#switchOn").css("background-color", "#000");
    $("#switchOff").css("background-color", "#05C");
    counterDisplay("off");
    
    resetSequenceParameters();
    
    playerSequence = [];    
    strictOn = false;
    gameOn = false;
    
    $("#startButton").off("click");
    $("#startButton").css("color", "#FFF");
    $("#strictButton").off("click");
    $("#strictButton").css("color", "#FFF");
    $("#resetButton").off("click");

    $("#green").off("click");
    $("#red").off("click");
    $("#yellow").off("click");
    $("#blue").off("click");
  }
  
  function turnGameOn()
  {
    $("#startButton").click(function(){
      $(this).css("color", "#E00");
      $(this).off("click");
      activateColorButtons();
      activateResetButton();
      runAISequence();
    }); // End of startButton click handler
    
    // Code for strict mode if strict button is on
    $("#strictButton").click(function(){
      strictOn = !strictOn;
      
      if(strictOn)
        $("#strictButton").css("color", "#E00");
      else
        $("#strictButton").css("color", "#FFF");
    });
  } // End of turnGameOn() function
  
  // Runs the AI sequence
  function runAISequence()
  {
    if(sequenceCorrect){
      sequenceCount++;
      showSequenceCount(sequenceCount);
      var randomNumber = generateRandomNumber();
      var randomColor = colorIDs[randomNumber];
      aiSequence.push("#" + randomColor);
      colorSequence.push(randomColor);
    }
    
    var index = 0;
    
    // Begin ai sequence
    var intervalID = setInterval(function(){
      playSound(colorSequence[index]);
      animateButtons(colorSequence[index]);
      index++;
      
      if(index == aiSequence.length){
        clearInterval(intervalID);
      }
    }, 1000);
  } // End of runAISequence function
  
  function activateResetButton()
  {
    // Code for reset button; takes the game back to 
    $("#resetButton").click(function(){
      resetSequenceParameters();
      playerSequence = [];
      counterDisplay("reset");
      resetSound.play();

      setTimeout(function(){
        runAISequence();
      }, 1000);
    });
  }
  
  /* Activates the clor buttons and handles the AI and player button sequences */
  function activateColorButtons()
  {
    // Begin player sequence
    $(".colorButtons").click(function(){
      var divColor = $(this).attr("id");
      playSound(divColor);
      animateButtons(divColor);
      playerSequence.push("#" + divColor);
      
      var inputCorrect = checkUserInput();
      
      // Player gets the sequence wrong (presses a wrong button)
      if(inputCorrect == false){
        counterDisplay("error");
        errorSound.play();
        sequenceCorrect = false;
        playerSequence = [];
        
        // if strict mode is on, re-start with a new sequence
        if(strictOn){
          resetSequenceParameters();
        }
        
        setTimeout(function(){
          showSequenceCount(sequenceCount);
          runAISequence();
        }, 2000);
      }
      
      // Player gets the sequence right
      if(inputCorrect == true){
        sequenceCorrect = true;
        
        if(playerSequence.length == aiSequence.length && sequenceCorrect == true){
          playerSequence = [];
          
          if(sequenceCount == MAX_LEVEL){
            counterDisplay("win");
            resetSequenceParameters();
            winSound.play();
            strictOn = false;
            $("#strictButton").css("color", "#FFF");
            setTimeout(function(){
              runAISequence();
            }, 5000);
          }
          else{
            setTimeout(function(){
              runAISequence();
            }, 1000);
          } // End of nested if-else
        } // End of outer if
      } // End of if(inputCorrect == true)
    }); // End of .colorButtons click handler
  } // End of activateColorButtons() function
  
  // Checks user sequence
  function checkUserInput()
  {
    if(playerSequence.length > aiSequence.length){
      return false;
    }
    
    for(var index = 0; index < playerSequence.length; index++){
      if(playerSequence[index] != aiSequence[index]){
        return false;
      }
    }
    
    return true;
  } // End of checkUserInput() function
  
  // "Lights up" each button
  function animateButtons(color)
  {
    $("#" + color).addClass(color);
    setTimeout(function(){
      $("#" + color).removeClass(color);
    }, 500);
  }
  
  // Plays a sound corresponding to each colour
  function playSound(color)
  {
    switch(color){
      case "green":
        var sound = new Audio(buttonSounds[0]);
        sound.play();
        break;
      case "red":
        var sound = new Audio(buttonSounds[1]);
        sound.play();
        break;
      case "yellow":
        var sound = new Audio(buttonSounds[2]);
        sound.play();
        break;
      case "blue":
        var sound = new Audio(buttonSounds[3]);
        sound.play();
    }
  }
  
  // Updates the step count display
  function counterDisplay(message)
  {
    switch(message){
      case "off":
        $("#counter").html("");
        break;
      case "initialize":
        $("#counter").html("--");
        break;
      case "error":
        $("#counter").html("ERR");
        break;
      case "reset":
        $("#counter").html("RS");
        break;
      case "win":
        $("#counter").html("WIN");
    }
  }
  
  // Shows the current steps in the step count display
  function showSequenceCount(sequence)
  {
    if(sequence < 10)
      $("#counter").html("0" + sequence);
    else
      $("#counter").html("" + sequence);
  }
  
  function generateRandomNumber()
  {
    return Math.floor(Math.random() * colorIDs.length);
  }
  
  function resetSequenceParameters()
  {
    aiSequence = [];
    colorSequence = [];
    sequenceCount = 0;
    sequenceCorrect = true;
  }
  
});
            
          
!
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.

Console