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.

            
              <html>
<head>
</head>
<body>
</body>
  <div id="container">
    <div id="roundFrame">
      <div id="menu">
          <h1>SIMON</h1>
        <div id="options">
          
          <div class="btnSec">
            <div id="count">
              <p id="num"></p>
            </div>          
            <p>COUNT</p>
          </div>
          
          <div class="btnSec">
            <div id="startBtn" class="btnRound"></div>
            <p>START</p>
          </div>
          
          <div class="btnSec">
            <div id="strictBtn" class="btnRound"></div>
            <p>STRICT</p>
          </div>
                  
          <div id = "light">         
          </div>  
        
         </div><!--options-->
        
        <div id="power">
          <p>OFF</p>
          <div id ="switch">
            <div id="onOff"></div>
          </div>  
          <p>ON</p>
        </div>          
        
      </div><!--menu-->
      
      <div id ="barRound" class="bar">
      </div>  
      <div id ="barVert" class="bar">
      </div>
      <div id ="barHoriz" class="bar">
      </div>
      <div id="0" class="Lgt green">
      </div>
      <div id="1" class="Lgt red">
      </div>
      <div id="2" class ="Lgt orange">
      </div>    
      <div id="3" class ="Lgt blue">
      </div>
      
    </div><!--roundFrame-->
  </div><!--container-->
</html>  

            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Russo+One');
@import url('https://fonts.googleapis.com/css?family=Aldrich');
@import url('https://fonts.googleapis.com/css?family=VT323');

body{
  background:#443636;
}

#container{
  display:flex;
  align-items:center;
  justify-content:center;
  height:87vmin;  
  width:87vmin;
  margin: 55px auto 0 auto;
}

#roundFrame{ 
  display:flex;
  flex-wrap:wrap;
  align-items:center;
  justify-content:center;
  background:#262626;
  height:90%;  
  width:90%;
  padding:5%;
  border-radius:50%; 
}

#menu{
 position:absolute;
 background:#262626;
 z-index:3;
 width:40vmin;
 height:40vmin;
 border-radius:50%;
}

h1{
  font-family:'Russo One',sans-serif;
  text-align:center;
  font-size:8vmin;
  font-weight:900;
  color:white;
  letter-spacing: .5vmin;
  margin:6vmin 0 3vmin 0;
}

#options{
  display:flex;
  flex-wrap: wrap;
  justify-content:center;
  align-items:center;
  height:12vmin;
}

p{
  font-family:'Aldrich',sans-serif;
  font-weight:500;
  font-size:2.1vmin;
  color:white;
}

#num{
  font-family:'VT323',sans-serif;
  color:#ff2121;
  text-align:center;
  font-size:5.5vmin;
}

.btnSec{
  display:flex;
  flex-direction:column;
  align-items:center;
  padding: 0vmin 1.5vmin 0vmin 1.5vmin;
}

#count{
  display:flex;
  justify-content:center;
  align-items:center;
  height:5vmin;
  width:8vmin;
  background:#363636;
  border-radius:1vmin;
}

.btnRound{
  transform: translateY(-.7vmin);
  box-shadow: 0 .7vmin #27496d;  
  border: none;
}

.btnRound:active{
  transform: translateY(0vmin);
  box-shadow: 0 0vmin #27496d;  
}

.btnRound:hover{
  cursor:pointer;
}

.btnRound:focus {
  outline:none;
}

#startBtn{
  height:5vmin;
  width:5vmin;
  background:red;
  border-radius:50%;
}

#startBtn:active{
  -webkit-tap-highlight-color:  rgba(255, 255, 255, 0);
}

#strictBtn{ 
  height:5vmin;
  width:5vmin;
  background:yellow;
  border-radius:50%;
}

#light{
  height :2vmin;
  width:2vmin;  
  background:grey;
  border-radius:50%;
  transform: translateY(-3.1vmin);
}

#power{
  display:flex;
  flex-direction:row;
  justify-content:center;
  align-items:center;
  width:25vmin;
  margin:auto;
}

#switch{
  display:flex;
  flex-direction:row;
  justify-content:flex-start;
  height:5vmin;
  width:10vmin;
  background:black;
  margin:auto 2.5vmin auto 2.5vmin;
  border-style:solid;
  border-width:.5vmin;
  border-radius:.6vmin;
  -webkit-tap-highlight-color:  rgba(255, 255, 255, 0);
}

