Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                %div{ :align => 'center' }
  .base
    .center
      .controls
        %h1 
          Simon
          %sup ®
        .round-count --
        %p Round
        .on-off
          .on
          .off
          %p Off
          %p On
        .start
        %p Start
        .strict
        .strict-led
        %p Strict
    .simon-row
      .green{ 'data-index' => 0 }
      .red{ 'data-index' => 1 }
    .simon-row
      .yellow{ 'data-index' => 2 }
      .blue{ 'data-index' => 3 }
              
            
!

CSS

              
                body
  background-color: #ffb54b

.color-active
  cursor: pointer
  background: radial-gradient(rgba(255,255,255,0.5), rgba(255,255,255,0))
  
.green
  border-top-left-radius: 100%
  background-color: green
  
.red
  border-top-right-radius: 100%
  background-color: red
  
.yellow
  border-bottom-left-radius: 100%
  background-color: yellow
  
.blue
  border-bottom-right-radius: 100%
  background-color: blue
  
.green, .red, .yellow, .blue
  position: relative
  display: inline-block
  width: 225px
  height: 225px
  z-index: 10

.base
  background-color: #333
  border-radius: 50%
  width: 500px
  height: 500px
  box-shadow: 0 0 15px 0 rgba(0,0,0,1)
  
  .simon-row
    position: relative
    top: 24px
    
.center
  width: 200px
  height: 200px
  background-color: #333
  border-radius: 50%
  position: relative
  top: 150px
  margin-bottom: -200px
  z-index: 15
  
.controls
  background-color: white
  width: 190px
  height: 190px
  border-radius: 50%
  position: relative
  top: -25px
  left: -25px
  font-family: 'Squada One', monospace
  z-index: 5
  border: 25px solid #333
  
h1
  color: #222
  padding: 0
  margin: 0
  display: inline
  position: relative
  left: 33px
  top: 11px
  
  sup
    font-family: monospace
    
.round-count
  position: relative
  display: inline-block
  background-color: #222
  padding: 3px
  border-radius: 3px
  color: #555
  top: 43px
  left: -45px
  font-family: 'Share Tech Mono', monospace
  font-weight: bold
  
p
  position: relative
  top: 43px
  left: -110px
  display: inline-block
  
.on-off
  background-color: #222
  position: relative
  width: 25px
  height: 10px
  top: 20px
  left: 43px
  
  >p:nth-child(3)
    position: relative
    top: -42px
    left: -15px
  
  >p:last-child
    position: relative
    top: -91px
    left: 13px
  
.on, .off
  width: 10px
  height: 10px
  position: relative
  cursor: pointer
    
  
.on
  left: 8px
  display: none
  background-color: green
  
  &:hover
    background-color: darkgreen
  
.off
  display: block
  left: -7px
  background-color: red
  
  &:hover
    background-color: darkred
  
.start
  display: inline-block
  width: 32px
  height: 32px
  background-color: #555
  border-radius: 50%
  position: relative
  top: 60px
  left: -20px
  
p:nth-child(6)
  position: relative
  top: 25px
  left: -54px

.strict
  width: 32px
  height: 32px
  background-color: #555
  border-radius: 50%
  position: relative
  top: 9px
  left: 40px

.strict-led
  width: 8px
  height: 8px
  background-color: #222
  border-radius: 50%
  position: relative
  top: -10px
  left: 67px
  
p:nth-child(9)
  position: relative
  top: -65px
  left: 40px
              
            
!

