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.

            
              .container
  .simonDevice
    .greenButton
    .redButton
    .yellowButton
    .blueButton
    .controlls
      h1 Simon
      .counter
        p Count
        .counterScreen 0
      .start
        button.startButton Start
      .strict
        p Strict
        label.switch
          input.strictMode(type="checkbox")
          .slider.round
            
          
!
            
              body
  background-color: #15B7B9
  font-family: sans-serif
  color: white
h1
  text-align: center
  display: block
  width: 50%
  margin: 17px auto
  margin-bottom: 0
.container
  width: 350px
  height: 350px
  margin: 0 auto
  .simonDevice
    position: relative
    border: 10px solid #222222
    box-sizing: border-box
    background-color: gray
    height: 100%
    width: 100%
    border-radius: 50%
    display: flex
    flex-wrap: wrap
    overflow: hidden
    .greenButton
      background-color: green
      border-right: 6px solid #222222
      border-bottom: 6px solid #222222
      box-sizing: border-box
      width: 50%
      height: 50%
    .redButton
      border-bottom: 6px solid #222222
      box-sizing: border-box
      background-color: red
      width: 50%
      height: 50%
    .yellowButton
      background-color: yellow
      border-right: 6px solid #222222
      box-sizing: border-box
      width: 50%
      height: 50%
    .blueButton
      background-color: blue
      width: 50%
      height: 50%
    .controlls
      position: absolute
      background-color: #222222
      flex-wrap: wrap
      overflow: hidden
      height: 200px
      width: 200px
      border-radius: 50%
      top: 50%
      left: 50%
      transform: translate(-50%, -50%)
      .counter
        width: 40%
        display: inline-block
        margin-left: 15px
        
        p
          margin-bottom: 5px
          font-weight: bold
          font-size: 17px
          margin-left: 5px
        .counterScreen
          width: 50px
          height: 25px
          font-size: 20px
          border-radius: 10px
          background-color: red
          border: 2px solid #406661
          text-align: center
          line-height: 25px
      .start
        display: inline-block
        height: 60px
        vertical-align: middle
        margin-top: -20px
        button
          font-size: 20px
          font-weight: bold
          color: white
          background-color: green
          width: 60px
          height: 50px
          margin-top: 5px
          margin-left: 10px
      .strict
        width: 80px
        height: 80px
        display: block
        p
          margin-top: 0
          margin-left: 5px
          font-weight: bold
          font-size: 20px
        .switch
        position: relative
        display: block
        margin: 0 auto
        width: 60px
        height: 34px
        input
          display: none
        .slider
          position: absolute
          cursor: pointer
          height: auto
          width: 100%
          margin-top: 23px
          height: 33px
          top: 0
          left: 0
          right: 0
          bottom: 0
          background-color: #aaa
          -webkit-transition: .4s
          transition: .4s
        .slider:before
          position: absolute
          content: ''
          height: 26px
          width: 26px
          left: 4px
          bottom: 4px
          background-color: white
          -webkit-transition: .4s
          transition: .4s
        input:checked + .slider
          background-color: green
        input:focust + .slider
          box-shadow: 0 0 1px #2196F3
        input:checked + .slider:before
          -webkit-transform: translateX(26px)
          -ms-transform: translateX(26px)
          transform: translateX(26px)
        .round
          border-radius: 34px
        .round:before
          border-radius: 50%
        
        
            
          
