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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <link href="https://fonts.googleapis.com/css?family=Montserrat+Subrayada" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Baloo" rel="stylesheet">


<body>
  <div class='container'>
    <svg class='gameBoard'></svg>
  </div>
</body>
              
            
!

CSS

              
                body{
  text-align: left;
  background: black;
  webkit-touch-callout: none;
  webkit-user-select: none;
  khtml-user-select: none;
  moz-user-select: none;
  ms-user-select: none;
  user-select: none;
  cursor: default;
}

.gameBoard{
  margin-left:20%;
  margin-top: 50px;
}

.button, #onOff, #mode {
  cursor: pointer;
}

#counter {
  fill: white;
  font-family: Baloo, sans-serif;
  font-size: 225px;
  text-anchor: middle;
}

#nMode, #nOnOff {
  font-size: 10px;
  fill: white;
  font-family: Baloo, sans-serif; 
  text-anchor: middle;
}

#box, #main{
  fill: black;
} 
              
            
!

JS

              
                var green = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
var red = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3");
var blue = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3");
var yellow = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");

var width = 300, height = 600;
var fullAngle = 2 * Math.PI, running = false;

var count = 0, master = [], mode = false, onOff = false, lose = false;

var simon = d3.select(".gameBoard")
        .attr("width", width)
        .attr("height", height);

buttonSetup();
interfaceSetup();

function buttonSetup(){
  var arc1 = d3.arc()
          .innerRadius(0)
          .outerRadius(width)
          .startAngle(0)
          .endAngle(fullAngle / 4);

  var arc2 = d3.arc()
          .innerRadius(0)
          .outerRadius(width)
          .startAngle(fullAngle / 4)
          .endAngle(fullAngle/2);

  var arc3 = d3.arc()
          .innerRadius(0)
          .outerRadius(width)
          .startAngle(fullAngle/2)
          .endAngle(fullAngle*3/4);

  var arc4 = d3.arc()
          .innerRadius(0)
          .outerRadius(width)
          .startAngle(-fullAngle/4)
          .endAngle(0);

  var buttonA = simon.append("path")
          .attr('class', 'button')
          .attr('id', 'red')
          .attr("d", arc1())
          .attr("fill", "red")
          .on('mousedown', function(){
            if(running==false&&onOff==true){
              running=true; lights(2); 
              setTimeout(function(){running=false;},410);
              if(count<master.length){
                check(2);
              }
            }
          });

  var buttonB = simon.append("path")
          .attr('class', 'button')
          .attr('id', 'blu')
          .attr("d", arc2())
          .attr("fill", "blue")
          .on('mousedown', function(){
            if(running==false&&onOff==true){
              running=true; lights(4); 
              setTimeout(function(){running=false;},410);
              if(count<master.length){
                check(4);
              }
            }
          });

  var buttonC = simon.append("path")
          .attr('class', 'button')
          .attr('id', 'yel')
          .attr("d", arc3())
          .attr("fill", "yellow")
          .on('mousedown', function(){
            if(running==false&&onOff==true){
              running=true; lights(3); 
              setTimeout(function(){running=false;},410);
              if(count<master.length){
                check(3);
              }
            }
          });          

  var buttonD = simon.append("path")
          .attr('class', 'button')
          .attr('id', 'gre')
          .attr("d", arc4())
          .attr("fill", "green")
          .on('mousedown', function(){
            if(running==false&&onOff==true){
              running=true; lights(1); 
              setTimeout(function(){running=false;},410);
              if(count<master.length){
                check(1);
              }
            }
          });

  buttonA.attr("transform", "translate(" + width/2 + "," + height/2 + ")");
  buttonB.attr("transform", "translate(" + width/2 + "," + height/2 + ")");
  buttonC.attr("transform", "translate(" + width/2 + "," + height/2 + ")");
  buttonD.attr("transform", "translate(" + width/2 + "," + height/2 + ")");
}//buttonSetup end
function interfaceSetup(){
  simon.append('rect')
        .attr('class', 'interface')
        .attr('id', 'box')
        .attr('x', width/2-20)
        .attr('y', 0)
        .attr('height', height)
        .attr('width', 40)
  
  simon.append('rect')
        .attr('class', 'interface')
        .attr('id', 'box')
        .attr('x', 0)
        .attr('y', width-20)
        .attr('height', 40)
        .attr('width', width)
  
  simon.append('circle')
        .attr('class', 'interface')
        .attr('id', 'main')
        .attr('r', 140)
        .attr('cx', width/2)
        .attr('cy', width)
        .style('fill', 'black');
  
  simon.append('text')
        .attr('id', 'counter')
        .attr('x', width/2)
        .attr('y', width + 65)
        .text('00');
  
  simon.append('rect')
        .attr('class', 'interface')
        .attr('id', 'onOff')
        .attr('height', 10)
        .attr('width', 38)
        .attr('x', width/2 + 2)
        .attr('y', 380)
        .style('fill', 'firebrick')
        .on('mousedown', function(){
          if(onOff==false){d3.select(this).style('fill', 'greenyellow'); onOff = true; game();}
          else{
            d3.select(this).style('fill', 'firebrick'); onOff = false; reset();};
        });
  
  simon.append('text')
        .attr('id', 'nOnOff')
        .attr('x', width/2 + 2+18)
        .attr('y', 380+20)
        .text('On/Off');
  
  simon.append('rect')
        .attr('class', 'interface')
        .attr('id', 'mode')
        .attr('height', 10)
        .attr('width', 34)
        .attr('x', width/2-34-2)
        .attr('y', 380)
        .style('fill', 'firebrick')
        .on('click', function(){
          if(mode==false){d3.select(this).style('fill', 'greenyellow'); mode = true;reset();game();}
          else{d3.select(this).style('fill', 'firebrick'); mode = false; reset(); game();};
        });
  
  simon.append('text')
        .attr('id', 'nMode')
        .attr('x', width/2-34+15)
        .attr('y', 380+20)
        .text('Strict');
}//interfaceSetup end

