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 class = 'container'>
  <div id = 'main'>
    <div class = "game-button" id = 'button1'></div>
    <div class = "game-button" id = 'button2'></div>
    <div class = "game-button" id = 'button3'></div>
    <div class = "game-button" id = 'button4'></div>
  </div>
  
  <div id = 'UI'>
    <button id = 'normal'>Normal</button>
    <button id = 'strict'>Strict</button>
    <p id = 'output'>Count: 0</p>
  </div>
</div>

<audio id = 'audio1'>
  <source src = 'https://s3.amazonaws.com/freecodecamp/simonSound1.mp3' type = 'audio/mpeg'>
</audio>

<audio id = 'audio2'>
  <source src = 'https://s3.amazonaws.com/freecodecamp/simonSound2.mp3' type = 'audio/mpeg'>
</audio>

<audio id = 'audio3'>
  <source src = 'https://s3.amazonaws.com/freecodecamp/simonSound3.mp3' type = 'audio/mpeg'>
</audio>

<audio id = 'audio4'>
  <source src = 'https://s3.amazonaws.com/freecodecamp/simonSound4.mp3' type = 'audio/mpeg'>
</audio>
              
            
!

CSS

              
                #main {
  border: 1px black solid;
  height: 200px;
  width: 300px;
  margin-top: 5%;
  margin-bottom: 5px;
  margin-left: auto;
  margin-right: auto;
}

.game-button {
  border: 1px black solid;
  height: 90px;
  width: 139px;
  margin: 5px;
  float: left;
}

#button1 {
  background-color: yellow;
}

#button2 {
  background-color: red;
}

#button3 {
  background-color: green;
}

#button4 {
  background-color: blue;
}

#UI {
  margin-bottom: 5%;
  text-align: center;
}

#counter {
  visibility: hidden;
}
              
            
!

JS

              
                //game class
