123

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.

            
              .bg
.machine
  #green.h(onclick="ui(0)")
  #red.h(onclick="ui(1)")
  #yellow.h(onclick="ui(2)")
  #blue.h(onclick="ui(3)")
  .gaps
    .g1
    .g2
    .g3
    .g4
  .centre
    .panel
      .title
        h1 SIMON
        .controls
          button.h(onclick="play()") PLAY
          #score 0
          button.h(onclick="stop()") STOP
        .strict
          #LED
          button.h(onclick="strict()") Strict Mode
        br
        button.h(onclick="rave()") RAVE
        h3 DUBSTEP EDITION
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Mono');

$radius: 300px;
$inner: 100px;
$centre: 70px;
$panel: 40px;
$gapW: 28px;
$gapH: 103px;
$grey: #202423;
$green: #6BAD50;
$red: #8E021A;
$yellow: #E0C341;
$blue: #255A9B;
$white: #C4CBCA;

body {
  text-align: center;
  font-family: 'IBM Plex Mono', monospace;
  margin: 0;
}

button {
  background: $grey;
  color: white;
}

button:active {
  transform: translate(0px, 2px);
  color: $yellow;
}

button:focus {
  outline: 0;
}

h1, h3 {
  margin: 3px;
}

.bg {
  position: fixed;
  z-index: -1;
  background: url(https://s3.eu-west-2.amazonaws.com/simondubstep/lights.jpeg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  width: 100%;
  height: 100%;
}

.machine {
  position: fixed;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  background-color: $grey;
  padding: 80px;
  border: solid 5px black;
  border-radius: 400px;
  width: $radius;
  height: $radius;
}

.h {
  cursor: pointer;
}

#green {
  position: fixed;
  margin: 20px;
  padding: 50px;
  -webkit-transform: translate(-35%, -35%);
  transform: translate(-35%, -35%);
  border-top-left-radius: 400px;
  background-color: $green;
  width: $inner;
  height: $inner;
}

#red {
  position: fixed;
  margin: 20px;
  padding: 50px;
  -webkit-transform: translate(-65%, -35%);
  transform: translate(65%, -35%);
  border-top-right-radius: 400px;
  background-color: $red;
  width: $inner;
  height: $inner;
}

#yellow {
  position: fixed;
  margin: 20px;
  padding: 50px;
  -webkit-transform: translate(-35%, 65%);
  transform: translate(-35%, 65%);
  border-bottom-left-radius: 400px;
  background-color: $yellow;
  width: $inner;
  height: $inner;
}

#blue {
  position: fixed;
  margin: 20px;
  padding: 50px;
  -webkit-transform: translate(65%, 65%);
  transform: translate(65%, 65%);
  border-bottom-right-radius: 400px;
  background-color: $blue;
  width: $inner;
  height: $inner;
}

.gaps * {
  position: fixed;
  top: 50%;
  left: 50%;
  background-color: $grey;
}

.g1 {
  -webkit-transform: translate(-50%, -200%);
  transform: translate(-50%, -200%);
  width: $gapW;
  height: $gapH;
}

.g2 {
  -webkit-transform: translate(-50%, 50%);
  transform: translate(-50%, 100%);
  width: $gapW;
  height: $gapH;
}

.g3 {
  -webkit-transform: translate(-50%, 200%);
  transform: translate(-200%, -50%);
  width: $gapH;
  height: $gapW;
}

.g4 {
  -webkit-transform: translate(-50%, 200%);
  transform: translate(100%, -50%);
  width: $gapH;
  height: $gapW;
}

.centre {
  position: fixed;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  background-color: $grey;
  padding: 80px;
  border-radius: 400px;
  width: $centre;
  height: $centre;
}

.panel {
  position: fixed;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  background-color: $white;
  padding: 80px;
  border-radius: 400px;
  width: $panel;
  height: $panel;
}

.title {
  position: fixed;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

.controls, .strict {
  display: inline-flex;
  justify-content: space-between;
}

.controls {
  width: 150px;
}

#LED {
  padding: 6px;
  background: black;
  border: solid 3px $grey;
  border-radius: 50px;
}

#score {
  padding: 0px 10px;
  max-width: 10px;
  color: $red;
  background-color: $grey;
  border: 5px inset #4d4d4d;
}
            
          
!
            
              const beat = new Sound(
  "https://s3.eu-west-2.amazonaws.com/simondubstep/beat.wav"
);
const wrong = new Sound("https://s3.eu-west-2.amazonaws.com/simondubstep/wrong.mp3");
const cymbal = new Sound("https://s3.eu-west-2.amazonaws.com/simondubstep/DD2_HAT_067.wav");
const crotchet = 428;
const warning = "Pressing OK will reset your current game - are you sure?";
let wubs = [];
let raveVars = [false, undefined, undefined];
let simonGo = false;
let gameActive = false;
let strictMode = false;
let copyThis = [];
let response = [];
let position = 0;
let gInt, rInt, yInt, bInt, dInt, sInt;

wubs[0] = new Sound(
  "https://s3.eu-west-2.amazonaws.com/simondubstep/green.mp3"
);
wubs[1] = new Sound("https://s3.eu-west-2.amazonaws.com/simondubstep/red.mp3");
wubs[2] = new Sound(
  "https://s3.eu-west-2.amazonaws.com/simondubstep/yellow.mp3"
);
wubs[3] = new Sound("https://s3.eu-west-2.amazonaws.com/simondubstep/blue.mp3");

