123

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.

            
              <!-- Fonts -->
<link href='https://fonts.googleapis.com/css?family=Righteous' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Architects+Daughter' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' type='text/css'>

<!-- App - Main Body -->
<!-- App - Menu -->
<div class="container appFrame startScreen text-center">
  <h1 class="text-center">Evil Simon</h1>
  <h2 class="text-center">You better do as I say!</h2>
  <button class="btn playNow">Play Now!</button>
  <h4 class="text-center">Game Mode</h4>
  <div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
    <label class="onoffswitch-label" for="myonoffswitch">
      <span class="onoffswitch-inner"></span>
      <span class="onoffswitch-switch"></span>
    </label>
  </div>
  <h5><a target="_blank" href="http://www.freepik.com/free-vector/comic-cartoon-character_758722.htm">Evil Simon Character - Designed by Freepik</a></h5>
</div>

<!-- App - Gaming Board -->
<div class="container appFrame gameBoard text-center">
  <div class="scoreCircle">
    <p class="text-center scoreCounter">0</p>
    <button class="btn stopBtn">Stop</button>
  </div>
  <div class="row">
    <button class="playField col-md-6 col-xs-6" id="G">
    </button>
    <button class="playField col-md-6 col-xs-6" id="R">
    </button>
  </div>
  <div class="row">
    <button class="playField col-md-6 col-xs-6" id="Y">
    </button>
    <button class="playField col-md-6 col-xs-6" id="B">
    </button>
  </div>
</div>

<!-- App - Result Page -->
<div class="container appFrame resultScreen text-center">
  <h1 class="text-center">Results</h1>
  <h1 class="text-center scoreResults"></h1>
  <h3 class="text-center">Think you can do better?</h3>
  <button class="btn playAgain">Play Again!</button>
</div>

<!-- App - Winner Page -->
<div class="container appFrame winnerScreen text-center">
  <h1 class="text-center">Winner !</h1>
  <h3 class="text-center">Think you can beat me again?</h3>
  <button class="btn playAgain">I dare you!</button>
  <h6><a target="_blank" href="http://www.freepik.com/free-vector/comic-cartoon-character_758722.htm">Icon made by Freepik from www.flaticon.com</a></h6>
</div>
            
          
!
            
              body {
  background-color: #324E5C;
}

h1 {
  margin-top: 30px;
  font-family: 'Righteous', cursive;
  font-size: 55px;
}

h2 {
  font-family: 'Architects Daughter', cursive;
  font-size: 25px;
  font-weight: bold;
  margin-top: -5px;
  background-color: rgba(0, 0, 0, 0.8);
  border-radius: 15px;
  color: white;
}

h3 {
  font-family: 'Montserrat', sans-serif;
  margin-top: 110%;
}

h4 {
  font-family: 'Montserrat', sans-serif;
  font-weight: bold;
  margin-top: 18px;
}

h5 {
  font-size: 6px;
  margin-top: 25px;
  margin-left: 134px;
}

h6 {
  font-size: 7px;
  margin-top: 20px;
  margin-left: 104px;
}

p {
  font-family: 'Montserrat', sans-serif;
  font-size: 45px;
  font-weight: bold;
  margin-top: 6px;
}

.appFrame {
  background-color: #D3D3D3;
  width: 320px;
  height: 580px;
  margin-top: 10%;
  border: 10px solid #333238;
  border-radius: 10px;
}

.playField {
  width: 140px;
  height: 275px;
  border: 4px solid black;
  margin-left: 6px;
  margin-top: 2px;
  border-radius: 12px;
}

.gameBoard {
  display: none;
}

.resultScreen {
  display: none;
}

.scoreResults {
  font-size: 80px;
  margin-bottom: -100px;
}

.startScreen {
  background-image: url("https://dl.dropboxusercontent.com/s/c2cppjlpg25f0i0/SimonChar.png?dl=0");
  background-size: cover;
}

.winnerScreen {
  background-image: url("https://www.dropbox.com/s/db80ujrecqcbfjj/winnerIcon.png?raw=1");
  background-size: 35% 30%;
  background-repeat: no-repeat;
  background-position: 50% 40%; 
  display: none;
}

