diff --git a/README.md b/README.md index 5c884f5..cdbdb51 100644 --- a/README.md +++ b/README.md @@ -7,4 +7,4 @@ Simon Game is my first project built with JavaScript after 2 days (~21.58 hours) ### Indonesia Simon Game adalah proyek pertama saya yang dibuat menggunakan JavaScript setelah belajar selama 2 hari (~21,58 jam). Game ini menguji memori pemain dengan pola warna dan suara yang semakin kompleks di setiap level. Proyek ini menggunakan HTML, CSS, jQuery, dan JavaScript. Meskipun mungkin hasilnya belum sempurna, saya tetap bangga dengan apa yang telah saya capai. Proyek ini adalah langkah awal saya dalam memahami konsep dasar DOM, animasi, dan event handling dalam JavaScript. - + diff --git a/sounds/blue.mp3 b/assets/sounds/blue.mp3 similarity index 100% rename from sounds/blue.mp3 rename to assets/sounds/blue.mp3 diff --git a/assets/sounds/gray.mp3 b/assets/sounds/gray.mp3 new file mode 100644 index 0000000..ae68cba Binary files /dev/null and b/assets/sounds/gray.mp3 differ diff --git a/sounds/green.mp3 b/assets/sounds/green.mp3 similarity index 100% rename from sounds/green.mp3 rename to assets/sounds/green.mp3 diff --git a/sounds/yellow.mp3 b/assets/sounds/orange.mp3 similarity index 100% rename from sounds/yellow.mp3 rename to assets/sounds/orange.mp3 diff --git a/sounds/red.mp3 b/assets/sounds/pink.mp3 similarity index 100% rename from sounds/red.mp3 rename to assets/sounds/pink.mp3 diff --git a/assets/sounds/purple.mp3 b/assets/sounds/purple.mp3 new file mode 100644 index 0000000..896b9f9 Binary files /dev/null and b/assets/sounds/purple.mp3 differ diff --git a/assets/sounds/red.mp3 b/assets/sounds/red.mp3 new file mode 100644 index 0000000..e7738ae Binary files /dev/null and b/assets/sounds/red.mp3 differ diff --git a/sounds/wrong.mp3 b/assets/sounds/wrong.mp3 similarity index 100% rename from sounds/wrong.mp3 rename to assets/sounds/wrong.mp3 diff --git a/assets/sounds/yellow.mp3 b/assets/sounds/yellow.mp3 new file mode 100644 index 0000000..b360c08 Binary files /dev/null and b/assets/sounds/yellow.mp3 differ diff --git a/assets/svg/sound-mute.svg b/assets/svg/sound-mute.svg new file mode 100644 index 0000000..a4d0c9b --- /dev/null +++ b/assets/svg/sound-mute.svg @@ -0,0 +1,44 @@ + + + + \ No newline at end of file diff --git a/assets/svg/sound-on.svg b/assets/svg/sound-on.svg new file mode 100644 index 0000000..ad9af8d --- /dev/null +++ b/assets/svg/sound-on.svg @@ -0,0 +1,32 @@ + + + + \ No newline at end of file diff --git a/game.js b/game.js index 9c84d67..791027a 100644 --- a/game.js +++ b/game.js @@ -1,33 +1,45 @@ -var userClickedPattern = []; -var gamePattern = []; -var randomNumber; -var buttonColours = ["red", "blue", "green", "yellow"]; -var levelGame = 0; -var gameStart = false; -var clickBtn = false; - - -function animationStart(counter) { - clickBtn = true; - +const buttonColours = ["red", "blue", "green", "yellow", "orange", "purple", "pink", "gray"]; +let userClickedPattern = []; +let gamePattern = []; +let randomNumber = 0; +let levelGame = 0; +let gameStart = false; +let clickBtn = false; +let soundStatus = false; + +function playsound(nameSound) { + if (!soundStatus) { + const sound = new Audio(`assets/sounds/${nameSound}.mp3`); + sound.play(); + } +} + +function animationStart(counter) { + clickBtn = false; + setTimeout(function () { - $(`#${gamePattern[counter]}`).fadeOut(100).fadeIn(100); - playSound(gamePattern[counter]); + $(`#${gamePattern[counter]}`).addClass("active"); + playsound(gamePattern[counter]); }, counter * 500); setTimeout(function () { - if (counter === gamePattern.length - 1) { - clickBtn = false; + $(`#${gamePattern[counter]}`).removeClass("active"); + }, counter * 500 + 300); + + setTimeout(function () { + if (counter === gamePattern.length - 1) { + clickBtn = true; } - }, counter * 800); + }, counter * 700); } function nextSequence() { gameStart = true; + randomNumber = Math.floor(Math.random() * 8); levelGame++; - randomNumber = Math.floor(Math.random() * 4); - var randomChosenColour = buttonColours[randomNumber]; - gamePattern.push(randomChosenColour); + + const randomColor = buttonColours[randomNumber]; + gamePattern.push(randomColor); $("h1").text(`Level ${levelGame}`); for (let i = 0; i < gamePattern.length; i++) { @@ -35,33 +47,6 @@ function nextSequence() { } } - -$(document).keydown(function () { - if (gameStart === true) { - return; - } - nextSequence(); -}); - -$(".btn").click(function () { - if (gameStart === false || clickBtn === true) { - return; - } - var userChosenColour = $(this).attr("id"); - $(`#${userChosenColour}`).fadeOut(100).fadeIn(100); - // $(`#${userChosenColour}`).addClass("pressed"); - userClickedPattern.push(userChosenColour); - playSound(userChosenColour); - - setTimeout(function () { - $(`#${userChosenColour}`).removeClass("pressed"); - }, 100); - - var currentLevel = userClickedPattern.length - 1; - - checkAnswer(currentLevel); -}); - function checkAnswer(currentLevel) { if (gamePattern[currentLevel] === userClickedPattern[currentLevel]) { if (gamePattern.length === userClickedPattern.length) { @@ -74,25 +59,54 @@ function checkAnswer(currentLevel) { function nextLevel() { setTimeout(function () { - nextSequence(); userClickedPattern = []; + nextSequence(); }, 1000); } function startOver() { - gameStart = false; + setTimeout(function () { + gameStart = false; + }, 1100); gamePattern = []; userClickedPattern = []; levelGame = 0; - playSound("wrong"); - $("h1").text("Game Over, Press Any Key to Restart"); + playsound("wrong"); + $("#level_title").text("Game Over, Click Anywhere To Restart"); $("body").addClass("game-over"); setTimeout(function () { $("body").removeClass("game-over"); }, 200); } -function playSound(currentKey) { - var audio = new Audio(`sounds/${currentKey}.mp3`); - audio.play(); -} +$("#sound_button").click(function () { + if (!soundStatus) { + soundStatus = true; + } else { + soundStatus = false; + } + $(this).toggleClass("mute"); +}); + +$(document).click(function () { + if (gameStart !== true) { + nextSequence(); + } +}); + +$(".btn").click(function () { + if (gameStart === false || clickBtn === false) { + return; + } + const userChosenColour = $(this).attr("id"); + $(`#${userChosenColour}`).addClass("active"); + userClickedPattern.push(userChosenColour); + + const currentLevel = userClickedPattern.length - 1; + checkAnswer(currentLevel); + playsound(userChosenColour); + + setTimeout(function () { + $(`#${userChosenColour}`).removeClass("active"); + }, 100); +}); diff --git a/image.png b/image.png index 893c8cb..8a0a6e5 100644 Binary files a/image.png and b/image.png differ diff --git a/index.html b/index.html index 44a19a5..799bb26 100644 --- a/index.html +++ b/index.html @@ -1,42 +1,73 @@ -
- - -