for (let i = 0; i < wubs.length; i++) {
  wubs[i].volume(0.5);
}
beat.volume(0.5);
wrong.volume(0.6);
cymbal.volume(0.8);

function ui(num) {
  if (!simonGo) {
    press(num);
    if (gameActive) {
      user(num);
    }
  }
}

function play() {
  if (!gameActive || confirm(warning)) {
    if (raveVars[0]) {
      rave();
    }
    gameActive = true;
    copyThis = [];
    position = 0;
    simon(1);
  }
}

function stop() {
  if (raveVars[0]) {
    rave();
  }
  if (!gameActive || confirm(warning)) {
    simonGo = false;
    gameActive = false;
    position = 0;
    $("#score").html(0);
    for (let i = 0; i < wubs.length; i++) {
      wubs[i].stop();
    }
    beat.stop();
    cymbal.stop();
    clearInterval(dInt);
    clearInterval(sInt);
  }
}

function simon(add) {
  if (add === 1) {
    $("#score").html(copyThis.length + 1);
  }
  simonGo = true;
  if (copyThis.length + add > 3 && position === 0) {
    beat.play();
    dInt = setInterval(() => { 
      beat.stop();
      beat.play();
    }, crotchet * 16);
  }
  if (position === copyThis.length) {
    if (add === 1) {
      let rnd = Math.floor(Math.random() * 4);
      press(rnd);
      copyThis.push(rnd);
    }
    beat.stop();
    cymbal.stop();
    cymbal.play();
    clearInterval(dInt);
    simonGo = false;
    position = 0;
  } else {
    press(copyThis[position]);
    position++;
    sInt = setTimeout(() => simon(add), crotchet);
  }
}

function user(n) {
  if (n === copyThis[position]) {
    position++
    if (position === copyThis.length) {
      position = 0;
      setTimeout(() => simon(1), crotchet * 2);
    }
  } else {
    for (let i = 0; i < wubs.length; i++) {
      wubs[i].stop();
    }
    wrong.play();
    if (strictMode) {
      gameActive = false;
      position = 0;
      copyThis = [];
    } else {
      position = 0;
      simonGo = true;
      setTimeout(() => simon(0), crotchet * 2);
    }
  }
}

function strict() {
  if (!gameActive) {
    strictMode = !strictMode;
    if (strictMode) {
      $("#LED").css({ background: "red" });
    } else {
      $("#LED").css({ background: "black" });
    }
  }
}

function press(col) {
  switch (col) {
    case 0:
      wubs[0].stop();
      wubs[0].play();
      $("#green").css({ filter: "brightness(200%)" });
      clearTimeout(gInt);
      gInt = setTimeout(() => {
        $("#green").css({ filter: "brightness(100%)" });
      }, crotchet);
      break;
    case 1:
      wubs[1].stop();
      wubs[1].play();
      $("#red").css({ filter: "brightness(200%)" });
      clearTimeout(rInt);
      rInt = setTimeout(() => {
        $("#red").css({ filter: "brightness(100%)" });
      }, crotchet);
      break;
    case 2:
      wubs[2].stop();
      wubs[2].play();
      $("#yellow").css({ filter: "brightness(200%)" });
      clearTimeout(yInt);
      yInt = setTimeout(() => {
        $("#yellow").css({ filter: "brightness(100%)" });
      }, crotchet);
      break;
    case 3:
      wubs[3].stop();
      wubs[3].play();
      $("#blue").css({ filter: "brightness(200%)" });
      clearTimeout(bInt);
      bInt = setTimeout(() => {
        $("#blue").css({ filter: "brightness(100%)" });
      }, crotchet);
      break;
  }
}

function Sound(src) {
  this.sound = document.createElement("audio");
  this.sound.src = src;
  this.sound.setAttribute("preload", "auto");
  this.sound.setAttribute("controls", "none");
  this.sound.style.display = "none";
  this.hasPlayed = false;
  document.body.appendChild(this.sound);
  this.play = function() {
    this.sound.play();
  };
  this.pause = function() {
    this.sound.pause();
  };
  this.stop = function() {
    this.sound.pause();
    this.sound.currentTime = 0;
  };
  this.rewind = function() {
    this.sound.currentTime = 0;
  };
  this.ended = function() {
    return this.sound.ended;
  };
  this.volume = function(vol) {
    this.sound.volume = vol;
  };
}

function makeTune() {
  let rnd = Math.floor(Math.random() * 4);
  press(rnd);
}

function rave() {
  if (!gameActive || confirm(warning)) {
    $("#score").html(0);
    gameActive = false;
    raveVars[0] = !raveVars[0];
    beat.play();
    press(Math.floor(Math.random() * 4));
    if (raveVars[0]) {
      raveVars[1] = setInterval(makeTune, crotchet);
      raveVars[2] = setInterval(() => {
        beat.stop();
        beat.play();
      }, crotchet * 16);
    } else {
      clearInterval(raveVars[1]);
      clearInterval(raveVars[2]);
      beat.stop();
      for (let i = 0; i < wubs.length; i++) {
        wubs[i].stop();
      }
    }
  }
}

            
          
!
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.

Console