Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.9 kB
14
Indexable
Never
var buttonColors = ["red", "blue", "green", "yellow"];

var gamePattern = [];
var userClickedPattern =[];

var started = false;
var level = 0;


$(document).keypress(function(){
   if (!started){
    $("#level-title").text("Level " + level);
    nextSequence();
    started = true;
    }
    
});
    




$(".btn").click(function() {

    var userChosenColor = $(this).attr("id");

    userClickedPattern.push(userChosenColor);

    

    playSound(userChosenColor);

    animatePress(userChosenColor);
    checkAnswer(userClickedPattern.length-1);
});


 




function checkAnswer(currentLevel) {

    if (gamePattern[currentLevel] === userClickedPattern[currentLevel]) {

      console.log("success");

      if (userClickedPattern.length === gamePattern.length){
        setTimeout(function () {
          nextSequence();
        }, 1500);
      }

    } else {

      console.log("wrong");

     
      playSound("wrong");

      $("body").addClass("game-over");
      setTimeout(function () {
        $("body").removeClass("game-over");
      }, 200);

      
      $("#level-title").text("Game Over, Press Any Key to Restart");
    }

}


function nextSequence(){
    userClickedPattern = [];
    level++;
    $("#level-title").text("level" + level);

    var randomNumber = Math.floor(Math.random()*4);
    var randomChosenColor = buttonColors[randomNumber];
    gamePattern.push(randomChosenColor);

    $("#" + randomChosenColor).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
    
    playSound(randomChosenColor);
    
    
}

function playSound(name){

    var audio =  new Audio("sounds/" + name + ".mp3");

    audio.play();

}


function animatePress(currentColor){

    $("#" + currentColor).addClass("pressed");
    setTimeout(function(){
        $("#" + currentColor).removeClass("pressed");
    }, 100);

}