cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-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.

            
                    <main>
        <div id="top-row">
          <svg id="top" class="button-box" height="100%" viewBox="0 0 600 600" version="1.1" xmlns="http://www.w3.org/2000/svg">
            <polygon class="button" data-number="0" id="green" points="300,0 500,200 300,600 100,200" />
            <image class="button" data-number="0" xlink:href="https://s3-eu-west-1.amazonaws.com/jjmax/images/simon/Darth-Vader.png" x="200" y="100" height="200px" width="200px"/>
          </svg>
        </div>
        <div id="middle-row">
          <svg id="left" class="button-box" height="100%" viewBox="0 0 600 600" version="1.1" xmlns="http://www.w3.org/2000/svg">
            <polygon class="button" data-number="1" id="red" points="0,300 200,100 600,300 200,500" />
            <image class="button" data-number="1" xlink:href="https://s3-eu-west-1.amazonaws.com/jjmax/images/simon/R2D2.png" x="100" y="175" height="200px" width="200px"/>
          </svg>
          <div id="centre">
            <svg version="1.1" class="logo" id="star-wars-logo" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 189.333 107.333">
            <g>
            	<path stroke="#e8f615" stroke-width="2" d="M148.175,66.501c0.182,3.29,0.787,7.427,6.254,12.398
            		c4.987,4.533-2.469,3.289-3.218,3.289c0,0-11.689-0.012-14.726-0.012c-3.036,0-4.847-1.822-4.847-1.822
            		c4.119,0,7.772-6.001,7.772-12.145s-6.001-11.378-10.809-11.378s-26.891,0-26.891,0v24.791h-3.235l-7.001-25H53.642l-3.795,12.651
            		l-3.795-12.651H31.377l-3.289,12.651l-3.542-12.651H10.567l11.196,37.952h11.386l5.495-17.964l4.878,17.964h11.133l7.422-25.302
            		h5.676l-7.468,25.294h12.469l2.024-5.313h13.156l1.953,5.313l25.488-0.024V82.125c0,0,5.819,6.832,8.855,9.615
            		c3.036,2.783,3.29,3.036,7.409,3.036l29.895-0.202c4.301,0,10.808-3.977,10.808-10.555c0-2.647,0.052-4.107-3.058-7.607
            		l-4.734-5.609c-2.712-2.53-0.233-2.795,2.044-2.795h16.391V56.622h-25.57C150.836,56.622,147.994,63.212,148.175,66.501z
            		 M77.561,80.145l4.301-13.916l4.047,13.916H77.561z M124.048,73.27c-3.471,0-8.673,0-8.673,0v-6.832c0,0,5.89,0,8.673,0
            		C128.024,66.438,128.711,73.27,124.048,73.27z"/>
            	<path stroke="#e8f615" stroke-width="2" d="M166.341,39.947c-4.807,0-4.807-1.771-4.807-1.771
            		c4.119,0,7.772-6.001,7.772-12.145c0-6.143-6.001-11.377-10.808-11.377s-26.891,0-26.891,0v25.293h-3.279l-7.083-25.293h-77.47
            		c-6.579,0-9.796,6.317-9.615,9.607c0.182,3.289,0.787,7.427,6.254,12.397c4.987,4.533-2.469,3.289-3.218,3.289
            		c-3.795,0-26.629,0-26.629,0v12.65c0,0,32.702,0,37.003,0c4.301,0,9.796-3.977,9.796-10.556c0-2.646,1.012-4.372-2.098-7.872
            		l-4.734-5.609c-2.712-2.53,0.324-2.53,2.602-2.53c1.265,0,15.624,0,15.624,0v26.566h12.39V26.031h16.748l-7.845,26.566h12.469
            		l2.024-5.313h13.157l1.953,5.313h25.614v-12.65c0,0,5.819,6.831,8.855,9.614s3.289,3.036,7.409,3.036c4.119,0,21.451,0,21.451,0
            		v-12.65C182.984,39.947,171.148,39.947,166.341,39.947z M107.329,38.176l4.301-13.916l4.048,13.916H107.329z M153.943,31.092
            		c-3.471,0-8.674,0-8.674,0v-6.831c0,0,5.89,0,8.674,0C157.919,24.261,158.606,31.092,153.943,31.092z"/>
            </g>
            </svg>
            <div id="controls">
              <button class="btn" id="start">Start</button>
              <button class="btn" id="reset">Reset</button>
              <button class="btn" id="strict">Strict</button>
            </div>
            <div id="counter">Counter</div>
          </div>
          <svg id="right" class="button-box" height="100%" viewBox="0 0 600 600" version="1.1" xmlns="http://www.w3.org/2000/svg">
            <polygon class="button" data-number="2" id="blue" points="0,300 400,100 600,300 400,500" />
            <image class="button" data-number="2" xlink:href="https://s3-eu-west-1.amazonaws.com/jjmax/images/simon/Chewbacca.png" x="300" y="175" height="200px" width="200px"/>
          </svg>
        </div>
        <div id="bottom-row">
          <svg id="bottom" class="button-box" height="100%" viewBox="0 0 600 600" version="1.1" xmlns="http://www.w3.org/2000/svg">
            <polygon class="button" data-number="3" id="yellow" points="300,0 500,400 300,600 100,400" />
            <image class="button" data-number="3" xlink:href="https://s3-eu-west-1.amazonaws.com/jjmax/images/simon/Stormtrooper.png" x="200" y="300" height="200px" width="200px"/>
          </svg>
        </div>

      </main>

      <hr>

      <footer>
        <div>
          <p>&copy; Se&aacute;n Behan 2015</p>
        </div>
      </footer>
      <audio id="r2a-sound" preload="auto">
        <source src="https://s3-eu-west-1.amazonaws.com/jjmax/audio/simon/r2a.ogg" type="audio/ogg">
        <source src="https://s3-eu-west-1.amazonaws.com/jjmax/audio/simon/r2a.mp3" type="audio/mpeg">
      </audio>
      <audio id="r2b-sound" preload="auto">
        <source src="https://s3-eu-west-1.amazonaws.com/jjmax/audio/simon/r2b.ogg" type="audio/ogg">
        <source src="https://s3-eu-west-1.amazonaws.com/jjmax/audio/simon/r2b.mp3" type="audio/mpeg">
      </audio>
      <audio id="r2c-sound" preload="auto">
        <source src="https://s3-eu-west-1.amazonaws.com/jjmax/audio/simon/r2c.ogg" type="audio/ogg">
        <source src="https://s3-eu-west-1.amazonaws.com/jjmax/audio/simon/r2c.mp3" type="audio/mpeg">
      </audio>
      <audio id="r2d-sound" preload="auto">
        <source src="https://s3-eu-west-1.amazonaws.com/jjmax/audio/simon/r2d.ogg" type="audio/ogg">
        <source src="https://s3-eu-west-1.amazonaws.com/jjmax/audio/simon/r2d.mp3" type="audio/mpeg">
      </audio>
      <audio id="win-sound" preload="auto">
        <source src="https://s3-eu-west-1.amazonaws.com/jjmax/audio/simon/win.ogg" type="audio/ogg">
        <source src="https://s3-eu-west-1.amazonaws.com/jjmax/audio/simon/win.mp3" type="audio/mpeg">
      </audio>
      <audio id="miss-sound" preload="auto">
        <source src="https://s3-eu-west-1.amazonaws.com/jjmax/audio/simon/shot.ogg" type="audio/ogg">
        <source src="https://s3-eu-west-1.amazonaws.com/jjmax/audio/simon/shot.mp3" type="audio/mpeg">
      </audio>
            
          
