├── .DS_Store ├── game.js ├── index.html ├── sounds ├── blue.mp3 ├── green.mp3 ├── red.mp3 ├── wrong.mp3 └── yellow.mp3 └── styles.css /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ayusrv/SimonGame/b93b22132150e59ed700f9c6a79bf7436ba79635/.DS_Store -------------------------------------------------------------------------------- /game.js: -------------------------------------------------------------------------------- 1 | var buttonColours = ["red", "blue", "green", "yellow"]; 2 | 3 | var gamePattern = []; 4 | var userClickedPattern = []; 5 | var started = false; 6 | var answer = true; 7 | var level = 0; 8 | $(document).keypress(function () { 9 | if (!started) { 10 | $("#level-title").text("Level " + level); 11 | nextSequence(); 12 | started = true; 13 | } 14 | }); 15 | 16 | $(".btn").click(function () { 17 | var userChosenColour = $(this).attr("id"); 18 | userClickedPattern.push(userChosenColour); 19 | if (answer) { 20 | playSound(userChosenColour); 21 | } 22 | animatePress(userChosenColour); 23 | checkAnswer(userClickedPattern.length - 1); 24 | }); 25 | 26 | function nextSequence() { 27 | userClickedPattern = []; 28 | level++; 29 | $("#level-title").text("Level " + level); 30 | 31 | var randomNumber = Math.floor(Math.random() * 4); 32 | var randomChosenColour = buttonColours[randomNumber]; 33 | gamePattern.push(randomChosenColour); 34 | 35 | $("#" + randomChosenColour) 36 | .fadeIn(100) 37 | .fadeOut(100) 38 | .fadeIn(100); 39 | playSound(randomChosenColour); 40 | } 41 | 42 | function playSound(name) { 43 | var audio = new Audio("sounds/" + name + ".mp3"); 44 | audio.play(); 45 | } 46 | 47 | function animatePress(currentColor) { 48 | $("#" + currentColor).addClass("pressed"); 49 | setTimeout(function () { 50 | $("#" + currentColor).removeClass("pressed"); 51 | }, 150); 52 | } 53 | 54 | function checkAnswer(currentLevel) { 55 | if (gamePattern[currentLevel] == userClickedPattern[currentLevel]) { 56 | console.log("Right"); 57 | if (userClickedPattern.length === gamePattern.length) { 58 | setTimeout(function () { 59 | nextSequence(); 60 | }, 1000); 61 | } 62 | } else { 63 | answer = false; 64 | var audio = new Audio("sounds/wrong.mp3"); 65 | audio.play(); 66 | $("body").addClass("game-over"); 67 | setTimeout(function () { 68 | $("body").removeClass("game-over"); 69 | }, 200); 70 | $("#level-title").text("Game Over, Press Any Key to Restart"); 71 | startOver(); 72 | } 73 | } 74 | 75 | function startOver() { 76 | level = 0; 77 | gamePattern = []; 78 | started = false; 79 | } 80 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 |