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.

            
              <canvas id="canvas"></canvas>
	<div id="victory"> </div>
	<div id="game">
		<div id="green" class="game-btn"></div>
		<div id="red" class="game-btn"></div>
		<div id="yellow" class="game-btn"></div>
		<div id="blue" class="game-btn"></div>
		<div id="controls"> 
			<p id="game-title">SIMON</p> 
			 <div id="count-display"><span class="count"></span></div>
			 <div id="start" class="button-container"> 
			 	<div class="button" id="start-btn"> 
			 	</div> 
			 start
			 </div>
			 <div id="reset" class="button-container"> 
			 	<div class="button" id="reset-btn"> 
			 	</div> 
			 reset 
			 </div>
			 <div id="strict">
				<label class="switch">
  				<input type="checkbox" class="slider-check">
  				<div class="slider round"></div>
				</label>
			  <span class="title">strict</span></div> 
		</div>
	</div>
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Luckiest+Guy);
@import url(https://fonts.googleapis.com/css?family=Roboto+Mono:400,700);

$red-base: darken(#CB0000, 10%);
$red-highlight: #FF0000;
$blue-base: darken(#15018D, 10%);
$blue-highlight: #0000FF;
$yellow-base: darken(#CBB000, 10%);
$yellow-highlight: #FFFF00;
$green-base: #00711E;
$green-highlight: #00FF00;
$silver: #A9A9A9;
$background:  #1f2e2e;
$victory-message: #42857C;

@mixin centerer {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

html {
	background-color: $background;
}

#game {
	font-family: 'Luckiest Guy', cursive;
	@include centerer;
	background-color: black;
	border-radius: 100%;
	height: 36em;
	width: 36em;
	z-index: 0;
}

#controls {
	@include centerer;
	background-color: $silver;
	border-radius: 100%;
	height: 12em;
	width: 12em;
	text-align: center;
	border: 1em solid #111;
	#game-title {
		font-size: 1.6em;
		margin-top: 0.5em;
		margin-bottom: 0;
	}
	z-index: 2;
}

.game-btn {
	// border-radius: 100%;
	cursor: pointer;
	border: 1em solid #111;
	margin: -0.5em;
	height: 17em;
	width: 17em;
	float: left;
	z-index: 1;
}

.lightup {
	&#green {
		background-color: $green-highlight;
	}
	&#red{
		background-color: $red-highlight;
	}
	&#yellow{
		background-color: $yellow-highlight;
	}
	&#blue{
		background-color: $blue-highlight;
	}
}

#green {
	background-color: $green-base;
	border-top-left-radius: 100%;

}

#red{
	background-color: $red-base;
	border-top-right-radius: 100%;
}

#yellow{
	background-color: $yellow-base;
	border-bottom-left-radius: 100%;
}


#blue{
	background-color: $blue-base;
	border-bottom-right-radius: 100%;
}


#count-display{
	background-color: black;
	color: red; 
	z-index: 3;
	height: 2.6em;
	width: 3em;
	margin: 0 auto;
	border: 4px solid darken($silver, 20%);
	border-radius: 10px;
	.count {
		font-weight: bold;
		font-family: 'Roboto Mono', ;
		font-size: 2em;
	}
}


.button{
	background-color: $yellow-base;
	height: 1.5em;
	width: 1.5em;
	border-radius: 100%;
	margin: auto;
}

.button-container {
	width: 40%;
	margin: -5% 5% 0 5%;
	float: left;
	cursor: pointer;
}

#strict {
	position: relative;
	clear: both;
	padding: 1.2em;
	.title {
		position: absolute;
		top: 0;
		left: 37%;
	}
}

#victory {
	@include centerer;
	z-index: 5; 
	font-family: 'Luckiest Guy', cursive;
	font-size: 4em;
	text-align: center;
	color: $victory-message;
	text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}

//Slider 

/* The switch - the box around the slider */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

/* Hide default HTML checkbox */
.switch input {display:none;}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: $red-base;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 1.5em;
  width: 1.5em;
  left: 4px;
  bottom: 4px;
  background-color: $silver;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: $green-base;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

canvas {
	display: block;
	z-index: 100;
}
            
          
!
            
              // Configuration
let count = 0;
let simonMoves = []; 
let playerMoves = [];
let strict = false;
let currentTurn;
let playBack;
const winTurn = 20;
const board = ["green", "red", "yellow", "blue"];
const audio = [ 
  new Audio("http://git.bbeckert.com/simon/sounds/simonSound1.mp3"), 
  new Audio("http://git.bbeckert.com/simon/sounds/simonSound2.mp3"), 
  new Audio("http://git.bbeckert.com/simon/sounds/simonSound3.mp3"), 
  new Audio("http://git.bbeckert.com/simon/sounds/simonSound4.mp3"), 
  new Audio("http://git.bbeckert.com/simon/sounds/buzzer.mp3"), 
  new Audio("http://git.bbeckert.com/simon/sounds/victory.mp3")
];


let flash = (color) => {
  audio[board.indexOf(color)].play();
  $("#" + color).toggleClass('lightup');
  setTimeout(function(){
    $("#" + color).toggleClass('lightup');
  }, 1000);
}

let initialize = () => {
  count = 0; 
  simonMoves = [];
  playerMoves = [];
  currentTurn = "Simon";
  clearInterval(playBack);
  $(".count").html(count);
  console.log("game reset");
};

let start = () => {
  initialize();
  turn();
};


let toggleStrict =() => {
  if(strict) strict = false;
  else strict = true;
  console.log("Strict Mode: " + strict);
  start();
};

let turn = () => {
  count ++;
  $(".count").html(count);
  let simonColor = board[Math.floor(Math.random() * 4)];
  simonMoves.push(simonColor);
  simonPlay();
};

let simonPlay = () => {
  playerMoves = [];
  currentTurn = "Simon";
  // Make a copy so it doesn't mutate
  let colors = simonMoves.slice();
  //takes a color from the front of the array and flashes it.
  playBack = setInterval(function(){
    let color = colors.shift();
    flash(color);
    if(colors.length === 0) {
      clearInterval(playBack);
      currentTurn = "Player";
    }
  }, 1200);
}

let playerPlay = (color) => {
  playerMoves.push(color);
  flash(color);
  // Check for matches
  let match = color === simonMoves[playerMoves.length - 1];
  if(!match) {
    audio[4].play();
    if(!strict) return simonPlay();
    if(strict) return start();
  } else if(playerMoves.length === simonMoves.length) {
    if(playerMoves.length == winTurn) return win();
    turn();
  }
}

let win = () => {
  $("#victory").html("Congratulations, You Win!");
  audio[5].play();
  StartConfetti();
  setTimeout(function(){
    $("#victory").html("");
    StopConfetti();
    start();
  }, 4500);
};

$('#game').on('click', '.game-btn', function(e) {
  if(currentTurn != "Player") return;
  let color = e.target.getAttribute("id");
  playerPlay(color);
});

$(".slider").click(function(){
    toggleStrict();
});

$("#start-btn").click(function() {
  start();
});

$("#reset-btn").click(function() {
  initialize();
});

            
          
!
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