cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

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="container">
  <div class="row">
    <div class="col-md-3" style="background-color: #000000;"></div>
    <div class="col-md-12 base drop">
      <div class="controls">
        <h1 class="name">simon<span style="font-size: 50%;"><sup>&#174;</sup></span></h1>
        <div class="control-row">
          <div class="counter">
            COUNTER
            <div class="display"> <div id="display-count" class="display-count">00</div></div>
          </div>
          <div class="start">
            START
            <button class="start-btn" id="start" type="button" onclick="playGame()"></button>
          </div>
          <div class="strict">
            STRICT
            <button class="strict-btn" id="strict-button" onclick="strictBtn()"></button>
            <button class="strict-btn-on" id="strict-button-on" onclick="strictBtnOn()"></button>
          </div>
        </div>
        <div class="control-row">
          <div class="on">ON</div>
          <div class="switch">
            <div id="switch" class="switch-button" onclick="switchBtn()"></div>
            <div id="switchoff" class="switch-button-on" onclick="switchBtnOff()"></div>
          </div>
          <div class="off">OFF</div>
        </div>
      </div>
      <div class="row">
        <div class="col-md-12" id="game">
          <div class="green-btn" id="green-button">
            <img src="http://www.mikemorkes.com/codepen/simon/green_btn_off.png" width="300" height="300" id="btn_0" onclick="btn_click(0)">
          </div>
          <div class="red-btn" id="red-button">
            <img src="http://www.mikemorkes.com/codepen/simon/red_btn_off.png" width="300" height="300" id="btn_1" onclick="btn_click(1)">
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-md-12">
          <div class="yellow-btn" id="yellow-button">
            <img src="http://www.mikemorkes.com/codepen/simon/yellow_btn_off.png" width="300" height="300" id="btn_2" onclick="btn_click(2)">
          </div>
          <div class="blue-btn" id="blue-button">
            <img src="http://www.mikemorkes.com/codepen/simon/blue_btn_off.png" width="300" height="300" id="btn_3" onclick="btn_click(3)">
          </div>
        </div>
      </div>
    </div>
    <div class="col-md-3"></div>
    <div style="display: none;"><form id="my_form">
                    Round: <input id="seq_len" type="text" size="2">
      </form></div>
  </div>
</div>

            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Roboto+Slab:700|Roboto:400,700|Roboto+Condensed:400,700);

body { /* background is from https://newevolutiondesigns.com/50-hd-retro-wallpapers */
  background: #7c0704 url('http://www.mikemorkes.com/codepen/simon/retro-stripes-background.jpg') no-repeat fixed bottom center / cover;
  font-family: 'Roboto Condensed', Arial, Helvetica, sans-serif;
}