var Game = function(mode){
  
  //stage of the game. how many buttons are pressed
  this.stage = 0;
  
  //random sequence for each button
  this.sequence = [];
  
  //mode of the game; either normal or strict
  this.mode = mode;
  
  //randomly finds the next num in series; between 1 and 4 for each button
  this.nextNum = function(){
    this.sequence.push(Math.floor((Math.random() * 4) + 1));
  }
  
  //plays the sequence of the game for the player to listen
  this.playSequence = function(item = 0){
    
    //used to reference the game object inside timers and event listeners
    var self = this;
    
    //function is recursive in order for the buttons to play asynchronously
    if (item == this.sequence.length) {
      
      //if the last item in the sequence has been played then the game listen for the user response
      this.listen();
    } else {
      //plays audio and animation for each respective button in the sequence
      if (this.sequence[item] === 1) {
        
        $('#button1').css('background-color', '#FFFF8C');
        audio1.play();
        
        var timer = setTimeout(function(){
          $('#button1').css('background-color', 'yellow');
          
          //plays the next item in the sequence
          item++;
          self.playSequence(item);
        }, 1000);
        
      } else if (this.sequence[item] === 2) {
        
        $('#button2').css('background-color', '#FF3333');
        audio2.play();
        
        var timer = setTimeout(function(){
          $('#button2').css('background-color', 'red');
          item++;
          self.playSequence(item);
        }, 1000);
        
      } else if (this.sequence[item] === 3) {
        
        $('#button3').css('background-color', '#F48C42');
        audio3.play();
        
        var timer = setTimeout(function(){
          $('#button3').css('background-color', 'green');
          item++;
          self.playSequence(item);
        }, 1000);
        
      } else if (this.sequence[item] === 4) {
        
        $('#button4').css('background-color', '#7C7CFC');
        audio4.play();
        
        var timer = setTimeout(function(){
          $('#button4').css('background-color', 'blue');
          item++;
          self.playSequence(item);
        }, 1000);
        
      }
    }
  }
  
  //listens for the player response
  this.listen = function(item = 0){
    var self = this;
    
    //waits until the last button is pressed and then moves to next stage
    if (item == this.sequence.length) {
      this.nextStage();
    } else {
      
      //if a button is clicked it plays the animation and audio and checks if it's the right button
      $('#button1').click(function(){
        $('#button1').css('background-color', '#FFFF8C');
        audio1.play();
        
        var timer = setTimeout(function(){
          $('#button1').css('background-color', 'yellow');
          
          if (self.sequence[item] === 1) {
            //if it's the right one, wait for the next button
            
            item++;
            self.listen(item);
          } else {
            //if it's wrong it will check if mode is strict or normal and act accordingly
            
            self.wrong();
          }
        }, 1000);
        
        //turns off event listeners
        $('.game-button').off();
      });
      
      $('#button2').click(function(){
        $('#button2').css('background-color', '#FF3333');
        audio2.play();
        
        var timer = setTimeout(function(){
          $('#button2').css('background-color', 'red');
          
          if (self.sequence[item] === 2) {
            item++;
            self.listen(item);
          } else {
            self.wrong();
          }
        }, 1000);
        
        $('.game-button').off();
      });
      
      $('#button3').click(function(){
        $('#button3').css('background-color', '#F48C42');
        audio3.play();
        
        var timer = setTimeout(function(){
          $('#button3').css('background-color', 'green');
          
          if (self.sequence[item] === 3) {
            item++;
            self.listen(item);
          } else {
            self.wrong();
          }
        }, 1000);
        
        $('.game-button').off();
      });
      
      $('#button4').click(function(){
        $('#button4').css('background-color', '#7C7CFC');
        audio4.play();
        
        var timer = setTimeout(function(){
          $('#button4').css('background-color', 'blue');
          
          if (self.sequence[item] === 4) {
            item++;
            self.listen(item);
          } else {
            self.wrong();
          }
        }, 1000);
        
        $('.game-button').off();
      });
    }
  }
  
  //if the player clicks the wrong button it acts according to the mode
  this.wrong = function() {
    var self = this;
    
    console.log(this.mode);
    
    //if mode is normal, the sequence will play again and the player can try again
    //if mode is strict, the game is over
    if (this.mode === "normal") {
      
      $('#output').text('Wrong! Try Again!');
      
      var timer = setTimeout(function(){
        $('#output').text('Counter: ' + self.stage);
        self.playSequence();
      }, 2000);
      
    } else {
      
      $('#output').text('Game Over! Press a mode to play again!');
      this.stop();
      
    }
  }
  
  //moves on to the next stage
  this.nextStage = function(){
    
    //if the game isn't won, keep playing
    if (!(this.checkWin())){
      this.stage++; //advances the stage
      
      $('#output').text("Counter: " + this.stage);
      
      this.nextNum();

      this.playSequence();
      
    } else {
      //if the game is won, stop
      
      this.stop();
      
    }
  }
  
  //if the player reaches stage 20, they win
  this.checkWin = function(){
    if (this.stage === 20) {
      
      $('#output').text("You Win! Press a mode to play again!");
      return true
      
    } else {
      return false;
    }
  }
  
  //brings up the normal and strict buttons to play again
  this.stop = function(){
    
    $('#normal').css('visibility', 'visible');
    $('#strict').css('visibility', 'visible');
  }
  
  //starts the game
  this.start = function(){
    
    this.nextStage();
  }
}

var ui = {};

//audios are stored
ui.audio1 = $('#audio1');
ui.audio2 = $('#audio1');
ui.audio3 = $('#audio1');
ui.audio4 = $('#audio1');

//start screen
$(document).ready(function(){
  //if you click on normal the game will be started on normal state where if you get something wrong you can try again
  //on strict state if you get something wrong you'll automatically lose the game
  
  $('#normal').click(function(){
    $('#normal').css('visibility', 'hidden');
    $('#strict').css('visibility', 'hidden');
    
    globals.game = new Game('normal');
    globals.game.start();
  });
  
  $('#strict').click(function(){
    $('#normal').css('visibility', 'hidden');
    $('#strict').css('visibility', 'hidden');
    
    globals.game = new Game('strict');
    globals.game.start();
  });
});

//global variable for the games
var globals = {};
              
            
!
999px

Console