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.

            
              <div class="content">
  
  <div class="row top">
    <div class="box">
      <div class="dialogue" id="dialogue">SIMON</div>
    </div>
    <div class="box">
      <div class="countertext" id="countertext">COUNT:</div>
      <div id="counter" class="counter">0</div>
      
    </div>
  
  </div>
   
  <div class="row">
    <button id="red" class="simon" value="0"></button>
    <button id="yellow" class="simon" value="1"></button>
    <button id="green" class="simon" value="2"></button>
    <button id="blue" class="simon" value="3"></button>
  </div>
  
  <div class="row">
    
    <div class="box">
      <button id="start" class="controls"></button>
      <div class="buttontext" id="starttext">START</div>
    </div>
  
    <div class="box">
      <button id="strict" class="controls"></button>
      <div class="buttontext" id="stricttext">STRICT</div>
    </div>
    
  </div>

</div>

 <audio id="redAudio" class="audio" src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"></audio>

 <audio id="yellowAudio" class="audio" src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"></audio>

 <audio id="greenAudio" class="audio" src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"></audio>

 <audio id="blueAudio" class="audio" src="https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"></audio>


 <audio id="winAudio" class="audio" src="http://www.freesound.org/data/previews/337/337049_3232293-lq.mp3"></audio>

 <audio id="loseAudio" class="audio" src="https://www.freesound.org/data/previews/327/327738_3694529-lq.mp3"></audio>
            
          
!
            
              .content {
  
  text-align: center;
  padding: 30px;
  width: 700px;
  margin: 0 auto;
  font-family: sans-serif;
  font-weight: bold;
  color: #29335C;
  letter-spacing: 2px;
}

button {  
  border-radius: 5px;
  border: none;
  outline:none; 
}
button:hover {
  opacity: 0.5;
}

.controls {
  
  width: 20px;
  height: 20px;
  margin: 10px;
  
}

.simon {
  
  width: 120px;
  height:120px;
  margin: 20px;
}

.simon:hover {
  
    opacity: 0.5;
}

.box {
  display:inline-block;
  padding: 10px;
}

#red {
  background-color: #E4572E;
}

#yellow {
  background-color: #F3A712;
}

#green {
  background-color: #A8C686;
}

#blue {
  background-color: #669BBC;
}

.dialogue {
  text-align: left;
  width: 200px;
  padding: 5px;
  
}

.counter, .countertext {
  display: inline;
}

.top {
  background-color: whitesmoke;
  margin: 5px;
  border-radius: 5px;
}
            
          
!
            
              $(document).ready(function() {
  var simon = $(".simon"); // Array of simon buttons. red is simon[0], yellow is simon[1], green is simon[2], blue is simon[3].
  var audio = $(".audio");
  var startButton = $("#start");
  var strictButton = $("#strict");
  var startText = $("#starttext");
  var strictText = $("#stricttext");
  var count = 0;
  var clicks = 0;
  var userTurn = false;
  var wait = 800;
  var array = [];
  var userArray = [];
  var strict = false;
  var counter = $("#counter");
  var goal = 20;

  function createArray() {
    var arr = [];
    for (var i = 0; i < 20; i++) {
      arr.push(Math.floor(Math.random() * 4));
    }
    return arr;
  }

  //play function -  plays sound of selected button. Button is a number referring to a simon button.
  function playButton(buttonId) {
    // e.g. playButton(1) {
    audio[buttonId].play();
   
    $(simon[buttonId]).animate({opacity: "toggle"}, wait/5);
    $(simon[buttonId]).animate({opacity: "toggle"}, wait/4);
  }

  // stops the game, clears array etc.

  function stopGame() {
    if (count == goal) {
      $("#dialogue").html("VICTORY!");
      audio[4].play();
    } else {
      $("#dialogue").html("GAME OVER");
    }

    userTurn = false;
    count = 0;
    counter.html(count);
    clicks = 0;
    startButton.css("background-color", "");
    startText.html("START");
    array = [];
    userArray = [];
  }

  // alerts user to mistake if input is incorrect, and either ends game (if in strict mode) or replays last sequence
  function mistake() {
    userTurn = false;
    $(simon).animate({opacity: "toggle"}, wait/5);
    $(simon).animate({opacity: "toggle"}, wait/4);
    audio[5].play();

    if (!strict) {
      setTimeout(function() {
        playSounds();
      }, wait);
    } else {
      stopGame();
    }
  }

  // plays array of sounds up to current count
  function playSounds() {
    userTurn = false;
    var playBack = 0;
    var intervalId = setInterval(function() {
      if (playBack <= count) {
        playButton(array[playBack]);
        playBack++;
      } else {
        clearInterval(intervalId);
        playBack = 0;
        userTurn = true;
        return;
      }
    }, wait);
  }

  function runGame(clicked) {
    clicks++;
    playButton(clicked);
    userArray.push(clicked);

    // compare user's click to current count in simon array. if match continue, else endgame.

    if (userArray.toString() !== array.slice(0, clicks).toString()) {
      mistake();
      userArray = [];
      clicks = 0;
    } else {
      if (clicks == count + 1) {
        userTurn = false;
        count++;

        if (count == goal) {
          stopGame();
          return;
        }

        counter.html(count);
        counter.innerHTML = 0;
        playSounds();
        clicks = 0;
        userArray = [];
      }
    }
  }

  strictButton.click(function() {
    if (strict === false) {
      strict = true;
      strictButton.css("background-color", "darkred");
    } else {
      strict = false;
      strictButton.css("background-color", "");
    }
  });

  startButton.click(function() {
    if (userTurn === false) {
      $("#dialogue").html("SIMON");
      userTurn = true;
      startButton.css("background-color", "darkred");
      startText.html("STOP");

      array = createArray();
      playSounds();
    } else {
      stopGame();
    }
  });

  //listens for simon clicks and calls playButton when clicked
  simon.click(function() {
    if (userTurn) {
      runGame($(this).attr("value"));
    }
  });
});

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console