-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
82 lines (75 loc) · 2.32 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var buttonColors = ["red", "blue", "green", "yellow"];
var gamePattern = [];
var userClickedPattern = [];
var level = 0;
// This button for starting the game.
$(".btnstart").click(function() {
gamePattern = [];
userClickedPattern = [];
level = 0;
$(".btnstart").fadeOut();
setTimeout(function() {
nextSequence();
}, 2000);
$("h1").text("Level " + level);
});
// This function is created to start new sequence.
function nextSequence() {
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);
// console.log(gamePattern);
level++;
$("h1").text("Level " + level);
}
// This function is created to play sounds.
function playSound(buttonid) {
var buttonAudio = new Audio("sounds/" + buttonid + ".mp3");
buttonAudio.play();
}
// This function is created to animate the clicked one.
function animatePress(color) {
$("#" + color).addClass("pressed");
setTimeout(function() {
$("#" + color).removeClass("pressed");
}, 100);
}
function userChosenColor(buttonid) {
userClickedPattern.push(buttonid);
// console.log(gamePattern);
// console.log(userClickedPattern);
var lastUserAnswer = userClickedPattern.length - 1
// console.log(lastUserAnswer);
checkAnswer(lastUserAnswer);
}
$(".btn").on("click", function(event) {
var buttonClicked = $(this).attr("id")
animatePress(buttonClicked);
playSound(buttonClicked);
userChosenColor(buttonClicked);
});
function checkAnswer(currentLevel) {
if (userClickedPattern[currentLevel] === gamePattern[currentLevel]) {
console.log("Succeed");
var status = "Succeed";
if (userClickedPattern.length === gamePattern.length) {
setTimeout(function() {
userClickedPattern = [];
nextSequence();
}, 1000);
}
} else {
console.log("Wrong");
var status = "Wrong";
var wrongAudio = new Audio("sounds/wrong.mp3");
wrongAudio.play();
$("body").addClass("game-over");
setTimeout(function() {
$("body").removeClass("game-over");
}, 200);
$("h1").text("Game Over, Press a Start Button to Restart")
$(".btnstart").fadeIn();
}
}