JS

              
                function SimonGame () {
  this.var = {};
  this.var.start_cycles  = -1;
  this.var.round         = 0;
  this.var.strict        = false;
  this.var.start         = false;
  this.var.sequence      = [];
  this.var.user_sequence = 0;
  
  this.audio = [ new Audio ('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3'),
                 new Audio ('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3'),
                 new Audio ('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3'),
                 new Audio ('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3') ];
 
  this.obj = {};
  this.obj.btn       = [ $('.green'), $('.red'), $('.yellow'), $('.blue') ];
  this.obj.start     = $('.start');
  this.obj.strict    = $('.strict');
  this.obj.strictLed = $('.strict-led');
  this.obj.round     = $('.round-count');
  this.obj.on        = $('.on');
  this.obj.off       = $('.off');

  /* functions */
  var self = this;
  
  this.on = function () {
    self.var.start_cycles = 0;
    var int_id = setInterval (function () {
      self.obj.off.hide ();
      self.obj.on.show ();
      
      if ( self.var.start_cycles == 6 ) {
           self.var.start_cycles = -1;
        
           self.var.start_cycles  = -1;
           self.var.round         = 0;
           self.var.strict        = false;
           self.var.start         = false;
           self.var.sequence      = [];
           self.var.user_sequence = 0;
        
           self.obj.start.css ({ 'background-color': 'red', 'cursor': 'pointer' });
           self.obj.strict.css ({ 'background-color': 'yellow', 'cursor': 'pointer' });
        
           self.obj.start.bind ('click', self.start);
           self.obj.strict.bind ('click', function () {
              self.var.strict = self.var.strict === false;
             
              if ( self.var.strict )
                   self.obj.strictLed.css ({ 'background-color': 'red' });
              else self.obj.strictLed.css ({ 'background-color': '#222' });
           });
        
           clearInterval (int_id);
           return;
      }
      
      var color = ( self.var.start_cycles % 2 ) ? '#e00' : '#400';
      self.obj.round.css ({ 'background-color': '#400', 'color': color });
      self.var.start_cycles++;
    }, 500);
  };

  this.off = function () {
    var int_id = setInterval (function () {
       if ( self.var.start_cycles == -1 ) {
            self.obj.round.css ({ 'background-color': '#222', 'color': '#555' }); 

            self.var.round        = 0;
            self.var.strict       = false;
            self.var.start        = false;
            self.var.sequence     = []; 
          
            self.obj.on.hide ();
            self.obj.off.show ();
            self.obj.round.text ("--");
            self.obj.strictLed.css ({ 'background-color': '#222' });  
            self.obj.start.css ({ 'background-color': '#555', 'cursor': 'default' });
            self.obj.strict.css ({ 'background-color': '#555', 'cursor': 'default' });
          
            self.obj.start.unbind ('click');
            self.obj.strict.unbind ('click');
         
            for ( var j = 0; j < 4; ++j ) {
              self.obj.btn[j].unbind ('click');
              self.obj.btn[j].css ({ 'cursor': 'default' });
              self.obj.btn[j].removeClass ('color-active');
              self.audio[j].pause ();
              self.audio[j].currentTime = 0;
            }
         
            clearInterval (int_id);
       }
    }, 100);
  };
  
  this.start = function () {
    self.var.sequence = [];
    self.var.sequence.push (Math.round (Math.random () * 3));
 
    self.var.round = 1;   
    for ( var j = 0; j < 4; ++j ) {
          self.obj.btn[j].unbind ('click');
          self.obj.btn[j].css ({ 'cursor': 'default' });
          self.obj.btn[j].removeClass ('color-active');
          self.audio[j].pause ();
          self.audio[j].currentTime = 0;
    }
    self.play_sequence ();
  };
  
  this.display = function (n) { 
      if ( n < 10 ) return '0' + n;
      
      return n;
  };
                               
  this.play_sequence = function () {    
    self.obj.round.text (self.display (self.var.round));
    var curr_sequence_pos = 0;
    
    var int_id = setInterval (function () {
      if ( curr_sequence_pos >= self.var.sequence.length ) {
           if ( curr_sequence_pos == self.var.sequence.length )
                self.obj.btn[self.var.sequence[curr_sequence_pos - 1]].removeClass ('color-active');
           
           self.var.user_sequence = 0;
           for ( var j = 0; j < 4; ++j ) {
                 self.obj.btn[j].css ({ 'cursor': 'pointer' });
                 self.obj.btn[j].bind ('click', self.btn_clicked);
           }
        
           clearInterval (int_id);
           return;
      }
      
      if ( curr_sequence_pos > 0 ) {
           self.obj.btn[self.var.sequence[curr_sequence_pos - 1]].removeClass ('color-active');
           self.audio[self.var.sequence[curr_sequence_pos - 1]].pause ();
           self.audio[self.var.sequence[curr_sequence_pos - 1]].currentTime = 0;
      }
      
      var curr_index = self.var.sequence[curr_sequence_pos];
      setTimeout (function () {
        self.audio[curr_index].play ();
        self.obj.btn[curr_index].addClass ('color-active');
      }, 100);
      
      curr_sequence_pos++;
    }, 700);
  };
  
  this.btn_clicked = function () {
    var index = $(this).data ('index');
    
    self.obj.btn[index].addClass ('color-active');
    self.audio[index].play ();
    
    setTimeout (function () {
      self.obj.btn[index].removeClass ('color-active');
      
      if ( self.var.sequence[self.var.user_sequence] != index ) {
           self.var.user_sequence = 0;
           for ( var j = 0; j < 4; ++j ) {
                 self.obj.btn[j].css ({'cursor': 'default'});
                 self.obj.btn[j].unbind ('click');
           }
        
           var blink = 0;
           var int_id = setInterval (function () {
             if ( blink == 7 ) {
                  if ( self.var.strict ) 
                       self.start ();
                  else self.play_sequence ();
                  
                  self.obj.round.text (self.display (self.var.round));
               
                  clearInterval (int_id);
                  return;
             }
             
             self.obj.round.text ((blink % 2) ? '!!' : '--' );
             blink++;
           }, 500);
      }
      else {
           self.var.user_sequence++;
           if ( self.var.user_sequence == self.var.sequence.length ) {
                self.var.user_sequence = 0;
        
                for ( var j = 0; j < 4; ++j ) {
                      self.obj.btn[j].css ({'cursor': 'default'});
                      self.obj.btn[j].unbind ('click');
                }
        
                self.var.sequence.push (Math.round (Math.random () * 3));
 
                self.var.round++;   
                if ( self.var.round > 20 ) {
                     var blink = 0;
                     var int_id = setInterval (function () {
                       if ( blink == 7 ) {
                            self.start ();
                  
                            self.obj.round.text (self.display (self.var.round));
               
                            clearInterval (int_id);
                            return;
                       }
                       
                       self.obj.round.text ((blink % 2) ? '**' : '--' );
                       blink++;
                     }, 500);
                }
                else self.play_sequence ();
           }
      }
    }, 500);
  };
  
  /* events */
  this.obj.off.click (this.on);
  this.obj.on.click (this.off);
}

$(document).ready (function () {
  var game = new SimonGame ();  
  
});
              
            
!
999px

Console