.base {
background: #000000; /* Old browsers */
background: -moz-linear-gradient(-45deg,  #292a2d 0%, #000000 100%); /* FF3.6-15 */
background: -webkit-linear-gradient(-45deg,  #292a2d 0%,#000000 100%); /* Chrome10-25,Safari5.1-6 */
background: linear-gradient(135deg,  #292a2d 0%,#000000 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#292a2d', endColorstr='#000000',GradientType=1 ); /* IE6-9 fallback on horizontal gradient */
  margin: 100px auto 0;
  border-radius: 50%;
  width: 600px;
  height: 600px;
  padding: 0 !important;
  box-shadow: inset 4px 4px 20px #666;
}

.controls {
background: #e2e2e2; /* Old browsers */
background: -moz-linear-gradient(-45deg,  #e2e2e2 0%, #dbdbdb 50%, #d1d1d1 51%, #fefefe 100%); /* FF3.6-15 */
background: -webkit-linear-gradient(-45deg,  #e2e2e2 0%,#dbdbdb 50%,#d1d1d1 51%,#fefefe 100%); /* Chrome10-25,Safari5.1-6 */
background: linear-gradient(135deg,  #e2e2e2 0%,#dbdbdb 50%,#d1d1d1 51%,#fefefe 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#e2e2e2', endColorstr='#fefefe',GradientType=1 ); /* IE6-9 fallback on horizontal gradient */

  border-radius: 100%;
  width: 220px;
  height: 220px;
  position: absolute;
  z-index: 1000000;
  top: 188px;
  left: 188px;
  margin: 0;
}

.name {
  font-family: 'Roboto Slab', serif; 
  text-align: center;
  margin: 30px auto 10px;
  font-size: 50px;
}

.control-row {
  width: 220px;
  margin-bottom: 5px;
  float: left;
}

.counter {
  float: left;
  width: 50%;
  text-align: center;
  font-size: 11px;
}

.display {
  width: 60%;
  height: 33px;
  border-radius: 8px;
  background-color: #33050b;
  margin: 5px auto;
  color: #c12900;
  font-family: 'Roboto', sans-serif;
  font-size: 24px;
}

.display-count {
    display: none;
}

.start {
  float: left;
  width: 35px;
  text-align: center;  
  font-size: 11px;  
}

.start-btn {
  border-radius: 100%;
  border: 3px solid #333;
  width: 25px;
  height: 25px;
  background-color: #009341;
  margin: 5px auto 0;
  cursor:pointer;
}

.strict {
  float: left;
  width: 55px;
  text-align: center;  
  font-size: 11px;  
}

.strict-btn {
  border-radius: 100%;
  border: 3px solid #333;
  width: 25px;
  height: 25px;
  background-color: #7c0704;
  margin: 5px auto 0;
  cursor:pointer;
  display: block;
}

.strict-btn-on {
  border-radius: 100%;
  border: 3px solid #333;
  width: 25px;
  height: 25px;
  background-color: #ef0600;
  margin: 5px auto 0;
  cursor:pointer;  
  display: none;
}

.on {
  float: left;
  width: 67px;
  text-align: right;  
  font-size: 11px;  
}

.switch {
  float: left;
  width: 62px;
  text-align: center;  
  background-color: #333;
  margin: 0 10px;
  border-radius: 3px;
  padding: 3px;
}

.switch-button {
  width: 20px;
  height: 20px;
  background-color: #7c0704;
  margin-left: 36px;
  cursor:pointer;  
}

.switch-button-on {
  width: 20px;
  height: 20px;
  background-color: #7c0704;
  cursor:pointer; 
  display: none;
}

.off {
  float: left;
  width: 67px;
  text-align: left;  
  font-size: 11px;  
}

.btn-shadow {
      -webkit-filter: drop-shadow( 0 0 10px #000 );
            filter: drop-shadow( 0 0 10px #000 );
}

.green-btn {
  width: 280px;
  height: 280px;
  margin: 3px 0 10px 5px;
  float: left;
  cursor:pointer;
}

.red-btn {
  width: 280px;
  height: 280px;
  margin: 3px 0 0 10px;
  float: left;
  cursor:pointer;
}

.yellow-btn {
  width: 280px;
  height: 280px;
  margin: 0 0 0 5px;
  float: left;
  cursor:pointer;
}


.blue-btn {
  width: 280px;
  height: 280px;
  border-radius: 0% 10px 100% 10px;
  margin: 0 0 0 10px;
  float: left;
  cursor:pointer;
}

input:focus,
I select:focus,
textarea:focus,
button:focus {
	outline: none !important;
}

            
          
!
            
              // worked from http://codeperspectives.com/front-end/simon-says-sequences/ when I was struggling with working with asymetric programming, but made sure I understood the code (hence the copious inline notes) - and tweaked/improved it. Also renamed functions and variables to make more sense.

// Global
var simon = {};

var strictBoo = false;

// strict, on/off button functions
function strictBtn() {
  document.getElementById("strict-button-on").style.display = "block";
  document.getElementById("strict-button").style.display = "none";  
  strictBoo = true;
}

function strictBtnOn() {
  document.getElementById("strict-button-on").style.display = "none";
  document.getElementById("strict-button").style.display = "block";
  strictBoo = false;
}

function switchBtn() {
  document.getElementById("switch").style.display = "none";
  document.getElementById("switchoff").style.display = "block";
  document.getElementById("display-count").style.display = "block";
  document.getElementById("display-count").innerHTML = "00";
}

function switchBtnOff() {
  document.getElementById("switch").style.display = "block";
  document.getElementById("switchoff").style.display = "none";
  document.getElementById("display-count").style.display = "none";
  gameOver();
}

// set up the button images
function initializebtnImages() {
  simon.imgOff = ["http://www.mikemorkes.com/codepen/simon/green_btn_off.png", "http://www.mikemorkes.com/codepen/simon/red_btn_off.png", "http://www.mikemorkes.com/codepen/simon/yellow_btn_off.png", "http://www.mikemorkes.com/codepen/simon/blue_btn_off.png"];

  simon.imgOn = ["http://www.mikemorkes.com/codepen/simon/green_btn_on.png", "http://www.mikemorkes.com/codepen/simon/red_btn_on.png", "http://www.mikemorkes.com/codepen/simon/yellow_btn_on.png", "http://www.mikemorkes.com/codepen/simon/blue_btn_on.png"];

  simon.imgDelay = 800; // sets up how long light comes on
}

// set up the sounds
function initializeSounds() {
  // win/error tones
  simon.successSound = new Audio("http://www.mikemorkes.com/codepen/simon/sounds/tada.wav");
  simon.failureSound = new Audio("http://www.mikemorkes.com/codepen/simon/sounds/buzzerheavy.wav");
  
  //button tones
  simon.btnSound = [new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"),
    new Audio("http://www.mikemorkes.com/codepen/simon/sounds/simonSound3.mp3"),
    new Audio("http://www.mikemorkes.com/codepen/simon/sounds/simonSound2.mp3"),
    new Audio("http://www.mikemorkes.com/codepen/simon/sounds/simonSound1.mp3")
  ];

}


// set up intialization of sequence - this also resets everything between games
function initializeSeq() {
  simon.sequence = new Array(); // get sequence; 0 = green, 1 = red, 2 = yellow, 3 = blue
  for (var i = 0; i < 20; i++) {
    simon.sequence[i] = Math.floor(Math.random() * 4);
  }
  simon.sequenceLength; // track current sequence length  
  simon.btnsPlayed; // track what notes were played
  simon.btnsClicked; // track what notes were clicked by user
  simon.count = 1;
  document.getElementById("display-count").innerHTML = "0" + simon.count; // set display to current round

}

// set up event listener
function initializeListener() {
  document.addEventListener("click", onClick);
}


// plays the current sequence of notes
function playSequence() {
  simon.btnsPlayed = 0;
  simon.btnsClicked = 0;
  simon.intervalSetup = setInterval(function() {
      if (simon.btnsPlayed < simon.sequenceLength) { // compare # of notes played to current sequence length
    	btnPlay(simon.sequence[simon.btnsPlayed]);
    	simon.btnsPlayed = simon.btnsPlayed + 1;
  	} else { // if sequence length, reached, clear the interval
    	clearInterval(simon.intervalSetup);
  	}
  }, 900);
}

// function to turn on light and play note, using i, which is determined by onClick
function turnBtnOn(i, btnImage) {
  simon.btnSound[i].play(); // play sound
  btnImage.src = simon.imgOn[i]; // img source is grabbed from array imgOn
}

// function to switch light off again
function turnBtnOff(i, btnImage) {
  btnImage.src = simon.imgOff[i]; // get img source from array imgOff
}

// function to turn on light and play sound when button should play
function btnPlay(i) {
  var imgID = "btn_" + String(i); // generate btnImage id, i is puled from onClick
  var btnImage = document.getElementById(imgID); // target btnImage element with imgID using var btnImage

  turnBtnOn(i, btnImage); // turn on btnImage and play sound
  setTimeout(function() {
    turnBtnOff(i, btnImage);
  }, simon.imgDelay); // turn off lit btnImage after delay
}

// button press functions
function onClick(evt) {
  var targetID; // this is null until something is assigned to it

  targetID = evt.target.id;
  if (targetID.match("btn_") === null) { // if nothing pressed yet, don't start for loop
    return -1; // program now waits for a button press
  }

  // get target ID # and drop it in i to use later
  var i = targetID.slice(-1);
  i = Number(i);
  if (i == -1) { // nothing has been pressed, so...
    return; // break loop
  }
  
  // otherwise, this function passes on button ID to btnPlay
  btnPlay(i);
  simon.btnsClicked = simon.btnsClicked + 1;
  if (simon.sequence[simon.btnsClicked - 1] === i) { // player pressed correct button
    // see if the sequence is completed
  if (simon.btnsClicked != simon.sequenceLength) {
    return; // this breaks the loop, because player hasn't pressed enough buttons to finish round
  }

  if (simon.btnsClicked < 20) { // if current round isn't last round,
    simon.sequenceLength = simon.sequenceLength + 1; // add another note to sequence
    simon.count++; // bump up counter by 1
    if (simon.count < 10) { // change counter amount
      document.getElementById("display-count").innerHTML = "0" + simon.count;
    } else {
      document.getElementById("display-count").innerHTML = simon.count;
    }
    timer = setTimeout(function() {
      playSequence(); // play sequence
    }, 900);
  } else { // player won
    gameOver(true); // player finished all sequences, so run winner stuff
  }
    
  } else { // player pushed wrong button
    if (strictBoo === true) { // if the strict button is active
      simon.failureSound.play();
      document.getElementById("display-count").innerHTML = "END";
    } else { // if the strict button is inactive
      simon.failureSound.play();
      document.getElementById("display-count").innerHTML = "NO!";
      timer = setTimeout(function() {
        if (simon.count < 10) {
          document.getElementById("display-count").innerHTML = "0" + simon.count;
        } else {
          document.getElementById("display-count").innerHTML = simon.count;
        }
        playSequence(); // play sequence
      }, 900);
    }
  }
}


// tests to see if player won game
function gameOver(playerWon) {
  document.removeEventListener('click', onClick);
  if (playerWon) {
    timer = setTimeout(function() { // run player win sequence
      document.getElementById("display-count").innerHTML = "WIN";
      simon.successSound.play();
    }, 1000);
    timer = setTimeout(function() { // start new game
      playGame();
    }, 1700);

  } else {
    if (strictBoo === true) { // run lose sound and put END in counter
      simon.failureSound.play();
      document.getElementById("display-count").innerHTML = "END";
    }
  }
}

// run the game
function playGame(i) {
  initializeSeq();
  initializeListener();
  console.log(simon.sequence);
  simon.sequenceLength = 1;
  timer = setTimeout(function() {
    playSequence(); // play sequence
  }, 300);
}

// initialize btnImages, sounds, and number of rounds
window.onload = function() {
  initializebtnImages();
  initializeSounds();
};
            
          
!
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