Simon Says Game FCC - by mate

In this example below you will see how to do a Simon Says Game FCC - by mate with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by sirmmate, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright sirmmate ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Simon Says Game FCC - by mate</title>
  
  
  <link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'>
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.1/animate.min.css'>

      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div class="container">
  <h1 class="text-center">Simon Says</h1>
  <div class="row">
  <div class="col-xs-12 col-md-12">
  <div class="circle center-block">
    <div class="quarter-circle-1" id="red"></div>
    <div class="quarter-circle-2" id="green"></div>
    <div class="panel">
      <section title=".roundedOne">
        <!-- .roundedOne -->
        <div class="roundedOne">
          <input type="checkbox" value="None" id="roundedOne" name="check" />
          <label for="roundedOne"></label>
          <p class="stricted">STRICT</p>
        </div>

        <!-- end .roundedOne -->
      </section>
      <div id="counter">0</div>
      <div class="switch">
        <input id="cmn-toggle-4" class="cmn-toggle cmn-toggle-round-flat" type="checkbox">
        <label for="cmn-toggle-4"></label>
      </div>
    </div>
    <div class="quarter-circle-3" id="blue"></div>
    <div class="quarter-circle-4" id="yellow"></div>
  </div>
 </div>
 </div>    
</div>  
<br>
<br>
<br>
<br>
<p class="text-center" style="color:#000000;">Made by <a href="https://www.freecodecamp.com/yerevin">Mateusz Skulski</a></p>
  <script src='https://code.jquery.com/jquery-2.2.4.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js'></script>
<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/sirmmate/simon-says-game-fcc-by-mate-qNdBJO */
body {
  background-color:#663300;
}

h1 {
  font-size: 4em;
  color:#000000;
}

