css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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 lang="en">
  <head>
    <title>Simon Game FCC</title>
  </head>
  <body>
    <div class="container">
      <div class="game">
        <div id="green" class="button"></div>
        <div id="red" class="button"></div>
        <div id="blue" class="button"></div>
        <div id="yellow" class="button"></div>
      </div>
      <div class="status">
        <h2> -- </h2>
      </div>
      <div class="control-panel">
        <div id="on-off-btn">
          <input id="on-off-slider"type="checkbox" class="cmn-toggle cmn-toggle-round">
          <label for="on-off-slider"></label>
          <p>Off/On</p>
        </div>
        <div id="level">
          <input id="level-slider"type="checkbox" class="cmn-toggle cmn-toggle-round">
          <label for="level-slider"></label>
          <p>Level</p>
        </div>
        <div id="start">
          <button class="btn-round"></button>
          <p>(re)Start</p>
        </div>
      </div>
    </div>
  </body>
  <script src="js/javascript.js" charset="utf-8"></script>
</html>

            
          
!
            
              $green: rgb(57, 213, 101);
$green-shadow: rgb(13, 147, 75);
$red: rgb(210, 58, 81);
$red-shadow: rgb(113, 21, 21);
$blue: rgb(0, 153, 255);
$blue-shadow: rgb(0, 102, 204);
$yellow: rgb(252, 154, 1);
$yellow-shadow: rgb(145, 104, 40);
$gray: rgb(170, 170, 170);

@mixin button-shadow($color-shadow){
  box-shadow: 0 6px 0 $color-shadow;
  bottom: 6px;
  transition: all 0.1s linear;
  &:active{
    bottom: 0px;
    box-shadow: 0px 0px 0px $color-shadow;
  }
}

body {
  // background-image: url("http://all4desktop.com/data_images/original/4236795-wallpaper-background.jpg");
  background: skyblue;
  font-family: 'Righteous', cursive;
  box-sizing: border-box;
}

.on{
  background: red;
}


.container{
  margin-top: 20px;
  width: 100%;
  height: 605px;
  display: flex;
  flex-wrap: wrap;
}

.animate{
  opacity: 0.23;
}
.game{
  display: flex;
  flex-wrap: wrap;
  width: 100%;
  height: 50%;
  margin-top: 10px;
}

.button{
  position: relative;
  display: block;
  height: 50%;
  width: 50%;
  margin-bottom: 1px;
  cursor: pointer;
}

#green{
  background: $green;
  transition: background-color 0.5s ease;
  border-top-left-radius: 25%;
  @include button-shadow($green-shadow);
}

#red{
  background: $red;
  transition: background-color 0.5s ease;
  border-top-right-radius: 25%;
  @include button-shadow($red-shadow);
}

#blue{
  background: $blue;
  transition: background-color 0.5s ease;
  border-bottom-left-radius: 25%;
  @include button-shadow($blue-shadow);
}

#yellow{
  background: $yellow;
  transition: background-color 0.5s ease;
  border-bottom-right-radius: 25%;
  @include button-shadow($yellow-shadow);
}

.status{
  width: 100%;
  height: 10%;
  text-align: center;
  color: #FFF;
  text-shadow: 1px 3px 2px black;
  margin-bottom: 5px;
}

.control-panel{
  text-align: center;
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  position: relative;
  height: 25%;
  align-items: center;
  justify-content: center;
  background: $gray;
  border-radius: 10px;
  #on-off-btn{
    width: 33.3333%;
  }
  #level{
    width: 33.3333%;
  }
  #start{
    width: 27.3333%;
  }
  p{
    margin: 8px 0px 0px 0px;
  }
}
//This hides the actual checkbox//
.cmn-toggle {
  position: absolute;
  margin-left: -9999px;
  visibility: hidden;
}
//make label as a container by position:relative
.cmn-toggle + label {
  display: block;
  position: relative;
  cursor: pointer;
  outline: none;
  user-select: none;
}

