Simon Says

Developer
Size
3,674 Kb
Views
6,072

How do I make an simon says?

Simon Says game build for freeCodeCamp using Javascript.. What is a simon says? How do you make a simon says? This script and codes were developed by Zac Clemans on 14 January 2023, Saturday.

Simon Says Previews

Simon Says - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Simon Says</title> <meta name="viewport" content="width=device-width, initial-scale=1">
<link href='https://fonts.googleapis.com/css?family=Press+Start+2P' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div class="container"> <div class="row"> <div id="btn-1" class="btn" data-value="1"></div> <div id="btn-2" class="btn" data-value="2"></div> </div> <div class="row"> <div id="btn-3" class="btn" data-value="3"></div> <div id="btn-4" class="btn" data-value="4"></div> </div> <div id="center"> <label id="mode-label" class="op-label" for="mode">Strict</label> <label id="start-label" class="op-label" for="start">Start</label> <label id="reset-label" class="op-label" for="reset">Reset</label> <input id="mode-btn" class="op-btn" type="button" name="mode"> <input id="start-btn" class="op-btn" type="button" name="start"> <input id="reset-btn" class="op-btn" type="button" name="reset"> <div id="count-display"></div> </div>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Simon Says - Script Codes CSS Codes

body { background: rgb(197, 204, 212);
}
div.container { position: relative; width: 460px; margin-top: 100px; margin-left: auto; margin-right: auto;
}
div.row { display: flex
}
div.btn { font-weight: bold; font-size: 40px; width: 200px; height: 200px; border: 10px solid rgb(0,0,0);
}
div#btn-1 { border-top-left-radius: 100%; border-top-width: 20px; border-left-width: 20px; background: linear-gradient(to bottom right, rgb(0, 255, 0), rgb(0, 100, 0));
}
div#btn-2 { border-top-right-radius: 100%; border-top-width: 20px; border-right-width: 20px; background: linear-gradient(to bottom left, rgb(255, 1, 1), rgb(100, 1, 1));
}
div#btn-3 { border-bottom-left-radius: 100%; border-bottom-width: 20px; border-left-width: 20px; /*background: rgb(0, 100, 255);*/ background: linear-gradient(to bottom left, rgb(0, 50, 100), rgb(0, 100, 255));
}
div#btn-4 { border-bottom-right-radius: 100%; border-bottom-width: 20px; border-right-width: 20px; /*background: rgb(230, 210, 1);*/ background: linear-gradient(to bottom right, rgb(170, 150, 1), rgb(230, 210, 1));
}
div#center { position: absolute; top: 25%; left: 25%; width: 230px; height: 230px; border-radius: 50%; background: rgb(0,0,0);
}
input.op-btn { position: absolute; top: 70%; width: 15px; height: 15px; border: none; border-radius: 50%;
}
input#mode-btn { left: 30%; background: rgb(210, 210, 1);
} input#mode-btn:hover { background: rgb(180, 180, 1); }
input#start-btn { left: 64%; background: rgb(1, 175, 1);
} input#start-btn:hover { background: rgb(1, 150, 1); } input#start-btn:active { width: 13px; background: rgb(1, 150, 1); border: 2px solid rgb(1, 100, 1); }
input#reset-btn { left: 47%; background: rgb(220, 1, 1);
} input#reset-btn:hover { background: rgb(180, 1, 1); } input#reset-btn:active { width: 13px; background: rgb(180, 1, 1); border: 2px solid rgb(100, 1, 1); }
div#count-display { position: absolute; top: 35%; left: 34%; color: green; text-align: center; line-height: 50px; font-family: "Press Start 2P"; font-weight: bold; font-size: 25px; width: 75px; height: 50px; background: rgb(50, 50, 50); border-radius: 5px;
}
label.op-label { position: absolute; top: 77%; color: white; font-weight: bold; font-size: 10px;
}
label#mode-label { left: 27%;
}
label#start-label { left: 62%;
}
label#reset-label { left: 44.5%;
}
.blink { filter: brightness(400%);
}

