cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<audio id = "greenSound">
  <source src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3" type="audio/mp3">
</audio>
<audio id = "redSound">
  <source src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3" type="audio/mp3">
</audio>
<audio id = "blueSound" >
  <source src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3" type="audio/mp3">
</audio>
<audio id = "yellowSound">
  <source  src="https://s3.amazonaws.com/freecodecamp/simonSound4.mp3" type="audio/mp3">
</audio>
<body>
<div id = "fullContainer">
  <h2 id = "title">Simon Game</h2>
  <div id = "banner"></div>
<div class = "container">
  <div id = "circle">
  </div>
  <div id = "smallCircle">
  </div>
  <button id = "newGame" class = "btn btn-primary">New Game</button>
  <h4 id = "scoreLabel">Score: </h4><h4 id = "score"></h4>
  <h5 id = "strictLabel">Strict Mode: </h5><button id = "strictSwitch" class = "btn btn-danger">Off</button>
  <a id = "green" class = "curve">
  </a>
  <a id = "red" class = "curve">
  </a>
</div>
<div class = "container">
  <a id = "yellow" class = "curve">
  </a>
  <a id = "blue" class = "curve">
  </a>
</div>
</div>
</body>
            
          
!
            
              a {
  cursor: pointer;
}
#circle {
  width: 350px;
  height: 350px;
  border-radius: 200px;
  position: absolute;
  background-color: #555555;
  margin-top: -48px;
  margin-left: -32px;
  z-index: 0;
}
#smallCircle {
  width: 190px;
  height: 190px;
  border-radius: 200px;
  position: absolute;
  background-color: #555555;
  margin-top: 32px;
  margin-left: -32px;
  z-index: 2;
}
#title {
  margin-bottom: 15px;
  margin-right: 60px;
}
h2 {
  text-align: center;
}
.btn {
  position: absolute;
  margin-top: 150px;
  margin-left: -32px;
  z-index: 3;
}
#fullContainer {
  padding-top: 80px;
  padding-bottom: 20px;
  margin-top: -50px;
}
#strictLabel {
  position: absolute;
  margin-top: 110px;
  margin-left: -60px;
  color: #DDDDDD;
  z-index: 3;
}
#strictSwitch {
  position: absolute;
  margin-top: 100px;
  margin-left: 10px;
}
#scoreLabel {
  position: absolute;
  margin-top: 70px;
  margin-left: -60px;
  color: #DDDDDD;
  z-index: 3;
}
#banner{
  margin-bottom: 65px;
}
#score {
  position: absolute;
  margin-top: 70px;
  margin-left: -14px;
  color: #DDDDDD;
  z-index: 3;
}
.container {
  display: flex;
  justify-content: center;
}
.curve{
  margin: 80px;
  padding: 0;
  transform: translate(-100px, -100px);
  z-index: 1;
}
    
