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.

            
              <div class="simonContainer">
  <div class="simonTopHalf">
    <div class="gameButton" id="green"></div>
    <div class="gameButton" id="red"></div>
  </div>
  <div class="centerCircle">
    <h1>SIMONE</h1>
    <div class="btnLabels">
      <p>power</p>
      <p>score</p>
      <p>strict</p>
    </div>
    <div class="centerCircleBtmHalf">
      <div id="powerButton"></div>
      <div id="counterDisplay">
        <h2 id="scoreDisplay">--</h2>
      </div>
      <div id="strictButton"></div>
    </div>
  </div>
  <div class="simonBottomHalf">
    <div class="gameButton" id="yellow"></div>
    <div class="gameButton" id="blue"></div>
  </div>
</div>
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Oswald);

@import url(https://fonts.googleapis.com/css?family=Roboto+Mono);

body {
  font-family: Oswald;
  background-color: #3f3f3f;
}

.simonContainer {
  position: relative;
  margin: auto;
  margin-top: 50px;
  padding: 1px;
  height: 500px;
  width: 500px;
  background-color: black;
  border-radius: 100%;
  user-select: none;
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
}

.simonTopHalf, simonBottomHalf {
  display: block;
  width: 100%;
}

.centerCircle {
  color: white;
  width: 250px;
  height: 250px;
  background-color: black;
  position: absolute;
  top: 126px;
  left: 126px;
  z-index: 2;
  border-radius: 100%;
}

.centerCircle h1 {
  font-size: 3.5em;
  font-weight: normal;
  line-height: 95px;
  text-align: center;
  vertical-align: middle;
  margin-bottom: 0;
}

.btnLabels {
  margin-left: 4px;
  display: flex;
  justify-content: center;
}

.btnLabels p {
  color: #555;
  margin: 0 17px 3px;
  text-align: left;
}

.centerCircleBtmHalf {
  display: flex;
  justify-content: center;
  width: 100%;
}

#powerButton {
  border-radius: 100%;
  height: 25px;
  width: 25px;
  background-color: #000080;
  margin-right: 30px;
}

.powerOn {
  background-color: #3333ff !important;
  box-shadow: 0 0 5px 2px #3333ff;
}

#counterDisplay {
  border-radius: 0.3em;
  height: 60px;
  width: 60px;
  background-color: #660000;
}

#scoreDisplay {
  color: red;
  font-size: 2.7em;
  font-family: Roboto Mono;
  margin: 0;
  text-align: center;
  vertical-align: middle;
  line-height: 60px;
}

#strictButton {
  border-radius: 100%;
  height: 15px;
  width: 15px;
  background-color: #808000;
  margin-left: 30px;
}

.strictOn {
  background-color: #ffff33 !important;
  box-shadow: 0 0 5px 1px #ffff33;
}

.gameButton {
  display: inline-block;  
  width: 212px;
  height: 212px;  
  margin-left: 25px;
  margin-top: 24px;
}

#green {
  background-color: #00b020;
  border-top-left-radius: 100%;
}

.greenActive {
  box-sizing: border-box;
  background-color: #00e62a !important;
  border-left: 3px solid #222;
  border-top: 3px solid #222;
}


#red {
  background-color: #e00606;
  border-top-right-radius: 100%;  
}

.redActive {
  box-sizing: border-box;
  background-color: #ff4d4d !important;
  border-top: 3px solid #222;
  border-right: 3px solid #222;
}

#yellow {
  background-color: #ffcc00;
  border-bottom-left-radius: 100%;
}

.yellowActive {
  box-sizing: border-box;
  background-color: #ffe066 !important;
  border-bottom: 3px solid #222;
  border-left: 3px solid #222;
}

#blue {
  background-color: #0039e6;
  border-bottom-right-radius: 100%;
}

