Simon Game

Size
3,197 Kb
Views
32,384

How do I make an simon game?

What is a simon game? How do you make a simon game? This script and codes were developed by Joo Vctor De Oliveira Santos on 27 August 2022, Saturday.

Simon Game Previews

Simon Game - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Simon Game</title> <link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="myModal" class="modal fade" tabindex="-1" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4 class="modal-title">You Won!</h4> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<div class="container"> <div class="row"> <div class="col-xs-4"> <button id="btn1" class="btn btn-success game-btn" /> </div> <div class="col-xs-4"> <span style="color:red;" class="hide" id="wrongAlert">Wrong!</span> </div> <div class="col-xs-4"> <button id="btn2" class="btn btn-danger game-btn" /> </div> </div> <div class="row"> <div class="col-xs-4 col-xs-offset-4"> <div class="btn-group" role="group" aria-label="..."> <button id="btnStart" class="btn btn-default">Start</button> <button id="btnOn" class="btn btn-default">On</button> <button id="scoreCounter" class="btn btn-default disabled ">--</button> <button id="btnStrictMode" class="btn btn-default">Strict</button> </div> </div> </div> <div class="row"> <div class="col-xs-4"> <button id="btn3" class="btn btn-warning game-btn" /> </div> <div class="col-xs-4 col-xs-offset-4"> <button id="btn4" class="btn btn-primary game-btn" /> </div> </div>
</div>
<audio id='sounds1' src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"></audio>
<audio id='sounds2' src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"></audio>
<audio id='sounds3' src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"></audio>
<audio id='sounds4' src="https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"></audio> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Simon Game - Script Codes CSS Codes

.game-btn { width: 50px; height: 50px;
}
div { text-align: center;
}
.container { margin-top: 200px;
}

Simon Game - Script Codes JS Codes

var colorCodes = ["green", "red", "orange", "blue"];
var game = { isOn: false, inGame: false, strictMode: false, currentScore: 0, sequence: [], currentSq: 0, state: "waiting", togglePower: function() { this.isOn = !(this.isOn); console.log("Toggle " + this.isOn); if (!(this.isOn)) { //Reset to default values this.reset(); } else { $("#scoreCounter").text("00"); } }, updateCounter: function() { $("#scoreCounter").text(this.currentScore); }, addRandom: function() { var r = Math.floor(Math.random() * 4) + 1; this.sequence[this.sequence.length] = r; }, reset: function() { console.log("Reseting..."); this.currentScore = 0; this.inGame = false; this.sequence = []; this.currentSq = 0; $("#scoreCounter").text("--"); this.state = "waiting"; }, start: function() { if (!this.isOn) return; console.log("Starting..."); this.inGame = true; this.updateCounter(); this.addRandom(); this.playSequence(); }, toggleStrict: function() { this.strictMode = !(this.strictMode); }, complete: function() { if(this.sequence.length == 20){ $('#myModal').modal('show'); this.reset(); return; } console.log("You complete one turn"); this.currentSq = 0; this.currentScore = this.sequence.length; this.addRandom(); this.updateCounter(); this.playSequence(); }, buttonPress: function(index = 0, player = false) { if (!this.isOn || (player && this.state != "waiting") || index == 0) return; console.log(index); var sound = $("#sounds" + index); sound[0].play(); $("#btn" + index).addClass("active"); sound.on("ended", this.releaseButton); if (player) { if (index == this.sequence[this.currentSq]) { this.currentSq++; if (this.currentSq == this.sequence.length) { this.complete(); } } else if (this.strictMode) { $("#wrongAlert").removeClass("hide"); setTimeout(function(){$("#wrongAlert").addClass("hide");}, 1000); this.reset(); } else { $("#wrongAlert").removeClass("hide"); setTimeout(function(){$("#wrongAlert").addClass("hide");}, 1000); this.currentSq = 0; this.playSequence(); } } }, _playSequence: function(index) { console.log("index : " + index); this.buttonPress(this.sequence[index]); if (index == this.sequence.length - 1) this.state = "waiting"; return index + 1; }, playSequence: function() { if (this.state == "playingSequence") return; var r = 0; var f = this; this.state = "playingSequence"; do { (function(r) { setTimeout(function() { f._playSequence(r); }, 1000 + (750 * r)); })(r); r++; } while (r < this.sequence.length); }, releaseButton: function(e) { $(this).off("ended"); for (var i = 1; i <= 4; ++i) { if ($("#btn" + i).hasClass("active")) $("#btn" + i).removeClass("active"); } }
};
$("#btnOn").on("click", function() { game.togglePower(); $(this).toggleClass('active');
});
$("#btnStrictMode").on("click", function() { game.toggleStrict(); $(this).toggleClass('active');
});
$("#btnStart").on("click", function() { if (game.inGame) game.reset(); else game.start();
});
$("#btn1").on("click", function() { game.buttonPress(1, true);
});
$("#btn2").on("click", function() { game.buttonPress(2, true);
});
$("#btn3").on("click", function() { game.buttonPress(3, true);
});
$("#btn4").on("click", function() { game.buttonPress(4, true);
});
Simon Game - Script Codes
Simon Game - Script Codes
Home Page Home
Developer Joo Vctor De Oliveira Santos
Username jvhti
Uploaded August 27, 2022
Rating 3
Size 3,197 Kb
Views 32,384
Do you need developer help for Simon Game?

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!

Joo Vctor De Oliveira Santos (jvhti) Script Codes
Create amazing video scripts 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!