.curve:after{
  content:"";
  position:absolute;
  width: 135px;
  height: 135px;
}
#green {
  border-top-left-radius:200px;
}
#green:after {
  border-top:50px solid green;
  border-left:50px solid green;
  border-top-left-radius:196px;
}
#greenPressed {
  border-top-left-radius:200px;
}
#greenPressed:after {
  border-top:50px solid #00BB00;
  border-left:50px solid #00BB00;
  border-top-left-radius:196px;
}
#red {
  border-top-right-radius:200px;
}
#red:after {
  border-top:50px solid red;
  border-right:50px solid red;
  border-top-right-radius:196px;
}
#redPressed {
  border-top-right-radius:200px;
}
#redPressed:after {
  border-top:50px solid #FF5050;
  border-right:50px solid #FF5050;
  border-top-right-radius:196px;
}
#yellow {
  border-top-right-radius:200px;
}
#yellow:after {
  border-bottom:50px solid yellow;
  border-left:50px solid yellow;
  border-bottom-left-radius:196px;
}
#yellowPressed {
  border-top-right-radius:200px;
}
#yellowPressed:after {
  border-bottom:50px solid #FFFFB0;
  border-left:50px solid #FFFFB0;
  border-bottom-left-radius:196px;
}
#blue {
  border-bottom-right-radius:200px;
}
#blue:after {
  border-bottom:50px solid blue;
  border-right:50px solid blue;
  border-bottom-right-radius:196px;
}
#bluePressed {
  border-bottom-right-radius:200px;
}
#bluePressed:after {
  border-bottom:50px solid #5050FF;
  border-right:50px solid #5050FF;
  border-bottom-right-radius:196px;
}
            
          
!
            
              $(document).ready(function(){
  var gameStarted = false;
  var strictMode = false;
  var order = [];
  var currentClick = 0;
  var next;
  
  $("#strictSwitch").click(function(){
    $("#banner").empty();
    if(strictMode) {
      $(this).removeClass("btn-success");
      $(this).addClass("btn-danger");
      $(this).html("Off");
      strictMode = false;
    }
    else {
      $(this).removeClass("btn-danger");
      $(this).addClass("btn-success");
      $(this).html("On");
      strictMode = true;
    }
  });
  
  $("#newGame").click(function(){
    $("#banner").empty();
    currentClick = 0;
    order = [];
    $("#score").html(currentClick);
    next = Math.floor(Math.random()*(4)+1);
    order.push(next);
    order.forEach(lights);
  });
  
  $("#green").click(function() {
    tap("green", 1);
  });
  $("#red").click(function() {
    tap("red", 2);
  });
  $("#blue").click(function() {
    tap("blue", 3);
  });
  $("#yellow").click(function() {
    tap("yellow", 4);
  });
  
  function tap(key, code) {
    $("#banner").empty();
    if(gameStarted) {
      //What to do when the click is correct
      if(code == order[currentClick]) {
        $("#" + key).attr('id', key + 'Pressed');
        $("#" + key + "Sound")[0].play();
        setTimeout(function(){ 
          $("#" + key + "Pressed").attr('id', key); }, 80);
        currentClick++;
        if(currentClick >= 20) {
          $("#score").html(currentClick);
          $("#banner").html("<h5 class = 'text-center alert alert-success'>You win!  Congratulations!</h5>");
          gameStarted = false;
        }
        else if(currentClick == order.length) {
          $("#score").html(currentClick);
          currentClick = 0;
          next = Math.floor(Math.random()*(4)+1);
          order.push(next);
          gameStarted = false;
          setTimeout(function(){
          order.forEach(lights);}, 500);
        }
      }
      //What to do when the click is incorrect
      else {
        $("#green").attr('id', 'greenPressed');
        $("#greenSound")[0].play();
        setTimeout(function(){ 
          $("#greenPressed").attr('id', 'green'); }, 80);
        $("#red").attr('id', 'redPressed');
        $("#redSound")[0].play();
        setTimeout(function(){ 
          $("#redPressed").attr('id', 'red'); }, 80);
        $("#blue").attr('id', 'bluePressed');
        $("#blueSound")[0].play();
        setTimeout(function(){ 
          $("#bluePressed").attr('id', 'blue'); }, 80);
        $("#yellow").attr('id', 'yellowPressed');
        $("#yellowSound")[0].play();
        setTimeout(function(){ 
          $("#yellowPressed").attr('id', 'yellow'); }, 80);
        if(strictMode) {
          gameStarted = false;
          $("#banner").html("<h5 class = 'text-center alert alert-danger'>You lost...  Feel free to try again!</h5>");
        }
        else {
          gameStarted = false;
          $("#banner").html("<h5 class = 'text-center alert alert-danger'>Uh oh, it looks like you messed up... but I'm not strict, so here is that pattern again...</h5>");
          setTimeout(function(){
          order.forEach(lights);}, 250);
          currentClick = 0;
        }
      }
    }
  }
  
  function lights(item, index, arr) {
    switch(item) {
      case 1: 
        colorPress("green", arr, index);
        break;
      case 2: 
        colorPress("red", arr, index);
        break;
      case 3: 
        colorPress("blue", arr, index);
        break;
      case 4: 
        colorPress("yellow", arr, index);
        break;
    }
  }
  function colorPress(color, arr, index) {
    setTimeout(function() {
      if (arr.length < 5) {
          setTimeout(function(){ 
          $("#" + color).attr('id', color + 'Pressed');
          $("#"  + color + "Sound")[0].play(); 
          setTimeout(function(){ 
            $("#" + color + "Pressed").attr('id', color);
            if(index + 1 == arr.length) {
              gameStarted = true;
            }
          }, 500);
          }, 500*(index+1));
        }
        else if (arr.length < 9) {
          setTimeout(function(){ 
          $("#" + color).attr('id', color + 'Pressed');
          $("#"  + color + "Sound")[0].play(); 
          setTimeout(function(){ 
            $("#" + color + "Pressed").attr('id', color);
            if(index + 1 == arr.length) {
              gameStarted = true;
            }
          }, 400);
          }, 400*(index+1));
        }
        else if (arr.length < 13) {
          setTimeout(function(){ 
          $("#" + color).attr('id', color + 'Pressed');
          $("#"  + color + "Sound")[0].play(); 
          setTimeout(function(){ 
            $("#" + color + "Pressed").attr('id', color);
            if(index + 1 == arr.length) {
            gameStarted = true;
            }
          }, 300);
          }, 300*(index+1));
        }
        else {
          setTimeout(function(){ 
          $("#" + color).attr('id', color + 'Pressed');
          $("#"  + color + "Sound")[0].play(); 
          setTimeout(function(){ 
            $("#" + color + "Pressed").attr('id', color);
          if(index + 1 == arr.length) {
          gameStarted = true;
          }
        }, 200);
            
          }, 300*(index+1));
        }
    }, ((5/arr.length)*180)*(index+1));
  }
});
            
          
!
999px
Loading ..................

Console