input.cmn-toggle-round + label {
  padding: 2px;
  max-width: 90%;
  height: 35px;
  background-color: #dddddd;
  border-radius: 60px;
  margin-top: 9px;
  margin-left: 5px;
}
//This creates the background of the container (inside label)
input.cmn-toggle-round + label:before,
input.cmn-toggle-round + label:after {
  display: block;
  position: absolute;
  top: 1px;
  left: 1px;
  bottom: 1px;
  content: "";
}
//This embelish the background of the container
input.cmn-toggle-round + label:before {
  right: 1px;
  background-color: white;
  border-radius: 60px;
  transition: background 0.4s;
}
//This is the toggle round switch itself
input.cmn-toggle-round + label:after {
  width: 38px;
  background-color: #fff;
  border-radius: 100%;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  transition: margin-left 0.4s;
}
//When user clicks the label, it will call transition
input.cmn-toggle-round:checked + label:before {
  background-color: #8ce196;
}
input.cmn-toggle-round:checked + label:after {
  margin-left: 58%;
}

.btn-round{
  width: 50%;
  height: 50px;
  background: rgb(237, 41, 20);
  border-radius: 100%;
  outline: none;
  @include button-shadow($red-shadow);
}
@media screen and (max-width: 311px) {
  input.cmn-toggle-round:checked + label:after {
    margin-left: 45%;
  }
}

@media screen and (min-width: 426px) {
  .container{
    width: 426px;
    margin-left: auto;
    margin-right: auto;
  }
  input.cmn-toggle-round:checked + label:after {
    margin-left: 66%;
  }
}

@media screen and (min-width: 765px) {
  .container{
    margin-top: 50px;
  }
}

            
          
