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

              
                <link href="https://fonts.googleapis.com/css?family=Pacifico" rel="stylesheet">
<div class="container text-center">
<h1 id="text">Welcome to Simon Says.Dare to play a round or two</h1>  
<div class="row col-xs-offset-4">
<div class="col-xs-6 grass" id="red"></div>
<div class="col-xs-6 grass" id="blue"></div>
</div>
<div class="row col-xs-offset-4">
<div class="col-xs-6 grass" id="green"></div>
<div class="col-xs-6 grass" id="yellow"></div>
</div>
</div>
<div class = "myContainer">
<div class="start col-xs-offset-4" id="inline">
<button>start</button>
</div>
<div class="timer" id="inline">00
</div>
  <button class="StrictMode" id = "inline">Strict</button>
</div>
<p id = "win" class = "text-center">Congrats on Beating Simon Says</p>
<audio  id="mySound">
        <source id="music" src="http://www.chiptape.com/chiptape/sounds/medium/PlaceTrap.wav" type="audio/wav">
    </audio>

              
            
!

CSS

              
                .container{
  margin-top:10px;
  margin-bottom:10px;
  padding-bottom:5px;
  -moz-box-shadow: 1px 2px 4px rgba(0, 0, 0,0.5);
  -webkit-box-shadow: 1px 2px 4px rgba(0, 0, 0, .5);
  box-shadow: 1px 2px 4px rgba(0, 0, 0, .5);
}

#text{
  font-family:'Pacifico', cursive;
  margin-bottom:15px;
}

#red{
	width:100px;
	height:100px;
	background-color:red;
}


#blue{
	width:100px;
	height:100px;
	background-color:blue;
}

#green{
	width:100px;
	height:100px;
	background-color:green;
}

#yellow{
	width:100px;
	height:100px;
	background-color:yellow;
}

.start{
   margin-top:10px;
   margin-right:10px;
}

.StrictMode{
  margin-top:10px;
  margin-left:10px; 
}

#inline {
  display: inline-block;
  vertical-align: middle;
}
 
.myContainer{
  margin-left:170px; 
}

#win{
  margin-top:10px;
  font-family:'Pacifico', cursive;
  font-size:28px;
}
              
            
!

JS

              
                var colors = ["#red","#blue","#yellow","#green"];//Stores index id's of colors. 
var userClick = [];
var classVariable = [];
var rounds = 0;
var runTime = 20;


var error = false;
var strict = false;

$(document).ready(function(){

 $("#win").hide();

$(".start").click(function(){//Starts functions.
    strict = false;
    error = false;
    rounds = 0;
    rounds++;
    classVariable = []
    userClick = [];
    simonSequence();
});

$(".grass").click(function(){
 id = $(this).attr('id');
  $(this).toggleClass(".grass").animate({opacity:0.23});
  if($(this).is('#red')){
	var myAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
                myAudio.play();
  }else if($(this).is('#blue')){
	myAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3');
                myAudio.play();			
  }else if($(this).is('#yellow')){
	 myAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3');
                myAudio.play();
  }else if($(this).is('#green')){
	 myAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3');
                myAudio.play();
  }
  setTimeout(function(){
  $(".grass").animate({opacity: 1});
}, 100);
  
  userSequence();
});

$(".StrictMode").click(function(){
   rounds = 0;
   rounds++;
   userClick = [];
   classVariable = [];
   strict = true;
   simonSequence();
});

});



function userSequence() {
    
	userClick.push("#" + id);
  console.log("#" + id);

  
    if(!checkUserSeq()) {
     
      if(strict) {
        console.log("strict");
        classVariable = [];
        rounds = 1;
      }   
      error = true;   
      displayError();
      userClick = [];      
      simonSequence();
    }
    //checking end of sequence
    else if(userClick.length == classVariable.length && userClick.length < runTime) {
      rounds++;
      userClick = [];
      error = false;
      console.log("start simon")
      simonSequence();
    }
    //checking for winners
    if(userClick.length == runTime) {
      displayWinner();
      resetGame();
    }     
  
}

function simonSequence(){
var sound = $("#mySound");
  console.log(rounds);
  $('.timer').text(rounds);
 
  if(!error) {
    getRandomNum();
  }
  if(error && strict) {//Laai die laaste random sequence van getRandom() funksie(classVariable) wat gestoor is voor speler foutief geklik het
    getRandomNum();
  }  
  var i = 0;
  var myInterval = setInterval(function(){
  id = classVariable[i];
  console.log(id);
  $(id).toggleClass(".grass").css("opacity",0.23);//Changes opacity of selected color id to 0.23 and toggles between 0.23 and 1.
	if($(id).is('#red')){//If index is #red then play sound file assigned to it.
    
	$("#music").attr("src", "https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
    
	}else if($(id).is('#blue')){

	 $("#music").attr("src", "https://s3.amazonaws.com/freecodecamp/simonSound2.mp3");
    
  }else if($(id).is('#yellow')){
     
	$("#music").attr("src", "https://s3.amazonaws.com/freecodecamp/simonSound3.mp3");
    
  }else if($(id).is('#green')){
 
	$("#music").attr("src", "https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");
    
  }
  sound[0].pause();
  sound[0].load();
  sound[0].play();
setTimeout(function(){
    $(id).css("opacity", 1);
},500);
 
  i++;
      if(i == classVariable.length){
      clearInterval(myInterval);
    }
  },1000);
  
  }



  
function checkUserSeq(){
  for(var i = 0;i < userClick.length;i++){
  if(userClick[i] != classVariable[i]){
  return false//If indexes in arrays dont match return false.Start level again.
  }
  }
  return true;//If indexes do match continue to next level.
  } 
  
  
function displayError(){
  console.log("error");
  var counter = 0;
  var myError = setInterval(function(){
  $(".timer").text("!!");
  counter++;
  if(counter == 3){
  $(".timer").text(rounds);
  clearInterval(myError);
  userClick = [];
  counter = 0;
  }
  },500);
  }
  
function getRandomNum(){
    var random = colors[Math.floor(Math.random() * 4)];
    classVariable.push(random);//console.log(classVariable);
  }

function displayWinner(){
  var count = 0;
  var winInterval = setInterval(function() { 
    count++;
    $(".timer").text("Win");
    $("#win").show();
    if(count == 5) {
      clearInterval(winInterval);
      $(".timer").text("00");
      $("#win").hide();
      count = 0;
    }
  }, 500);
}

function resetGame(){
	userClick = [];
	classVariable = [];
    counter = 0;
    $(".timer").text("00");
}
  

              
            
!
999px

Console