.blueActive {
  box-sizing: border-box;
  background-color: #3366ff !important;
  border-bottom: 3px solid #222;
  border-right: 3px solid #222;
}
            
          
!
            
              $(document).ready(function(){
  var simonArr = [];
  var gameRunning = false;
  var acceptInput = false;
  var strict = false;
  
  var greenAudio = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
  var redAudio = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3");
  var yellowAudio = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3");
  var blueAudio = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");  
  var wrongAudio = new Audio("http://oringz.com/ringtone/served/sounds-913-served/");
  var winAudio = new Audio("http://oringz.com/ringtone/expect-good-news/79_high-gong/");
  
  function simonGen(){
    var newBtn = Math.random();
    if (newBtn < 0.25){
      simonArr.push("green");
    } else if (newBtn < 0.5){
      simonArr.push("red");
    } else if (newBtn < 0.75){
      simonArr.push("yellow");
    } else {
      simonArr.push("blue");
    }
  };
  
  function playButton(optArgument){
    var btnName = optArgument;
    var button = document.getElementById(btnName);
    var activeClass = btnName + "Active";
    //switch statement to trigger audio for button presses
    switch(btnName){
      case "green":
        greenAudio.play();
        break;
      case "red":
        redAudio.play();
        break;
      case "yellow":
        yellowAudio.play();
        break;
      case "blue":
        blueAudio.play();
        break;
    }
    button.classList.add(activeClass);
    acceptInput = false;
    var removeClass = function(){
      button.classList.remove(activeClass);
      acceptInput = true;
    };
    setTimeout(removeClass,450);
  };
  
  function wrongButton(optArgument){
    var btnName = optArgument;
    var button = document.getElementById(btnName);
    var activeClass = btnName + "Active";
    wrongAudio.play();
    button.classList.add(activeClass);
    var removeClass = function(){
      button.classList.remove(activeClass);
    };
    setTimeout(removeClass,250);
  };
  
  function simonIterates(){
    acceptInput = false;
    var index = 0;
    function simonCounter(){
      setTimeout(function(){
        playButton(simonArr[index]);
        index += 1;
        if (index < simonArr.length){
          simonCounter();
        } else {
          compareToSimon();
        }
      }, 750);
    }
    simonCounter();
  };
  
  function showScore() {
    var length = simonArr.length;
    var currentScore;
    if (length < 10){
      currentScore = "0" + length;
    } else {
      currentScore = length;
    }
    $("#scoreDisplay").text(currentScore);
  };
  
  function simonSay(){
    acceptInput = false;
    simonGen();
    simonIterates();
  };
  
  function compareToSimon(){
    if (!gameRunning){
      return false;
    }
    acceptInput = true;
    var currentIndex = 0;
    $(".gameButton").off().on("click", function(){
      var color = this.id;
      if (acceptInput && color === simonArr[currentIndex]){
        playButton(color);
        currentIndex += 1;
        if (currentIndex === 20){ //win condition
          gameRunning = false;
          showScore();
          setTimeout(function(){
            winAudio.play();
          },1000);
          setTimeout(function(){
            alert("a winner is you");
          }, 3000);
        } else if (currentIndex === simonArr.length){
          acceptInput = false;
          showScore();
          setTimeout(simonSay, 500);
        }
      } else if (gameRunning && acceptInput && color !== simonArr[currentIndex]){
        //play wrongButton sound;
        wrongButton(color);
        acceptInput = false;
        if (strict){
          simonArr = [];
          showScore();
          setTimeout(simonSay, 500);
        } else {
          setTimeout(simonIterates,500);
        }
      }
    });
  }
  
  function powerToggle(){
    if (!gameRunning){
      gameRunning = true;
      $("#powerButton").addClass("powerOn");
      $("#scoreDisplay").text("00");
      simonSay();
    } else if (gameRunning){
      gameRunning = false;
      acceptInput = false;
      $("#scoreDisplay").text("--");
      $("#powerButton").removeClass("powerOn");
      simonArr = [];
    }    
  }
  
  $("#powerButton").on("click",powerToggle);
  
  $("#strictButton").on("click",function(){
    if (strict){
      this.classList.remove("strictOn");
      strict = false;
    } else if (!strict){
      this.classList.add("strictOn");
      strict = 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