.circle {
  border: 5px solid #000000;
  margin-top: 6%;
  border-radius: 50%;
  min-width:350px;
  min-height:350px;
  max-width:350px;
  max-height:350px;
  width: 500px;
  height: 500px;
  position: relative;
  background-image: url(https://s-media-cache-ak0.pinimg.com/736x/ea/89/9a/ea899a258fe7f4b4ffaede007a6097ae.jpg);
box-shadow: 0 0 10px 5px rgba(0,0,0,0.6); -moz-box-shadow: 0 0 10px 5px rgba(0,0,0,0.6); -webkit-box-shadow: 0 0 10px 5px rgba(0,0,0,0.6); -o-box-shadow: 0 0 10px 5px rgba(0,0,0,0.6);
}

.panel {
  border: 12px solid #000000;
  background-image: url(http://www.photos-public-domain.com/wp-content/uploads/2011/02/purple-leather-texture.jpg);
  background-size:cover;
  border-radius: 50%;
  width: 185px;
  height: 185px;
  position: absolute;
  z-index: 2;
  top: 50%;
  left: 50%;
  margin: -95px 0px 0px -95px;
  box-shadow: 0 0 15px 15px rgba(0,0,0,0.6); -moz-box-shadow: 0 0 15px 15px rgba(0,0,0,0.6); -webkit-box-shadow: 0 0 15px 15px rgba(0,0,0,0.6); -o-box-shadow: 0 0 15px 15px rgba(0,0,0,0.6);
}


/* .roundedOne */

.stricted {
  position: absolute;
  z-index: 2;
  top: 50%;
  left: 50%;
  font-weight: bold;
  color: white;
  margin: 12px 0px 0px -25px;
}

.roundedOne {
  width: 28px;
  height: 28px;
  position: absolute;
  z-index: 2;
  top: 50%;
  left: 50%;
  margin: -25px 0px 0px 35px;
  background: #fcfff4;
  background: -webkit-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  background: linear-gradient(to bottom, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%);
  border-radius: 50px;
  box-shadow: inset 0px 1px 1px white, 0px 1px 3px rgba(0, 0, 0, 0.5);
}

.roundedOne label {
  width: 20px;
  height: 20px;
  cursor: pointer;
  position: absolute;
  left: 4px;
  top: 4px;
  background: -webkit-linear-gradient(top, #222222 0%, #45484d 100%);
  background: linear-gradient(to bottom, #222222 0%, #45484d 100%);
  border-radius: 50px;
  box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.5), 0px 1px 0px white;
}

.roundedOne label:after {
  content: '';
  width: 16px;
  height: 16px;
  position: absolute;
  top: 2px;
  left: 2px;
  background: #27ae60;
  background: -webkit-linear-gradient(top, #27ae60 0%, #145b32 100%);
  background: linear-gradient(to bottom, #27ae60 0%, #145b32 100%);
  opacity: 0;
  border-radius: 50px;
  box-shadow: inset 0px 1px 1px white, 0px 1px 3px rgba(0, 0, 0, 0.5);
}

.roundedOne label:hover::after {
  opacity: 0.3;
}

.roundedOne input[type=checkbox] {
  visibility: hidden;
}

.roundedOne input[type=checkbox]:checked + label:after {
  opacity: 1;
}


/* end .roundedOne */

#counter {
  width: 60px;
  height: 40px;
  font-size: 2em;
  color: #a6a6a6;
  background-color: #262626;
  border: 3px solid #000000;
  border-radius: 20px;
  position: absolute;
  z-index: 3;
  top: 50%;
  left: 50%;
  margin: -35px 0px 0px -35px;
  text-align: right;
  padding: 0px 15px 35px 0px;
}

.quarter-circle-1 {
  cursor: pointer;
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -220px 0px 0px -220px;
  background-image: url(https://d3ui957tjb5bqd.cloudfront.net/images/screenshots/products/12/121/121134/wu6i1v3vowfazimxo5bfzlvl95ni4hbopy804mwxdvol50gwdr0fcmnjaaolyz6h-f.jpg?1401663891);
  height: 200px;
  width: 200px;
  -moz-border-radius: 200px 0 80px 0;
  border-radius: 200px 0 80px 0;
  border: 5px solid #000000;
  box-shadow: 0 0 10px 7px rgba(0,0,0,0.6); -moz-box-shadow: 0 0 10px 7px rgba(0,0,0,0.6); -webkit-box-shadow: 0 0 10px 7px rgba(0,0,0,0.6); -o-box-shadow: 0 0 10px 7px rgba(0,0,0,0.6);
}

.quarter-circle-2 {
  cursor: pointer;
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -220px 0px 0px 15px;
  background-image: url(https://c1.staticflickr.com/5/4019/4228893115_b2521da235_b.jpg);
  height: 200px;
  width: 200px;
  -moz-border-radius: 0 200px 0 80px;
  border-radius: 0 200px 0 80px;
  border: 5px solid #000000;
  box-shadow: 0 0 10px 7px rgba(0,0,0,0.6); -moz-box-shadow: 0 0 10px 7px rgba(0,0,0,0.6); -webkit-box-shadow: 0 0 10px 7px rgba(0,0,0,0.6); -o-box-shadow: 0 0 10px 7px rgba(0,0,0,0.6);
}

.quarter-circle-3 {
  cursor: pointer;
  position: absolute;
  top: 50%;
  left: 50%;
  margin: 15px 0px 0px 15px;
  background-image: url(http://m.rgbimg.com/cache1olcj0/users/m/me/melodi2/600/msBUtUs.jpg);
  height: 200px;
  width: 200px;
  -moz-border-radius: 80px 0 200px 0;
  border-radius: 80px 0 200px 0;
  border: 5px solid #000000;
  box-shadow: 0 0 10px 7px rgba(0,0,0,0.6); -moz-box-shadow: 0 0 10px 7px rgba(0,0,0,0.6); -webkit-box-shadow: 0 0 10px 7px rgba(0,0,0,0.6); -o-box-shadow: 0 0 10px 7px rgba(0,0,0,0.6);
}

.quarter-circle-4 {
  cursor: pointer;
  position: absolute;
  top: 50%;
  left: 50%;
  margin: 15px 0px 0px -220px;
  background-image: url(http://texturelib.com/Textures/rust/scratches/rust_scratches_0030_02_preview.jpg);    
  height: 200px;
  width: 200px;
  -moz-border-radius: 0 80px 0 200px;
  border-radius: 0 80px 0 200px;
  border: 5px solid #000000;
  box-shadow: 0 0 10px 7px rgba(0,0,0,0.6); -moz-box-shadow: 0 0 10px 7px rgba(0,0,0,0.6); -webkit-box-shadow: 0 0 10px 7px rgba(0,0,0,0.6); -o-box-shadow: 0 0 10px 7px rgba(0,0,0,0.6);
}

.switch {
  position: absolute;
  top: 50%;
  left: 50%;
  margin: 20px 0px 0px -45px;
  
}

.cmn-toggle {
  position: absolute;
  margin-left: -9999px;
  visibility: hidden;
  
}

.cmn-toggle + label {
  display: block;
  position: relative;
  cursor: pointer;
  outline: none;
  user-select: none;
  box-shadow: 0 0 10px 5px rgba(0,0,0,0.6); -moz-box-shadow: 0 0 10px 5px rgba(0,0,0,0.6); -webkit-box-shadow: 0 0 10px 5px rgba(0,0,0,0.6); -o-box-shadow: 0 0 10px 5px rgba(0,0,0,0.6);
}

input.cmn-toggle-round-flat + label {
  padding: 1px;
  width: 80px;
  height: 40px;
  background-color: #000;
  border-radius: 60px;
  transition: background 0.4s;
}

input.cmn-toggle-round-flat + label:before,
input.cmn-toggle-round-flat + label:after {
  display: block;
  position: absolute;
  content: "";
}

input.cmn-toggle-round-flat + label:before {
  top: 2px;
  left: 2px;
  bottom: 2px;
  right: 2px;
  background-color: #fff;
  border-radius: 60px;
  transition: background 0.4s;
}

input.cmn-toggle-round-flat + label:after {
  top: 4px;
  left: 4px;
  bottom: 4px;
  width: 32px;
  background-color: #000;
  border-radius: 52px;
  transition: margin 0.4s, background 0.4s;
}

input.cmn-toggle-round-flat:checked + label {
  background-color: #8ce196;
}

input.cmn-toggle-round-flat:checked + label:after {
  margin-left: 40px;
  background-color: #8ce196;
}

/*Downloaded from https://www.codeseek.co/sirmmate/simon-says-game-fcc-by-mate-qNdBJO */
  var arrGame = [];
  var arrPlayer = [];
  var winCounter = 0;
  var gameLevel = 0;
  var textField = $('#counter');
  var winVal = 0;
  var l = 0;
  var clickCounter = -1;
  var z = 1;
  var stop;
  var strictMode = 0;

  var redSound = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
  var greenSound = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3');
  var blueSound = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3');
  var yellowSound = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3');
  var winSound = new Audio('https://instaud.io/_/private/a834b96289282820386729d5cefc8a5105970cc8.mp3');
  var loseSound = new Audio('https://instaud.io/_/private/901b53825ae5462b52013510924949675df310b2.mp3');
  var finalWinSound = new Audio('https://instaud.io/_/private/0c6f6b7beb1e8167da826fb90f002746c9cdd8b8.mp3');

  function playRed(z) {
    setTimeout(function() {
      redSound.play();
      $("#red").animate({
        opacity: .2
      }, 400).animate({
        opacity: 1
      }, 200);
    }, 1000 * z - 500 * z);
  }

  function playGreen(z) {
    setTimeout(function() {
      greenSound.play();
      $("#green").animate({
        opacity: .2
      }, 400).animate({
        opacity: 1
      }, 200);
    }, 1000 * z - 500 * z);
  }

  function playBlue(z) {
    setTimeout(function() {
      blueSound.play();
      $("#blue").animate({
        opacity: .2
      }, 400).animate({
        opacity: 1
      }, 200);
    }, 1000 * z - 500 * z);
  }

  function playYellow(z) {
    setTimeout(function() {
      yellowSound.play();
      $("#yellow").animate({
        opacity: .2
      }, 400).animate({
        opacity: 1
      }, 200);
    }, 1000 * z - 500 * z);
  }

  function playButtons() {
    setTimeout(function() {
      for (var z = 1; z - 1 < arrGame.length; z++) {
        if (arrGame[z - 1] == 1) {
          playRed(z);
        } else if (arrGame[z - 1] == 2) {
          playGreen(z);
        } else if (arrGame[z - 1] == 3) {
          playBlue(z);
        } else if (arrGame[z - 1] == 4) {
          playYellow(z);
        }
      }
    }, 0);
  }

  function getarrGame() {
    for (; l <= gameLevel; l++) {
      var rand = Math.floor(Math.random() * 4) + 1;
      arrGame.push(rand);
    }
  }

  function nextLevel() {
    winSound.play();
    gameLevel++;
    textField.text(gameLevel);
    winVal++;
    setTimeout(startGame, 1500);
  }

  function restartGame() {
    arrGame = [];
    arrPlayer = [];
    winCounter = 0;
    gameLevel = 0;
    textField.text(gameLevel);
    winVal = 0;
    l = 0;
    disableClicks(false);
  }

  function winGame() {
    finalWinSound.play();
    alert('YOU WON!');
  }

  function startGame() {
    getarrGame();
    simonGame();

  }

  function stopSequence() {
    clearTimeout(stop);
  }

  function arraysEqual(arrGame, arrPlayer) {
    if (arrGame.length !== arrPlayer.length)
      return false;
    for (var i = arrGame.length; i--;) {
      if (arrGame[i] !== arrPlayer[i])
        return false;
    }

    return true;
  }

  function disableClicks(boolean) {
    if (boolean == false) {
      $("#red").off('click', clickButton1);
      $("#green").off('click', clickButton2);
      $("#blue").off('click', clickButton3);
      $("#yellow").off('click', clickButton4);
    } else {
      $("#red").on('click', clickButton1);
      $("#green").on('click', clickButton2);
      $("#blue").on('click', clickButton3);
      $("#yellow").on('click', clickButton4);
    }
  }

  function simonGame() {
    clickCounter = -1;
    arrPlayer = [];
    winCounter = 0;
    disableClicks(false);
    playButtons();
    $("#red").on('click', clickButton1);
    $("#green").on('click', clickButton2);
    $("#blue").on('click', clickButton3);
    $("#yellow").on('click', clickButton4);
    if (winVal === 20) {
      winGame();
      restarGame();
    }
  }

  function clickButton1() {
    clickCounter++;
    arrPlayer.push(1);
    $("#red").animate({
      opacity: .2
    }, 400).animate({
      opacity: 1
    }, 300);
    redSound.play();
    if (arrPlayer[clickCounter] === arrGame[clickCounter]) {
      winCounter++;
      if (winCounter === arrGame.length) {
        nextLevel();
      }
    } else {
      loseSound.play();
      if ($('#roundedOne').is(':checked')) {
        restartGame();
        startGame();
      }
      setTimeout(simonGame, 2000);
    }
  }

  function clickButton2() {
    clickCounter++;
    arrPlayer.push(2);
    $("#green").animate({
      opacity: .2
    }, 400).animate({
      opacity: 1
    }, 300);
    greenSound.play();
    if (arrPlayer[clickCounter] === arrGame[clickCounter]) {
      winCounter++;
      if (winCounter === arrGame.length) {
        nextLevel();
      }
    } else {
      loseSound.play();
      if ($('#roundedOne').is(':checked')) {
        restartGame();
        startGame();
      }
      setTimeout(simonGame, 2000);
    }
  }

  function clickButton3() {
    clickCounter++;
    arrPlayer.push(3);
    $("#blue").animate({
      opacity: .2
    }, 400).animate({
      opacity: 1
    }, 300);
    blueSound.play();
    if (arrPlayer[clickCounter] === arrGame[clickCounter]) {
      winCounter++;
      if (winCounter === arrGame.length) {
        nextLevel();
      }
    } else {
      loseSound.play();
      if ($('#roundedOne').is(':checked')) {
        restartGame();
        startGame();
      }
      setTimeout(simonGame, 2000);
    }
  }

  function clickButton4() {
    clickCounter++;
    arrPlayer.push(4);
    $("#yellow").animate({
      opacity: .2
    }, 400).animate({
      opacity: 1
    }, 300);
    yellowSound.play();
    if (arrPlayer[clickCounter] === arrGame[clickCounter]) {
      winCounter++;
      if (winCounter === arrGame.length) {
        nextLevel();
      }
    } else {
      loseSound.play();
      if ($('#roundedOne').is(':checked')) {
        restartGame();
        startGame();
      }
      setTimeout(simonGame, 2000);
    }
  }
  $('#cmn-toggle-4').click(function() {
    if ($('#cmn-toggle-4').is(':checked')) {
      $('#counter').css('color', 'red');
      startGame();
    } else {
      stopSequence();
      $('#counter').css('color', '#a6a6a6');
      restartGame();
    }

  });

Comments