cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-personicon-teamoctocatpop-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.

            
              <div class="container">
  <div id="msg"></div>
  <div id="btn1" class="sndbtn"></div>
  <div id="btn2" class="sndbtn"></div>
  <div id="btn3" class="sndbtn"></div>
  <div id="btn4" class="sndbtn"></div>
  <div class="tgl_wrapper">
	  <button id="gm_tgl"       class="start">Start</button>
	  <button id="strct_tgl">Strict:   off</button>
  </div>
</div>

            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Roboto');

* {
    font-family: 'Roboto', 'Helvetica', sans-serif;
}

body, html {
    width: 100%;
    margin: 0;
    padding: 0;
}

body {
    background-color: #111;
}

.container {
    width: 480px;
    height: 480px;
    margin: 20px auto 0 auto;
    padding: 10px 10px;

    text-align: center:
}

#msg {
    width: 200px;
    height: 50px;
    line-height: 50px;
    text-align: center;

    font-size: 24px;

    background-color: #333;
    color: #aaa;

    border-radius: 1px;
    
    margin: 10px auto;
}

.sndbtn {
    width: 220px;
    height: 150px;
    display: inline-block;
    float: left;
    margin: 5px 5px;

    border-radius: 20px;
    border: 5px solid;
}

.tgl_wrapper {
    width: 220px;
    margin: 0 auto;

    text-align: center;
}

.tgl_wrapper button {
    width: 100px;
    height: 50px;
    font-size: 18px;    

    float: left;
    display: inline;

    border: 3px solid #aaa;
    border-radius: 2px;

    margin: 20px 5px;
    text-align: center;


    color: #aaa;
    background-color: #111;
}

.tgl_wrapper button:hover {
    background-color: #aaa;
    color: #111;
}

#btn1 {
    border-color: rgb(83, 116, 84);
}

#btn1:hover, #btn1.glow {
    background-color: rgb(83, 116, 84);
}

#btn2 {
    border-color: #983636;
}

#btn2:hover, #btn2.glow {
    background-color: #983636;
}

#btn3 {
    border-color: #bdab5f;
}

#btn3:hover, #btn3.glow {
    background-color: #bdab5f;
}

#btn4 {
    border-color: rgb(71, 86, 120);
}

#btn4:hover, #btn4.glow {
    background-color: rgb(71, 86, 120);
}
            
          
!
            
              $(document).ready(function() {

    // levels player will go through
    const levels = [[1],[1,4],[1,4,3],[1,4,3,4],[1,4,3,4,4],
		    [1,4,3,4,4,2],[1,4,3,4,4,2,3],[1,4,3,4,4,2,3,4],
		    [1,4,3,4,4,2,3,4,1],[1,4,3,4,4,2,3,4,1,2],
		    [1,4,3,4,4,2,3,4,1,2,3],[1,4,3,4,4,2,3,4,1,2,3,4],
		    [1,4,3,4,4,2,3,4,1,2,3,4,1],
		    [1,4,3,4,4,2,3,4,1,2,3,4,1,2],
		    [1,4,3,4,4,2,3,4,1,2,3,4,1,2,3],
		    [1,4,3,4,4,2,3,4,1,2,3,4,1,2,3,4],
		    [1,4,3,4,4,2,3,4,1,2,3,4,1,2,3,4,2],
		    [1,4,3,4,4,2,3,4,1,2,3,4,1,2,3,4,2,3],
		    [1,4,3,4,4,2,3,4,1,2,3,4,1,2,3,4,2,3,4],
		    [1,4,3,4,4,2,3,4,1,2,3,4,1,2,3,4,2,3,4,1]];

    var level = 0,
	playerClicks = [],
	gameOn = false,
	strictMode = false,
	speed = 600;

    // toggle the game between strict and not strict
    $('#strct_tgl').click(function() {
	
	if (strictMode) {
	    strictMode = false;
	    $(this).html('Strict: off');
	} else {
	    strictMode = true;
	    $(this).html('Strict: on');
	}

	reset();
    });

    $('#gm_tgl').click(function() {

	if (!gameOn)
	{
	    gameOn = true;
	    $(this).attr('class', 'reset');
	    $(this).html('Reset');
	    showPattern();
	}
	else
	{
	    gameOn = false;
	    $(this).attr('class', 'start');
	    $(this).html('Start');
	    reset();
	}
	
    });

/***************************************************************

---------------------------HELPERS-----------------------------

***************************************************************/
    function showPattern() {
	if (gameOn) {
	    setTimeout(displayMessage(level+1), 3000);
	    if (level > 6)
		speed = 400;
	    else if (level > 12)
		speed = 200;
	    let i = 0;
	    let pattern = setInterval(function() {
		
		glowButton(levels[level][i]);
		playSound(levels[level][i]);
		
		i++;
		
		if (i >= levels[level].length)
		    clearInterval(pattern);
		
	    }, speed);

	    playerClicks = [];
	    playerMove();
	}
    }

    function glowButton(btn) {
	$('#btn'+btn).addClass('glow');
	setTimeout(function() {
	    $('#btn'+btn).removeClass('glow');
	}, 200);
    }

    function playerMove() {
	if (gameOn) {
	    $('.sndbtn').off().click(function() {
		switch (this.id) {
		case 'btn1':
		    playSound(1);
		    playerClicks.push(1);
		    break;
		case 'btn2':
		    playSound(2);
		    playerClicks.push(2);
		    break;
		case 'btn3':
		    playSound(3);
		    playerClicks.push(3);
		    break;
		case 'btn4':
		    playSound(4);
		    playerClicks.push(4);
		    break;
		}

		if (playerClicks.length <= levels[level].length) {
		    checkClicks();
		}
	    });
	}
    }

    function checkClicks() {
	if (playerClicks[playerClicks.length-1]
	    !== levels[level][playerClicks.length-1]) {
	    if (strictMode) {
		displayMessage('Wrong! Start Over');
		setTimeout(startOver, 1000);
	    } else {
		displayMessage('Wrong! Try Again');
		setTimeout(tryAgain, 1000);
	    }
	} else if (playerClicks.length == levels[level].length) {
	    displayMessage('Level Up!');
	    setTimeout(levelUp, 1000);
	} else {
	    playerMove();
	}
    }

    function startOver() {
	playerClicks = [];
	level = 0;
	showPattern();
    }

    function tryAgain() {
	showPattern();
    }
    
    function levelUp() {
	if (level == 19) {
	    displayMessage('You Won!');
	    return;
	} else {
	    level++;
	    showPattern();
	}
    }
    
    function displayMessage(message) {
	$('#msg').html(message)
    }
    
    function playSound(btn) {
	var sound = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound' + btn + '.mp3');
	sound.play();
	sound.currentTime=0;
    }

    function reset() {
	gameOn = false;
	level = 0;
	playerClicks = [];
	$('#msg').html('');
	$('#gm_tgl').attr('class', 'start').html('Start');
    }

});

            
          
!
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