#switch:hover{
  cursor:pointer;
}

#onOff{
  height:5vmin;
  width:5vmin;
  background:#5155ff;
  border-radius:.6vmin;
}

.bar{
  position:absolute;
  background:#262626;
  z-index:1;
}

#barRound{
  height:44vmin;
  width:44vmin;
  border-radius:50%;
  z-index:2;
}

#barVert{
 width:83vmin;
 height:5vmin; 
}

#barHoriz{
 width:5vmin;
 height:83vmin; 
}

.Lgt{
  height:50%;
  width:50%;
  cursor:pointer;
}

.green{
  background:#42984b;
  border-radius: 100% 0 0 0;
}

.green-active{
  -webkit-tap-highlight-color:  rgba(255, 255, 255, 0);
  background:#a0fbac;
}

.green:active{
  -webkit-tap-highlight-color:  rgba(255, 255, 255, 0); 
}

.red{
  background:#bf392e;
  border-radius: 0 100% 0 0;
}

.red-active{
  -webkit-tap-highlight-color:  rgba(255, 255, 255, 0);
  background:#ffc6c1;
}

.red:active{
  -webkit-tap-highlight-color:  rgba(255, 255, 255, 0); 
}

.orange{ 
 background:#cec532;
 border-radius: 0 0 0 200%;
}

.orange-active{
  -webkit-tap-highlight-color:  rgba(255, 255, 255, 0);
  background:#fff990;
}

.orange:active{
  -webkit-tap-highlight-color:  rgba(255, 255, 255, 0); 
}

.blue{
 background:#2255be;
 border-radius: 0 0 100% 0;
}

.blue-active{
 -webkit-tap-highlight-color:  rgba(255, 255, 255, 0); 
 background:#b3ccfd;
}

.blue:active{
   -webkit-tap-highlight-color:  rgba(255, 255, 255, 0); 
}




            
          
!
            
              const maxLevels = 20;
var powerOn = false;
var startOn = false;
var strictOn = false;
var errorOn = false;
var errored = false
var winOn = false;
var pressOn = false;
var simonOn = false;
var playerRythm = [];
var simonRythm = [];
var id, col;
var level = 0;
var sounds =["https://s3.amazonaws.com/freecodecamp/simonSound1.mp3","https://s3.amazonaws.com/freecodecamp/simonSound2.mp3","https://s3.amazonaws.com/freecodecamp/simonSound3.mp3","https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"];

window.onload = function(){
  onOff();
}

//Adds event listener to power switch
function onOff(){
  document.getElementById("power").addEventListener("click",function(){{powered()};});
}

//Switches game power on and off 
function powered(){
  //Ensures the light pads cannot be clicked on yet
  pressOn = false;
  //Turns power on and adds click event listeners to start and strict buttons
  if(powerOn == false){
    $("#switch").css({"justify-content":"flex-end"});
    powerOn = true;    
    $("#startBtn").click(function(){{start()};});
    $("#strictBtn").click(function(){{strict()};});
  //Turns off power and anything which should only be turned off when the power has bee turned off, resets the game, and removes event listeners from start and strict buttons  
  }else if (powerOn == true){
    $("#switch").css({"justify-content":"flex-start"});
    powerOn = false;
    startOn = false;
    strictOn = false; 
    reset();
    $("#startBtn").off('click');
    $("#strictBtn").off('click');   
    $("#light").css({"background":"grey"});
  }
}