function random(){//generates a random number between 1 & 4
  var number = Math.floor(Math.random()*10);
  while(number<1||number>4){
    number = Math.floor(Math.random()*10);
  }
  return number;
}//random end
function reset(){//resets all counters and states
  count = 0, master = [], running = false, lose = false;
  d3.select('#counter').text('00').style('text-size', '225px');
}//reset end
function game(){//set's & displays sequence values
  if(onOff==true){
    running=true;
    var one = random();
    master.push(one); console.log(master);
    setTimeout(function(){running=false}, master.length*550);  
    (function theLoop (i) {
      setTimeout(function () {
        lights(master[i]);
        if (i<master.length-1) {
          i++; theLoop(i); 
        }
      }, 600); 
    })(0);
  }
}//game end
function lights(num){//activates visual & audio response
  switch(num){
          case 1:
            d3.select('#gre') 
              .transition().duration(300).attr('fill', 'palegreen')
              .transition().duration(100).attr('fill', 'green');
              green.play(); break;
          case 2:
            d3.select('#red')
              .transition().duration(300).attr('fill', 'lightsalmon')
              .transition().duration(100).attr('fill', 'red');
              red.play(); break;
          case 3: 
            d3.select('#yel')
              .transition().duration(300).attr('fill', 'lemonchiffon')
              .transition().duration(100).attr('fill', 'yellow');
              yellow.play(); break;
          case 4:
            d3.select('#blu')
              .transition().duration(300).attr('fill', 'lightblue')
              .transition().duration(100).attr('fill', 'blue');
              blue.play(); break;
        }
}//lights end
function check(val){//checks if user input matches sequence
  if(onOff==true){
    if(master[count]!=val){//wrong input sequence
      if(mode==false){//used for regular mode
        console.log('nope, try again: ' + master[count] + ' : ' + val);
        setTimeout(function(){lose=true; events(); lose=false},1000);
      }
      else{ //used for strict mode
        setTimeout(function(){lose=true; events();},1000);
        setTimeout(function(){reset(); game();},2300);
      }
    }
    else{//corect input sequence
      console.log('nooice');
      count++;
      if(count>=master.length){
        if(master.length>99){d3.select('#counter').style('text-size', '112px');}
        d3.select('#counter').text(function(){//counter display formating
          if(count<10){return '0'+ count;}
          else{return count;}
        }); 
        setTimeout(function(){events();},300);
        count = 0;
        setTimeout(function(){game();},1600);
      } 
    }             
  }
}//check end
function events(){
    running=true;
    setTimeout(function(){running=false;}, 1225);
  if(lose==false){//indicates completion of sequence
    d3.select('#counter') 
              .transition().duration(500).style('opacity', 0)
              .transition().duration(100).style('opacity', 1)
              .transition().duration(500).style('opacity', 0)
              .transition().duration(100).style('opacity', 1);
  }
  else{//indicates loss in strict more
    d3.select('#counter')
              .transition().duration(200).style('opacity', 0)
              .style('fill', 'lightsalmon')
              .transition().duration(100).style('opacity', 1)
              .transition().duration(200).style('opacity', 0)
              .transition().duration(100).style('opacity', 1)
              .transition().duration(200).style('opacity', 0)
              .transition().duration(100).style('opacity', 1)
              .transition().duration(200).style('opacity', 0)
              .transition().duration(100).style('opacity', 1)
              .style('fill', 'white');    
  }
}//events end






              
            
!
999px

Console