!
            
              $(document).ready(function(){
  var game = new Game();
  var board = new Board();
  //Sources of the sounds for buttons and wrong buzzer
  var audioSource = {
    "green" : new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"),
    "red" : new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"),
    "blue" : new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"),
    "yellow" : new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"),
    "wrong" : new Audio("http://mp3gfx.com/download/179262796.mp3")
  }
  //Slider is used to turn on/off game
  //When off, the click event listener is off
  $("#on-off-slider").click(function(){
    $(this).toggleClass("on")
    if($("#on-off-slider").hasClass("on")){
      game.on = true;
    } else{
      game.on = false;
      game.start = false;
      game.score = 0;
      $(".button").unbind("click")
      $(".status h2").text("--")
    }
  })
  //Selects the level either easy(default) or strict
  $("#level-slider").click(function(){
    $(this).toggleClass("strict")
    if($("#level-slider").hasClass("strict")){
      game.level = "strict";
    } else{
      game.level = "easy";
    }
  })
  //if the (re)start button is pressed and the game is on
  //it will start or restart the game, based on game.start condition
  $(".btn-round").click(function(){
    if($("#on-off-slider").hasClass("on")){
      if(game.start === true){
        game.restart = true;
        game.newGame("Restarting the Game!");
        game.score = 0;
      } else{
        game.newGame("Game Start!")
      }
      game.start = true;
    } else{
      game.start = false;
    }
  })
  function Board(gameLevel){
    this.sequence = [];
    this.level = gameLevel || "";
    this.index = 0;
  }
  Board.prototype.initialize = function (level) {
    board = new Board(level)
  };
  //calls when the appropiate sound should make
  Board.prototype.makeNoise = function(color){
    audioSource[color].play();
  }
  //updates the game status with fade-in-out effect
  Board.prototype.updateStatus = function(status){
    $(".status h2").text(status);
    setTimeout(function() {
      $(".status h2").fadeOut(500, function() {
          $(this).text("Score: " + game.score);
          $(this).fadeIn(500);
          });
      }, 1000);
  }
  //gets user's input via click listener
  Board.prototype.getUserInput = function(){
    //extra caution: if user press input then
    //turn off the game immediately
    if (game.on === false){return }
    var currentLevel = board.sequence.slice(0,board.index);
    var turn = 0;
    var error = false; //uses during "easy" level -- calls the animateDisplay again
    $(".button").click(function(){
      var color = $(this).attr('id');
      if(currentLevel[turn] === color){
        turn += 1;
        board.makeNoise(color);
      } else{
        if(board.level === "strict"){ //strict level algorithm structure
          board.makeNoise("wrong");
          game.gameOver("You Lose!");
          setTimeout(function(){
            game.newGame("Game Start!")
          }, 2500)
        } else{ //easy level algorithm structure
          board.makeNoise("wrong");
          board.index -= 1;
          error = true;
          board.updateStatus("Try Again!")
          setTimeout(game.play, 3000)
        }
      }
      //if turn reaches the right number of clicks and error is false
      //user wins a round
      if(turn === board.index && error === false){
        game.score += 1 ;
        if (game.score === 20){
          game.gameOver("You Win!")
          return
        }
        var scoreString = "Score: " + game.score;
        $(".status h2").text(scoreString)
        setTimeout(game.play, 1000)
      }
    })
  }
  //lets simply collect all 20 random sequence in the beginning
  Board.prototype.randomSequence = function(){
    var arrColors = ["green","blue","red","yellow"];
    while(board.sequence.length < 20){
      var newSeq = arrColors[Math.floor(Math.random() * arrColors.length)];
      board.sequence.push(newSeq);
    }
  };
  //animateDisplay simply animates the board
  Board.prototype.animateDisplay = function(){
    //extra caution: if user press button then
    //turn off the game immediately
    if (game.on === false || game.restart === true){
      return
    }
    //speed sequence is based on the current game score
    function animatePattern(condition){
      var speed;
      if(game.score < 5){
        speed = 1000
      } else if (game.score >= 5 && game.score <= 10) {
        speed = 500
      } else if (game.score > 11 && game.score <= 15) {
        speed = 300;
      } else{
        speed = 250;
      }
      //exit if generatedPattern is zero
      //otherwise, let's light up
      if (generatedPattern.length === 0 || game.restart === true){
        return
      }
      $("#" + generatedPattern[0]).toggleClass("animate");
      if(condition){
        board.makeNoise(generatedPattern[0])
        setTimeout(function(){
          animatePattern(false)
        },speed)
      } else{
        generatedPattern.splice(0,1);
        setTimeout(function(){
          animatePattern(true)
        },70)
      }
    }
    //generatedPattern takes a slice of the generated sequence from .prototype.randomSequence
    var generatedPattern = board.sequence.slice(0,board.index + 1);
    animatePattern(true) //Here is where we call the function animatePattern
    board.index += 1; //everytime animateDisplay() is called, we'll proceed the next pattern
    game.writing = false;
  }
  //turns off the event listener
  Board.prototype.turnOffUserInput = function(){
    $(".button").unbind("click");
  }
  //lets initilize the game first as default in the following:
  function Game(level){
    this.on = false;
    this.level = level || "easy";
    this.start = false;
    this.score = 0;
    this.over = false;
    this.restart = false;
    this.writing = false;
  }
  //The game mechanics
  Game.prototype.play = function(){
    board.turnOffUserInput(); //turns off user's input
    board.animateDisplay(); //animate the sequence on the board
    board.getUserInput(); //turns on user's input
  }
  //initialize new game and outputs its status
  Game.prototype.newGame = function(status){
    game.restart = false;
    board.updateStatus(status)
    board.initialize(game.level);
    board.randomSequence();
    game.score = 0;
    setTimeout(game.play, 2500);
  }
  Game.prototype.gameOver = function(prompt){
    $(".status h2").text(prompt);
    board.turnOffUserInput()
    game.over = 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.
Loading ..................

Console