Skip to content

Instantly share code, notes, and snippets.

@Deityhub
Created February 13, 2018 17:21
Show Gist options
  • Save Deityhub/a7ddbc848f5065f4e9e4317bef18c8c1 to your computer and use it in GitHub Desktop.
Save Deityhub/a7ddbc848f5065f4e9e4317bef18c8c1 to your computer and use it in GitHub Desktop.
simon game
<div class="wrap">
<div class="wrap-in">
<div class="rw">
<div class="t-l inline push green unclickable" id='green' onclick="userPlay('green')"></div>
<div class="t-r inline push red but unclickable" id='red' onclick="userPlay('red')"></div>
</div>
<div class="rw">
<div class="b-l inline push yellow unclickable" id='yellow' onclick="userPlay('yellow')"></div>
<div class="b-r inline push blue but unclickable" id='blue' onclick="userPlay('blue')"></div>
</div>
</div>
<div class="center">
<h1 class="brand">Simon<span class="small">®</span></h1>
<div class="rw">
<div class="display inline">
<h1 class="count led-off ">--</h1>
<h3 class="label">LEVEL</h3>
</div>
<div class="btn-box inline">
<div class="round-btn full-red but clickable" id="start"></div>
<h3 class="label">START</h3>
</div>
<div class="btn-box inline">
<div class="round-btn but clickable" id="mode"></div>
<h3 class="label">STRICT</h3>
<div class="led" id="mode-led"></div>
</div>
</div>
<div class="rw bot">
<h3 class="label inline">OFF</h3>
<div class="sw-slot inline">
<div class="switch" id="pwr-sw"></div>
</div>
<h3 class="label inline">ON</h3>
</div>
</div>
</div>
var game = {
count: 0,
comEntry: ['blue', 'red', 'yellow', 'green'],
comPlay: [],
userPlay: [],
level: 1,
strict: false,
sound:{
blue: new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3'),
red: new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3'),
yellow: new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3'),
green: new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3')
}
}
var gameInterval, gameCount = 1;
var switchOn = false;
//event handlers for some buttons
$('div.sw-slot').click(function(){
if(!switchOn){
switchOn = true;
$('div.switch').addClass('sw-on');
$('h1.count').addClass('led-on');
}else{
switchOn = false;
gameCount = 1;
game.level = 1;
clearGame();
clearInterval(gameInterval);
$('.push').removeClass('clickable');
$('.push').addClass('unclickable');
$('div.switch').removeClass('sw-on');
$('h1.count').removeClass('led-on');
$('h1.count').text('--');
}
})
$('div#mode').click(function (){
if(switchOn){
if(!game.strict){
$('div#mode-led').addClass('led-on');
game.strict = true;
}else{
$('div#mode-led').removeClass('led-on');
game.strict = false;
}
}
})
$('div#start').click(function(){
if(switchOn){
gameCount = 1;
game.level = 1;
clearGame();
clearInterval(gameInterval);
$('.push').removeClass('clickable');
$('.push').addClass('unclickable');
startGame();
}
})
//The main functionality of the simon game
function newGame(){
clearGame();
}
function clearGame(){
game.count = 0;
game.comPlay = [];
game.userPlay = [];
}
function startGame(){
checkLevel(game.level)
}
function checkLevel(level){
$('h1.count').text(level);
switch(level){
case 1:
gameCount = 2;
addComMove();
break;
case 2:
gameCount = 4;
addComMove();
break;
case 3:
gameCount = 6;
addComMove();
break;
case 4:
gameCount = 8;
addComMove();
break;
case 5:
gameCount = 10;
addComMove();
break;
case 6:
gameCount = 12;
addComMove();
break;
case 7:
gameCount = 14;
addComMove();
break;
case 8:
gameCount = 16;
addComMove();
break;
case 9:
gameCount = 18;
addComMove();
break;
case 10:
gameCount = 20;
addComMove();
break;
default:
clearInterval(gameInterval);
gameCount = 1;
game.level = 1;
$('h1.count').text('WIN');
newGame();
}
}
function addComMove(){
gameInterval = setInterval(function(){
if(game.count != gameCount){
game.count++;
var randColor = game.comEntry[Math.floor(Math.random() * 4)]
game.comPlay.push(randColor);
displayInput(randColor);
sound(randColor);
}else{
clearInterval(gameInterval);
game.level += 1;
$('.push').removeClass('unclickable');
$('.push').addClass('clickable');
}
}, 700)
}
function userPlay(color){
displayInput(color);
sound(color);
game.userPlay.push(color);
var length = game.userPlay.length;
if(game.userPlay[length - 1] == game.comPlay[length - 1]){
if(length == game.comPlay.length){
$('.push').removeClass('clickable');
$('.push').addClass('unclickable');
setTimeout(checkLevel, 1000, game.level);
clearGame();
}
}else{
if(game.strict){
clearGame();
clearInterval(gameInterval)
game.level = 1;
gameCount = 1;
$('h1.count').addClass('led-on');
$('h1.count').text("LOSS")
setTimeout(checkLevel, 1500, game.level);
}else{
game.userPlay = [];
$('h1.count').text('!!');
setTimeout(function(){
$('h1.count').text(game.level - 1);
}, 1000)
}
}
}
function displayInput(color){
var index = '#' + color;
$(index).addClass('light');
setTimeout(function(){
$(index).removeClass('light');
}, 300)
}
function sound(color){
switch(color){
case 'blue':
game.sound.blue.play();
break;
case 'red':
game.sound.red.play();
break;
case 'yellow':
game.sound.yellow.play();
break;
case 'green':
game.sound.green.play();
break;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

simon game

The simon game is basically a memory game. The game has four colored buttons, each producing a particular tone when it is pressed or activated by the inbuilt AI. A round in the game consists of lighting up one or more buttons in a random order, after which the player must reproduce that order by pressing the buttons. As the game progresses, the number of buttons to be pressed increases.

A Pen by Ojini Chizoba Jude on CodePen.

License.

@import url('https://fonts.googleapis.com/css?family=Alfa+Slab+One|VT323|Oswald');
/* Basic Reset */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
/* Disable mouse selection */
-webkit-user-select: none; /* Chrome/Safari */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* IE10+ */
/* Rules below not implemented in browsers yet */
-o-user-select: none;
user-select: none;
}
html{
background-image: url('http://cdn.backgroundhost.com/backgrounds/subtlepatterns/retina_wood.png');
}
.inline{
display: inline-block;
}
.push{
position : relative;
width:200px;
height:200px;
background-color : grey;
border: 12px solid #333;
}
.rw{
margin-bottom:-4px;
}
.b-r{
border-bottom-right-radius : 100%;
}
.b-l{
border-bottom-left-radius : 100%;
}
.t-l{
border-top-left-radius : 100%;
}
.t-r{
border-top-right-radius : 100%;
}
.wrap{
width: 472px;
height:472px;
border-radius:100%;
position: relative;
text-align: center;
margin:auto;
background-color:#333;
top:20vh;
box-shadow: 0px 0px 12px #222;
}
.wrap-in{
position: relative;
top:12px;
}
.center{
width:220px;
height:220px;
position : absolute;
border-radius:100%;
top:50%;
left:50%;
margin: -122px 0 0 -122px;
background-color:#ECE7EE;
border: 12px solid #333;
}
.brand{
font-family: 'Alfa Slab One', cursive;
color : #222;
font-size: 3.2em;
margin-top : 35px;
}
.brand .small{
font-size : 0.3em;
position : relative;
top : -20px;
}
.count{
font-family: 'VT323', monospace;
color : #DC0D29;
padding : 0px;
font-size : 2em;
width : 60px;
background-color: #32050C;
position:relative;
border : 4px solid #222;
border-radius : 10px;
margin:auto;
}
.round-btn{
width : 20px;
height : 20px;
position: relative;
background-color : yellow;
border-radius : 100%;
border : 4px solid #444;
box-shadow : 0px 2px 3px #222;
margin:auto;
top : -5px;
}
.round-btn:active{
box-shadow : 0 1px 1px #292929;
top : -4px;
}
.clickable{
pointer-events : auto;
cursor:pointer;
}
.unclickable{
pointer-events:none;
}
.led{
width:6px;
height:6px;
background-color: #32050C;
border-radius : 100%;
position: absolute;
left:0;
right: 0;
margin : auto;
border: 2px solid #222;
top: -18px;
}
.led-on{
background-color:#DC0D29;
}
.led-off{
color:#430710;
}
.label{
color : #222;
font-family : 'Oswald',Arial,sans;
font-size:0.7em;
margin-top:5px;
text-align:center;
}
.display{
width : 60px;
position:relative;
text-align:center;
}
.btn-box{
width : 50px;
position:relative;
}
.sw-slot{
height:20px;
width:40px;
background-color : #222;
position:relative;
top:5px;
border-radius:2px;
cursor: pointer;
}
.switch{
height:16px;
width: 16px;
border-radius:4px;
background-color : #3193DE;
position:relative;
border : 2px solid #333;
}
.sw-on{
left:20px;
}
.rw.bot{
margin-top: 10px;
}
.full-red{
background-color : #FC0102;
}
.red{
background-color : #9f0f17;
}
.green{
background-color : #00a74a;
}
.yellow{
background-color : #cca707;
}
.blue{
background-color : #094a8f;
}
/*show this colors when the buttons are activated either by click or computer play*/
.yellow.light{
background-color : #fed93f;
}
.blue.light{
background-color : #1c8cff;
}
.red.light{
background-color : #ff4c4c;
}
.green.light{
background-color : #13ff7c;
}
/* Scaling for Mobile */
@media screen and (max-width: 500px){
.wrap {
top: 100px;
-moz-transform: scale(0.6);
-ms-transform: scale(0.6);
-o-transform: scale(0.6);
-webkit-transform: scale(0.6);
transform: scale(0.6);
-o-transform-origin: 0 0;
-ms-transform-origin: 0 0;
-webkit-transform-origin: 0 0;
-moz-transform-origin: 0 0;
transform-origin: 0 0;
left: 50%;
margin-left: -142px;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment