cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <head>
<link href='https://fonts.googleapis.com/css?family=Alfa+Slab+One' rel='stylesheet' type='text/css'>

<link rel="stylesheet" type="text/css" href="simon.css">
<link href="https://fonts.googleapis.com/css?family=Titillium+Web" rel="stylesheet">

<script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>

<script src = "simon.js" type="text/javascript"></script>
</head>

<body>
  <div class="container" id="game">
    
    <div class="block" id="red"></div>
  <div class="block" id="blue"></div>
    <div class="block" id="yellow"></div>
  <div class="block" id="green"></div>
    <div style = "clear:both"></div>
    <div id = "controller">
      <div id ="simon"><h1 class="brand">Simon<span class="small">&#174</span></h1></div>
      <div class = "buttons">
        <button id = "switch"></button></div>
      <div class = "buttons"><button id = "start" disabled></button></div>
      <div class = "buttons"><button  id = "strict" disabled></button></div>
      <div id = "so"></div>
      <div id = "score"><p id = "tf">--</p></div>
    </div>
  </div>

  <div id = "instructions">
    <h3>Instructions:</h3>
    <p>This is a simple Simon game. Just follow the pattern in the correct order. Do this 20 times to win the game!<br>
    Press the <span id = "b">blue</span> button to switch the game on and off.<br>
    Press the <span id = "r">red</span> button to start the game.<br>
    Press the <span id = "y">yellow</span> button to play in strict mode. When the strict mode is on, the game restarts when you make a mistake. <br>
    <h4>Good luck!</h4>
    </p>
  </div>
  
</body>
            
          
!
            
              body {
  background-color:#c7c7af;
}

.block {
  margin:0 auto;
  border:8px solid #333333;
  float:left;
  height:220px;
  width:220px;
  box-shadow:10px 10px 5px #888888;
}

#red {
  background-color:#9f0f17;
}
#blue {
  background-color:#1b95e0;
}

#green {
  background-color:
#00a74a;
}

#yellow {
  background-color:#cca707;
}
#game {
  margin:0 auto;
   margin-top:40px;
  width:475px;
  height:550px;
}

#controller {
  border-radius:20px;
  width:400px;
  height:70px;
  border:4px solid #333333;
  background-color:white;
  margin:0 auto;
  margin-top:25px;
}

#simon {
  float:left;
}

h1 {
  line-height:25px;
  margin-left:5px;
}

.brand{
  font-family: 'Alfa Slab One', cursive;
  color : #222;
  font-size: 3.2em;
}

.brand .small{
  font-size : 0.3em;
  position : relative;
  top : -20px;
}

button {
   pointer:cursor;
   border:2px solid #333333;
  float:left;
  margin-top:20px;
  margin-left:20px;
  height:20px;
  width:20px;
  border-radius:10px;
  box-shadow:2.5px 2.5px 2px #888888;
}

#strict {
  background-color:#ffff00;
}

#start {
  background-color:#fc0102;
}
#switch {
  background-color:#3193de;
}

#score { 
  font-size:18px;
  color:#430710;
  background-color:#32050c;
  width:50px;
  height:30px;
  float:left;
  text-align:center;
  margin-top:16px;
  margin-left:20px;
  border-radius:8px;
  line-height:24px;
  border:3px solid #333333;
}

#so {
  margin-left:4px;
  margin-top:26px;
  float:left;
  height:8px;
  background-color:black;
  width:8px;
  border-radius:4px;
}
 button:focus {outline:0;}

#instructions {
  width:500px;
  margin: 0 auto;
}

#instructions h3, h4 {
 font-family: 'Alfa Slab One', cursive;
 text-align: center;
}

#instructions p {
  font-family: 'Titillium Web', sans-serif;
}
#instructions #b {
  color: blue;
}

#instructions #y {
  color: yellow;
}