!
            
              body, html {
  height: 100%;
  margin: 0;
}

body {
  font-family: 'Share Tech', monospace;
  font-size: 16px;
  background-color: #000;
  color: #fff;
}

.button {
  text-align: center;
  cursor: pointer;
  opacity: .7;
}

.button-box {
  width: 20%;
}

#centre {
  width: 30%;
}

#counter {
  margin: 10px;
}

.highlight {
  opacity: 1;
}

.logo {
  width: 80%;
}

main {
  max-width: 1000px;
  min-width: 700px;
  margin: 0 auto;
}

main, #centre {
  display: flex;
  flex-flow: column;
  align-items: center;
  justify-content: center;
}

#middle-row, #top-row, #bottom-row, #controls {
  display: flex;
  flex-flow: row;
  align-items: center;
  justify-content: center;
  width: 100%;
}

#green {
  fill: rgb(0, 255, 0);
}

#red {
  fill: rgb(255, 0, 0);
}

#blue {
  fill: rgb(0, 0, 255);
}

#yellow {
  fill: rgb(255, 255, 0);
}

.btn {
  background: #e8f615;
  background-image: linear-gradient(to bottom, #e8f615, #eaf518);
  border-radius: 5px;
  color: #000000;
  padding: 5px 10px;
  margin: 5px;
}

            
          
!
            
              var WIN = 20;
var DURATION = 500;
var strictMode = false;
var counter = document.getElementById('counter');
var buttons = document.getElementsByClassName('button');

var tempo, // tempo increases 5th, 9th & 13th step
    steps,
    currentMove,
    buttonsEnabled,
    timeout;

function updateSequence() {
  // starts when start button pressed
  // reset on reset button or strict mode mistake
  // ends on game win
  switch (steps.length) {
    case 5:
      tempo = 0.8;
      updateTimeout();
      break;
    case 9:
      tempo = 0.6;
      updateTimeout();
      break;
    case 13:
      tempo = 0.4;
      updateTimeout();
      break;
  }
  buttonsEnabled = false;

  //pick a random number and add it to sequence
  steps.push(Math.floor(Math.random() * 4));
  updateCounter(steps.length);

  displaySequence();

  buttonsEnabled = true;
}

function displaySequence() {
  buttonsEnabled = false;
  var i = 0;
  function displayLoop() {
    setTimeout(function() {
      var selector = '[data-number="' + steps[i] + '"]';
      var element = document.querySelector(selector);
      activateButton(element, steps[i]);
      i++;
      if (i < steps.length) {
        setTimeout(function() {
          displayLoop();
        }, timeout);
      } else {
        buttonsEnabled = true;
      }
    }, timeout);
  }
  displayLoop();
}

function activateButton(element, step) {
  element.classList.add('highlight');
  var sound;
  switch (step){
    case 0:
      sound = 'r2a-sound';
      break;
    case 1:
      sound = 'r2b-sound';
      break;
    case 2:
      sound = 'r2c-sound';
      break;
    case 3:
      sound = 'r2d-sound';
      break;
  }
  document.getElementById(sound).play();
  setTimeout(function() {
    element.classList.remove('highlight');
  }, timeout);
}

function pressButton() {
  if (buttonsEnabled) {
    var buttonNumber = parseInt(this.getAttribute('data-number'));
    if (buttonNumber === steps[currentMove]) {
      activateButton(this, buttonNumber);
      currentMove++;
      if (currentMove >= WIN) {
        winGame();
      } else {
        if (currentMove === steps.length) {
          clearTimeout();
          currentMove = 0;
          setTimeout(function() {
            updateSequence();
          }, timeout);
        }
      }
    } else {
      buttonsEnabled = false;
      document.getElementById('miss-sound').play();
      if (strictMode) {
        resetGame();
        updateSequence();
      } else {
        currentMove = 0;
        displaySequence();
      }
    }
  }
}

function updateTimeout() {
  timeout = DURATION * tempo;
}

function resetGame() {
  steps = []; // new game, empty steps array TODO - reset and start same
  currentMove = 0;
  tempo = 1;
  updateTimeout();
  buttonsEnabled = false;
  updateCounter('--');
}

function winGame() {
  buttonsEnabled = false;
  document.getElementById('win-sound').play();
  resetGame();
}

function updateCounter(steps) {
    counter.innerHTML = steps;
}

document.getElementById('start').addEventListener('click', function(e) {
  resetGame();
  updateSequence();
});

document.getElementById('reset').addEventListener('click', function(e) {
  resetGame();
});

document.getElementById('strict').addEventListener('click', function(e) {
  resetGame();
  if (!strictMode) {
    strictMode = true;
    this.innerHTML = 'Normal';
  } else {
    strictMode = false;
    this.innerHTML = 'Strict';
  }
});

for (var j = 0; j < buttons.length; j++) {
  buttons[j].addEventListener('click', pressButton, false);
}

resetGame();

            
          
!
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