LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

Code

Simon®

0
Level
Start
Strict

on/off

CSS
button {
  outline: 0;
}

body {
  text-align: center;
  background-color: #ececec;
  font-family: "Raleway", sans-serif;
  font-size: 16px;
}

h1,
h2,
h3,
h4 h5 {
  margin: 0;
  padding: 0;
}

h1 {
  margin-top: 10px;
  margin-bottom: 15px;
  font-weight: 800;
}

h2,
h4 {
  display: inline;
}

h4,
h5 {
  font-weight: bold;
}

h5 {
  display: block;
}

.simon {
  position: relative;
  margin-top: 28px;
}

.reserved {
  font-size: 13px;
  position: absolute;
  top: 0;
}

#board {
  height: 210px;
  width: 210px;
  background-color: #ececec;
  position: absolute;
  margin: auto;
  top: 500px;
  left: 0;
  bottom: 0;
  right: 0;
  -ms-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
}

.level-board {
  width: 65px;
  background-color: black;
  display: inline-block;
  color: #ecf0f1;
}

#levelNum {
  padding-bottom: 5px;
  font-size: 1.875em;
}

.no-rotate {
  -ms-transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

#game {
  width: 510px;
  margin: 0 auto;
  position: relative;
}

.field {
  float: left;
}

.green,
.red,
.yellow,
.blue {
  width: 0;
  height: 0;
}

.green {
  border-bottom: 250px solid #00b26f;
  border-left: 250px solid transparent;
  margin-right: 10px;
}

.red {
  border-bottom: 250px solid #eb4847;
  border-right: 250px solid transparent;
}

.yellow {
  border-top: 250px solid #ffe151;
  border-left: 250px solid transparent;
  margin-top: 10px;
}

.blue {
  border-top: 250px solid #4272d7;
  border-right: 250px solid transparent;
  margin-top: 10px;
  margin-left: 10px;
}

.settings {
  display: inline-block;
  height: 70px;
  margin: 3px;
  overflow: hidden;
}

.active {
  background-color: yellow !important;
}

#alerts {
  margin-top: 25px;
  height: 50px;
  width: 100%;
  line-height: 50px;
}

.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 24px;
  margin: 5px 0 5px 5px;
}

.switch input {
  display: none;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: 0.4s;
  transition: 0.4s;
}
.slider:before {
  position: absolute;
  content: "";
  height: 16px;
  width: 16px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: 0.4s;
  transition: 0.4s;
}

input:checked + .slider {
  background-color: #ecb43e;
}

input:focus + .slider {
  box-shadow: 0 0 1px #a4469c;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

#start {
  background-color: #e74c3c;
  height: 20px;
  width: 20px;
}

.board-btn {
  border-radius: 50px;
  border: 1px solid gray;
  margin: 9px 5px 11px 5px;
}

#start:hover {
  background-color: #f89291;
}

#on-off {
  background-color: #e69032;
  height: 20px;
  width: 20px;
}

.simonOn {
  background-color: #ffe048 !important;
}

.disable {
  pointer-events: none;
}
JS
$(document).ready(function() {
  const green = "green",
    red = "red",
    yellow = "yellow",
    blue = "blue";
  let level = 0;
  let sequence = [];
  let step = 0;
  let colors = [red, blue, green, yellow];
  let strict = false;

  // Start Simon
  $("#start").on("click", start);

  // Strict Mode
  $("#strictMode").on("click", function() {
    strictMode();
  });

  function strictMode() {
    $("#strictMode").toggleClass("active");
    strict = !strict;
  }

  // On/Off Mode
  $("#on-off").on("click", function() {
    $(this).toggleClass("simonOn");
    $("#start, .switch, .green, .yellow, .blue, .red").toggleClass("disable");
  });

  // Animate if clicked
  $(".green").on("click", function() {
    $(".green")
      .animate({ opacity: 0.3 }, 200)
      .animate({ opacity: 1 }, 200);
  });

  $(".red").on("click", function() {
    $(".red")
      .animate({ opacity: 0.3 }, 200)
      .animate({ opacity: 1 }, 200);
  });

  $(".yellow").on("click", function() {
    $(".yellow")
      .animate({ opacity: 0.3 }, 200)
      .animate({ opacity: 1 }, 200);
  });

  $(".blue").on("click", function() {
    $(".blue")
      .animate({ opacity: 0.3 }, 300)
      .animate({ opacity: 1 }, 300);
  });

  //Start the game
  function start() {
    step = 0;
    level = 1;
    sequence = [];
    nextSequence();
    $("#levelNum").html(level);
    $("#alerts").html("");
  }

  function nextSequence() {
    var newColor = colors[Math.floor(Math.random() * colors.length)];
    sequence.push(newColor);
    //console.log("Sequence: " + sequence);
    let gameOn = setTimeout(function() {
      animate(sequence);
      $("#levelNum").html(level);
    }, 800);
  }

  function userSequence(color) {
    if (color === sequence[step]) {
      if (step === sequence.length - 1) {
        //console.log("Sequence complete");
        winner();
        step = 0;
        level++;
        nextSequence();
      } else {
        step++;
      }
    } else {
      if (strict) {
        $("#alerts").html("

Wrong! You lost.

"); reset(); } else { gameOn = setTimeout(function() { $("#alerts").html("Wrong sequence! Try again."); tryAgain(); }, 1800); } } function tryAgain() { gameOn = setTimeout(function() { $("#alerts").html(""); animate(sequence); }, 1000); } function winner() { if (level === 20 && step === sequence.length - 1) { level = 0; step = false; gameOn = setTimeout(function() { $("#alerts").html("You won! Congratulations!"); sequence = false; level = 0; }, 800); } } } function reset() { sequence = []; step = 0; level = 0; strict = false; $("#strictMode").removeClass("active"); } function animate(sequence) { var i = 0; var interval = setInterval(function() { flash(sequence[i]); playSound(sequence[i]); i++; if (i >= sequence.length) { clearInterval(interval); } }, 1300); } function flash(pad) { $("." + pad) .animate({ opacity: 0.3 }, 600) .animate({ opacity: 1 }, 600); } function playSound(sound) { var sound = $("#audio-" + sound)[0]; sound.currentTime = 0; sound.play(); } // User's Answer $(".green").click(function() { userSequence(green); }); $(".red").click(function() { userSequence(red); }); $(".yellow").click(function() { userSequence(yellow); }); $(".blue").click(function() { userSequence(blue); }); $(".green").click(function() { $("#audio-green")[0].play(); }); $(".blue").click(function() { $("#audio-blue")[0].play(); }); $(".yellow").click(function() { $("#audio-yellow")[0].play(); }); $(".red").click(function() { $("#audio-red")[0].play(); }); });
Term
Wed, 12/27/2017 - 07:05

Related Codes

Pen ID
Pen ID
Pen ID
Square Adv