#instructions #r {
  color: red;
}
            
          
!
            
              $(document).ready(function() {
  var play = false;
  var gameReset = false;
  var counter = 0;
  var gameTurns = 0;
  var moveCount = 0;
  var gameOn = false;
  var gameOpen = false;
  var strict = false;
  var score = 0;
  var computerTurn = true;
  var playerSequence = [];
  var sequence = [];
  var ids = [];
  var colors = ['red', 'blue', 'yellow', 'green'];
  var error = new Audio("error.mp3");
  var cheer = new Audio("cheer.mp3");
  var errorMade = false; 
  var audio = {
    red: "https://s3.amazonaws.com/freecodecamp/simonSound1.mp3",
    blue: "https://s3.amazonaws.com/freecodecamp/simonSound2.mp3",
    yellow: "https://s3.amazonaws.com/freecodecamp/simonSound3.mp3",
    green: "https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"
  };
  var changeColor = {
    red: '#9f0f17',
    blue: '#1b95e0',
    yellow: '#cca707',
    green: '#00a74a'
  };

  $('#switch').click(function() {
     clearInterval(play);
    if (!gameOpen) {
      gameOpen = true;
      document.getElementById("start").disabled = false;
      document.getElementById("strict").disabled = false;
      $('#score').css('color', '#dd043b');
    } else {
       document.getElementById("start").disabled = true;
       document.getElementById("strict").disabled = true;
     Funbind();
      $('#score').css('color', '#430710');
      $('#so').css('background-color', 'black');
      gameOpen = false;
      gameOn = false;
      resetGame();
    }
  });

  $('#start').click(function() {
    document.getElementById("start").disabled = true;
    document.getElementById("strict").disabled = true;
    startGame();
  });

  $('#strict').click(function() {
    if (strict) {
      strict = false;
      $('#so').css('background-color', 'black');
    } else {
      $('#so').css('background-color', 'red');
      strict = true;
    }
  });

  var startGame = function() {
    gameOn = true;
    if(score == 20)
    {
      cheer.play();
      alert("You won!");
      setTimeout(function() {
          resetGame();
        },2000);
    }
     else if (computerTurn) {
      setTimeout(function() {
      playSequence();
    }, 500);

    } else if (!computerTurn) {
    Fbind();
      moveCount = 0;
      playerSequence = [];
      $('.block').mousedown(function() {
        var clicked = $(this).attr("id");
        $(this).css('background-color', clicked);
      });
      $('.block').mouseup(function() {
        var clicked = $(this).attr("id");
        $(this).css('background-color', changeColor[clicked]);
      });
      $('.block').click(function() {
        moveCount++;
        var clickedId = $(this).attr("id");
          playerSequence.push(clickedId);
        if(playerSequence[moveCount-1]!=sequence[moveCount-1]) {
        Funbind();
        error.play();
        errorMade = true;
        $("#score").text("!!");
        $("#score").addClass("animated flash infinite");
        setTimeout(function() {
        $("#score").removeClass("animated flash infinite");
        $("#score").text(score);
        }, 800);
        if(errorMade && strict) {
          gameReset = true;
          setTimeout(function() {
          gameReset = false;
          resetGame();
        },800);
        }
        else if(errorMade && !strict) {
          setTimeout(function() {
          playSequence();
        },500);
        }
        }
        else {
            var audioPlay = new Audio(audio[clickedId]);
        audioPlay.play();
        }
        if (moveCount == sequence.length && !errorMade && !gameReset) {
           Funbind();
          score++;
          computerTurn = true;
          $("#score").text(score);
          startGame();
        }
      });
    }
  };

  var playSequence = function() {
   Funbind();
    if(errorMade && !strict) {
        errorMade = false;
    }
    else {
    gameTurns++;
    var random = Math.floor(Math.random() * 4);
    sequence.push(colors[random]);
    ids.push(random);
  }
     moveCount = 0;
     playerSequence = [];
     counter = 0;
     play = setInterval(function() {
      if (counter == sequence.length) {
        clearInterval(play);
        computerTurn = false;
        startGame();
      } else {
        var audioPlay = new Audio(audio[colors[ids[counter]]]);
        audioPlay.play();
        $('#' + sequence[counter]).css('background-color', colors[ids[counter]]);
        setTimeout(function() {
          var color = changeColor[sequence[counter]];
          $('#' + sequence[counter]).css('background-color', color);
          counter++;
        }, 800);
      }
      clearTimeout();
    }, 1000);
  };

  var resetGame = function() {
    Funbind();
    $('#score').text('--');
    errorMade = false;
    counter = 0;
    gameTurns = 0;
    moveCount = 0;
    strict = false;
    score = 0;
    computerTurn = true;
    playerSequence = [];
    sequence = [];
    ids = [];
    gameOn = false;
    $('#score').css('color', '#430710');
    $('#red').css('color', '##9f0f17');
    $('#blue').css('color', '#1b95e0');
    $('#yellow').css('color', 'cca707');
    $('#green').css('color', '#00a74a');
    document.getElementById("strict").disabled = true;
    document.getElementById("start").disabled = true;
    sequence = [];
    ids = [];
    playerSequence = [];
    moveCount = 0;
    if(gameOpen) {
      setTimeout(function() {
        strict = true;
       $("#score").css("color", "red");
        startGame();
      },1000);
    }
  };

  var Fbind = function() {
     $('#red').bind("click mousedown mouseup");
      $('#blue').bind("click mousedown mouseup");
      $('#yellow').bind("click mousedown mouseup");
      $('#green').bind("click mousedown mouseup");
      $('.block').css("cursor","pointer");
  };

  var Funbind = function() {
   $('#red').unbind("click mousedown mouseup");
      $('#blue').unbind("click mousedown mouseup");
      $('#yellow').unbind("click mousedown mouseup");
      $('#green').unbind("click mousedown mouseup");
       $('.block').css("cursor","default");
  };

 
});
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console