//Starts the game when the start button has been pressed on
function start(){
  //Prevents strict button from working again during gameplay
  $("#strictBtn").off('click');
  
  //Increments the game level, displays it on screen and then calls the simon sequence
  if(powerOn == true && startOn == false){
    level++;
    document.getElementById("num").innerHTML = level;
    startOn = true;
    simonSeq();
  } 
  
  //Removes click event listeners from light pads if there are any
  $('.Lgt').off('click');
  
  //Adds click event listeners to light pads if the simon sequence is finished
    $(".Lgt").click(function(){
     if(pressOn == true && simonOn == false){
       //The id number of the light pad being clicked on
      id = $(this).attr("id");
      //The class colour of the light pad being clicked on
      var colClass = $(this).attr("class"); 
      colClass = colClass ? colClass.split(" ")[1] : ' ';
      col = colClass;
      //pushes pad id onto players sequence 
      playerRythm.push(id);
      //sends pad and colour to sound function to play its sound 
      addSoundClass(id, col);
      //checks user sequence. If false, resets players sequence and calls error function
      if(checkMatch() == false){
        playerRythm = [];
        error();
      }
      //Calls next simon sequence round    
      if(playerRythm.length == simonRythm.length && playerRythm.length < maxLevels){
        pressOn = false;
        level++;
        playerRythm = [];
        simonSeq();
      }
      //Checks for winner
        if(playerRythm.length == maxLevels && errorOn == false && winOn == false){
           winOn == true;
           pressOn = false;
           var count = 0;
           var playerWin = setInterval(function(){
             count++;
             document.getElementById("num").innerHTML = "WIN";
             //If the winner display is finished, resets the game and starts again
             if(count == 3){
               clearInterval(playerWin);
               reset();
               level++;
               simonSeq();
             }      
           },1000);         
         }         
        }  
     });
}

//Checks if player sequence matches simon sequence, returns false or true
function checkMatch(){
  for(var i = 0; i < playerRythm.length; i++){
    if(playerRythm[i] != simonRythm[i]){
      return false;
    }
  }
  return true;
}

//Displays an error
function error(){
  if(errorOn == false && winOn == false){
    errorOn = true;
    errored = true;
    pressOn = false;
    var count = 0;
    var playerError = setInterval(function(){
      document.getElementById("num").innerHTML = "--";
      count++;
      //When the error display is finished
      if(count == 3){
        document.getElementById("num").innerHTML = level;
        clearInterval(playerError);
        count = 0;
        errorOn = false;
        //If strick mode is on, failure sends the player back to level 1
        if(strictOn == true){
             reset();
             level++;
             simonSeq();
        //Otherwise the current level is played again  
        }else{
          simonSeq();
        }
      }
    },500);
  }
}

//Starts simon sequence
function simonSeq(){
  var simCount = 0;
  //Waits for a second before the simon sequence starts
  var simonWait = setInterval(function(){
  simCount++;
  if(simCount == 1){
     clearInterval(simonWait); 
     simonOn = true;
     pressOn = false;
     //Adds level to display
     document.getElementById("num").innerHTML = level;
     //If there has been no error from the player, get the next random value
     if (errored == false){
       getRandVal();
     }
     //Light pads id and colour class of simon sequence
     var i = 0;
     var myInt = setInterval(function(){
     id = simonRythm[i];        
     var colClass = $("#"+ id).attr("class"); 
     colClass = colClass ? colClass.split(" ")[1] : ' ';
     col = colClass;
     //Sound of simon sequence 
     addSoundClass(id,col);
     //Stop after simon sequence limit has been reached  
     i++;
     if(i == simonRythm.length){
        clearInterval(myInt);  
        pressOn = true;
        simonOn = false;
        //If there was an error, turn the errored switch back to normal after finished
        if (errored == true){
          errored = false;
        }
     }     
   }, 1000);  
   } 
  },1000);  
}

//Generate random number
function getRandVal(){
  var random = Math.floor(Math.random() * 4);
  simonRythm.push(random);
}

//Add temp sound and class
function addSoundClass(id,col){
  $("#"+id).addClass(col+"-active");
 soundPlay(id);
  setTimeout(function(){
    $("#"+id).removeClass(col+"-active");
  },500);
}

//Sound play
function soundPlay(id){
  var sound = new Audio(sounds[id]);
  sound.play();
}

//Turns strict mode on or off
function strict(){
  if(powerOn == true && strictOn == false){
  $("#light").css({"background":"yellow"});   
    strictOn = true;   
  }else if(powerOn == true && strictOn == true){
    $("#light").css({"background":"grey"});
    strictOn = false;
  }
}

//Resets game
function reset(){
  errorOn = false;
  errored = false;
  pressOn = false;
  simonOn = false;
  level = 0;
  playerRythm = [];
  simonRythm = [];
  id = null;
  col = null;
  document.getElementById("num").innerHTML ="";
  winOn = false;  
}


            
          
!
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