!
            
              $('document').ready(function(){
  var simonGame = (function(){
    var _buttonPattern = [];
    var _activeButtonColors = {
      '.greenButton' : '#57D131',
      '.redButton' : '#F23A3A',
      '.yellowButton' : '#FCFF82',
      '.blueButton' : '#0C81F6'
    }
    var _buttons = {
      0: '.greenButton',
      1: '.redButton',
      2: '.yellowButton',
      3: '.blueButton',
    }
    var _classToId = {
      'greenButton': 0,
      'redButton': 1,
      'yellowButton': 2,
      'blueButton': 3
    };
    var _greenButtonAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
    var _redButtonAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3');
    var _yellowButtonAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3');
    var _blueButtonAudio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3');
    var _buttonsAudio = {
      0: _greenButtonAudio,
      1: _redButtonAudio,
      2: _yellowButtonAudio,
      3: _blueButtonAudio
    }
    var _playerIndex = 0;
    var _playerInteraction = false;
    var _strictMode = false;
    var getPlayerInteraction = function(){
      return _playerInteraction;
    }
    var _activateButton = function(buttonId){
      var currentButton = _buttons[buttonId]
      console.log(currentButton);
      var activeColor = _activeButtonColors[currentButton];
      var oldColor = $(currentButton).css('background-color');
      _buttonsAudio[buttonId].currentTime = 0;
      _buttonsAudio[buttonId].play();
      $(currentButton).css('background-color', activeColor);
      setTimeout(function(){
        $(currentButton).css('background-color', oldColor);
      }, 1000);
    }
    var setStrictMode = function(mode){
      _strictMode = mode;
      console.log(_strictMode);
    }
    var _restartGame = function(){
      _buttonPattern = [];
    }
    var _playPattern = function(){
      var i = 0;
      _playerInteraction = false
      var playInterval = setInterval(function(){
        _activateButton(_buttonPattern[i]);
        i++;
        if(i >= _buttonPattern.length){
          clearInterval(playInterval);
          setTimeout(function(){
            _playerInteraction = true;
          }, 1500);
        }
      }, 1500)
    }
    var _increasePattern = function(){
      _buttonPattern.push(Math.floor(Math.random() * 4));
      $('.counterScreen').text(_buttonPattern.length);
      console.log(_buttonPattern);
    }
    var startGame = function(){
      _restartGame();
      _increasePattern();
      _playPattern();
    }
    var playerAction = function(buttonClass){
      var buttonId = _classToId[buttonClass];
      _buttonsAudio[buttonId].play();
      if(buttonId != _buttonPattern[_playerIndex]){
        $('.counterScreen').text('! !');
        _playerInteraction = false;
        setTimeout(function(){
          
          if(_strictMode){
          _buttonPattern = [];
          _increasePattern();
        } else {
          $('.counterScreen').text(_buttonPattern.length);
        }
        _playerIndex = 0;
        _playPattern();
        console.log('wrong');
        }, 3000)
      } else {
        _playerIndex++;
      }
      
      if(_playerIndex >= _buttonPattern.length){
        _playerIndex = 0;
        _increasePattern();
        _playPattern();
        console.log('nextLevel()');
      }
      console.log(buttonId);
    }
    return {
      setStrictMode,
      startGame,
      playerAction,
      getPlayerInteraction
    };
  })();
  
  var buttonHandler = function(button, color){
    if(simonGame.getPlayerInteraction()){
      simonGame.playerAction($(button).attr('class'));
      $(button).css('background-color', color);
    }
  }
  $('.strictMode').change(function(){
    if(this.checked){
      simonGame.setStrictMode(true);
    } else {
      simonGame.setStrictMode(false);
    }
  });
  $('.startButton').click(function(){
    simonGame.startGame();
  });
  $('.greenButton').mousedown(function(){
    buttonHandler(this, '#57D131');
  });
  $('.greenButton').mouseup(function(){
    $(this).css('background-color', 'green');
  });
  $('.redButton').mousedown(function(){
    buttonHandler(this, '#F23A3A');
  });
  $('.redButton').mouseup(function(){
    $(this).css('background-color', '#FF0000');
  });
  $('.yellowButton').mousedown(function(){
    buttonHandler(this, '#FCFF82');
  });
  $('.yellowButton').mouseup(function(){
    $(this).css('background-color', 'yellow')
  });
  $('.blueButton').mousedown(function(){
    buttonHandler(this, '#0C81F6');
  });
  $('.blueButton').mouseup(function(){
    $(this).css('background-color', '#06648C');
  });
  $('div').click(function(){
    console.log($(this).attr('class'));
  });
});
            
          
!
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