Simon Says - Script Codes JS Codes

function Game(mode) { this.mode = mode; this.greenSound = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"); this.redSound = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"); this.blueSound = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"); this.yellowSound = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");
}
Game.prototype.init = function() { this.gameSequence = []; this.sequenceLength = 0; this.errors = 0; this.playerTurn = false; this.playerIndex = 0; var self = this; $(".btn").on('click', function() { if (self.playerTurn) { var btnId = $(this).data('value'); self.playSound(btnId); self.checkPlayerInput($(this).data('value')); } }); $(".btn").on('mousedown', function(){ if (self.playerTurn) { $(this).addClass('blink'); } }); $(".btn").on('mouseup', function(){ if (self.playerTurn) { $(this).removeClass('blink'); } });
}
Game.prototype.addNewSequence = function() { var next = Math.floor((Math.random() * 4) + 1); this.gameSequence.push(next); console.log(this.gameSequence); this.sequenceLength++; this.displayCount();
}
Game.prototype.displaySequence = function() { var self = this; var index = 0; blinkSequence(this.gameSequence[index]); function blinkSequence(number) { setTimeout(function() { self.playSound(number); $("#btn-" + number).toggleClass('blink'); setTimeout(function(){ $("#btn-" + number).toggleClass('blink'); if (index < self.sequenceLength){ index++; blinkSequence(self.gameSequence[index]); } else { console.log("player turn"); self.playerTurn = true; } }, 500); }, 50); }
}
Game.prototype.playSound = function(button) { switch(button) { case 1: this.greenSound.play(); break; case 2: this.redSound.play(); break; case 3: this.blueSound.play(); break; case 4: this.yellowSound.play(); break; }
}
Game.prototype.displayCount = function() { if (this.sequenceLength < 10) { $("#count-display").text("0" + this.sequenceLength); } else { $("#count-display").text(this.sequenceLength); }
}
Game.prototype.checkPlayerInput = function(input) { var self = this; if(input == this.gameSequence[this.playerIndex]) { this.playerIndex++; if (this.playerIndex >= this.sequenceLength) { if (this.sequenceLength < 20) { this.playerTurn = false; this.playerIndex = 0; this.errors = 0; this.addNewSequence(); setTimeout(function(){ self.displaySequence(); }, 1000); } else { this.wonGame(); } } } else { if (this.mode == "strict") { this.gameOver(); $("#count-display").text("!"); setTimeout(function(){ self.startGame(); }, 1000); } else { if (this.errors < 1) { this.errors++; this.playerIndex = 0; $("#count-display").text("!"); setTimeout(function(){ self.displaySequence(); }, 1000); } else { this.gameOver(); $("#count-display").text("!"); setTimeout(function(){ self.startGame(); }, 1000); } } }
}
Game.prototype.gameOver = function() { $(".btn") .off('click') .off('mousedown') .off('mouseup');
}
Game.prototype.wonGame = function() { var self = this; $("#count-display").text("Win!"); setTimeout(function(){ $("#reset-btn").click(); }, 3000);
}
Game.prototype.startGame = function() { this.init(); this.addNewSequence(); this.displaySequence();
}
var mode = "casual";
$('#mode-btn').on('click', function(){ if (mode == "casual") { mode = "strict"; $(this).css({'width': '13px', 'background': 'rgb(180, 180, 1)', 'border': '2px solid rgb(100, 100, 1)'}); } else { mode = "casual"; $(this).css({'width': '15px', 'background': 'rgb(210, 210, 1)', 'border': 'none'}); }
});
$("#start-btn").on('click', function(){ var game = new Game(mode); game.startGame(); $(this).off('click'); $("#reset-btn").on('click', function(){ game.gameOver(); game.startGame(); });
});
Simon Says - Script Codes
Simon Says - Script Codes
Home Page Home
Developer Zac Clemans
Username thalpha
Uploaded January 14, 2023
Rating 3
Size 3,674 Kb
Views 6,072
Do you need developer help for Simon Says?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Zac Clemans (thalpha) Script Codes
Create amazing love letters with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!