#G {
  background-color: #B9BD6F;
}

#R {
  background-color: #F73955;
}

#Y {
  background-color: #EEC84C;
}

#B {
  background-color: #467B87;
}

.scoreCircle {
  background-color: #D3D3D3;
  border: 5px solid black;
  border-radius: 50%;
  width: 120px;
  height: 120px;
  z-index: 1;
  margin-top: 220px;
  margin-left: 75px;
  position: absolute;
}

.playAgain {
  font-family: 'Montserrat', sans-serif;
  font-size: 25px;
  font-weight: bold;
  width: 170px;
  height: 80px;
  border: 3px solid black;
  border-radius: 15px;
  background-color: #38A3AC;
}

.playNow {
  font-family: 'Montserrat', sans-serif;
  font-size: 25px;
  font-weight: bold;
  width: 170px;
  height: 80px;
  border: 3px solid black;
  border-radius: 15px;
  background-color: #38A3AC;
  margin-top: 85%;
}

.stopBtn {
  font-weight: bold;
  height: 33px;
  width: 59px;
  border-radius: 14px;
  margin-top: -22px;
  border: 2px solid black;
  background-color: #38A3AC;
}

.onoffswitch {
  position: relative;
  width: 130px;
  margin-left: auto;
  margin-right: auto;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}

.onoffswitch-checkbox {
  display: none;
}

.onoffswitch-label {
  display: block;
  overflow: hidden;
  cursor: pointer;
  border: 2px solid #999999;
  border-radius: 10px;
}

.onoffswitch-inner {
  display: block;
  width: 200%;
  margin-left: -100%;
  transition: margin 0.3s ease-in 0s;
}

.onoffswitch-inner:before,
.onoffswitch-inner:after {
  display: block;
  float: left;
  width: 50%;
  height: 30px;
  padding: 0;
  line-height: 30px;
  font-size: 14px;
  color: white;
  font-family: Trebuchet, Arial, sans-serif;
  font-weight: bold;
  box-sizing: border-box;
}

.onoffswitch-inner:before {
  content: "NORMAL";
  padding-left: 10px;
  background-color: #38A3AC;
  color: #FFFFFF;
}

.onoffswitch-inner:after {
  content: "HERO MODE";
  padding-right: 10px;
  background-color: #EB6161;
  color: #FAF0F0;
  text-align: right;
}

.onoffswitch-switch {
  display: block;
  width: 18px;
  margin: 6px;
  background: #FFFFFF;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 96px;
  border: 2px solid #999999;
  border-radius: 10px;
  transition: all 0.3s ease-in 0s;
}

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
  margin-left: 0;
}

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
  right: 0px;
}
            
          
!
            
              $(document).ready(function() {

  // Initialize Game Settings
  var sequence = [];
  var score = 0;
  var gameTempo = 1100; // Lowest tempo available
  var gameMode = "Normal";
  var thisRound = [];
  var playFieldId = "";
  const soundFiles = [
    ["https://crossorigin.me/https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"], // Green
    ["https://crossorigin.me/https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"], // Red
    ["https://crossorigin.me/https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"], // Yellow
    ["https://crossorigin.me/https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"], // Blue
    ["https://crossorigin.me/http://freesound.org/data/previews/142/142608_1840739-lq.mp3"] // Wrong
  ];

  // Function building the sequence for current game.
  // Abbrv. : G - Green. R - Red. Y - Yellow. B - Blue.
  var getSequence = function(sequenceSize) {
    for (var i = 0; i < sequenceSize; i++) {
      var rand = Math.random();
      if (rand < 0.25) {
        sequence.push("G");
      } else if (rand >= 0.25 && rand < 0.50) {
        sequence.push("R");
      } else if (rand >= 0.50 && rand < 0.75) {
        sequence.push("Y");
      } else {
        sequence.push("B");
      }
    }
  };

  // Function defining the game speed. Values in millisecond.
  var getTempo = function(score) {
    if (score < 5) {
      gameTempo = 200;
    } else if (score >= 5 && score < 9) {
      gameTempo = 350;
    } else if (score >= 9 && score < 13) {
      gameTempo = 830;
    } else {
      gameTempo = 740;
    }
  };

  // Function playing the sound files. Using Howler library.
  // Also, responsible for highlighting tile colors.
  var animateField = function(index) {
    var whichSound;
    if (index === "G") {
      whichSound = soundFiles[0];
      $("#G").animate({
        opacity: 0.2
      }, 200).animate({
        opacity: 1
      }, 100);
    } else if (index === "R") {
      whichSound = soundFiles[1];
      $("#R").animate({
        opacity: 0.2
      }, 200).animate({
        opacity: 1
      }, 100);
    } else if (index === "Y") {
      whichSound = soundFiles[2];
      $("#Y").animate({
        opacity: 0.2
      }, 200).animate({
        opacity: 1
      }, 100);
    } else if (index === "B") {
      whichSound = soundFiles[3];
      $("#B").animate({
        opacity: 0.2
      }, 200).animate({
        opacity: 1
      }, 100);
    } else if (index === "Wrong") {
      // Sound file for wrong moves.
      whichSound = soundFiles[4];
    }
    var soundUsingHowler = new Howl({
      src: whichSound
    });
    soundUsingHowler.play();
  };

  // Game Mode Button functionality
  $(".onoffswitch-label").on("click", function() {
    if (gameMode == "Normal") {
      gameMode = "Strict";
    } else {
      gameMode = "Normal";
    }
  });

  // Function timing the animation sequence according to tempo.
  var timedAnimations = function(index) {
    getTempo();
    $(".scoreCounter").text(score);
    setTimeout(function() {
      animateField(sequence[index]);
    }, index * gameTempo);
  };

  // Function responsible for the animations.
  var playSequence = function() {
    for (var j = 0; j <= score; j++) {
      thisRound.push(sequence[j]);
      timedAnimations(j);
      playFieldId = "";
    }
  };

  var clickOn = false; // Credit to Ken Haduch @khaduch for solving a crucial bug at this function.
  // Function that takes player input
  var getPlayerSequence = function() {
    // Play sequence up to the score point.
    playSequence();
    if (clickOn === false) {
      $('.playField').click(function() {
        // Animate user-pressed playing fields.
        playFieldId = $(this).attr('id');
        // If the last one is correct, proceed to next round.
        // Intoduces 1.5 second delay between rounds.
        if (playFieldId == thisRound[0] && thisRound.length === 1) {
          score += 1;
          // At score 20, Winning game condition.
          if (score == 20) {
            $(".gameBoard").css("display", "none");
            $(".winnerScreen").css("display", "block");
            Howler.mute(true);
          }
          animateField(playFieldId);
          setTimeout(function() {
            thisRound.shift();
            playFieldId = " ";
            playSequence();
          }, gameTempo);
          // Keep pushing till the last one.
        } else if (playFieldId == thisRound[0] && thisRound.length > 1) {
          animateField(playFieldId);
          thisRound.shift();
          playFieldId = " ";
        } else {
          // Wrong input.
          animateField("Wrong");
          if (gameMode == "Strict") {
            // Failing at strict mode ends the game.
            $(".gameBoard").css("display", "none");
            $(".resultScreen").css("display", "block");
            $(".scoreResults").text(score);
          } else {
            // Failing at Normal mode plays the sequence again.
            setTimeout(function() {
              thisRound = [];
              playSequence();
            }, 500);
          }
        }

      });
    }
    clickOn = true;
  };

  // Button functionality
  $(".btn").on("click", function() {
    var whichButton = $(this).text();
    if (whichButton === "Play Now!") {
      $(".startScreen").css("display", "none");
      $(".gameBoard").css("display", "block");
      // Reset Game Settings.
      score = 0;
      thisRound = [];
      sequence = [];
      Howler.mute(false);
      // Initiate new game.
      getSequence(20);
      getPlayerSequence();
    } else if (whichButton === "Play Again!" || whichButton === "I dare you!") {
      $(".resultScreen").css("display", "none");
      $(".winnerScreen").css("display", "none");
      $(".startScreen").css("display", "block");
    } else if (whichButton === "Stop") {
      Howler.mute(true);
      $(".gameBoard").css("display", "none");
      $(".resultScreen").css("display", "block");
      $(".scoreResults").text(score);
    }
  });

});
            
          
!
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