-
Notifications
You must be signed in to change notification settings - Fork 0
/
simon.js
95 lines (77 loc) · 1.96 KB
/
simon.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
83
84
85
86
87
88
89
90
91
92
93
94
const buttonArr = ["red", "blue", "green", "yellow"];
let GamePattren = [];
let UserClickPattren = [];
let level = 0;
let GameStatus = false;
const gameCall = () => {
if (!GameStatus) {
$("#level-title").text("level " + level);
console.log("Game Start called");
nextSequence();
GameStatus = true;
}
};
const newGame = () => {
if (GameStatus) {
startOver();
}
GameStatus = false;
};
$(document).on("click", gameCall);
$(document).keydown(gameCall);
$("#new-game").click(newGame);
$(".btn").click(function () {
if (GameStatus) {
const UserChosenColor = $(this).attr("id");
UserClickPattren.push(UserChosenColor);
PlaySound(UserChosenColor);
animatePress(UserChosenColor);
checkAnswer(UserClickPattren.length - 1);
}
});
function checkAnswer(currlevel) {
if (GamePattren[currlevel] == UserClickPattren[currlevel]) {
if (UserClickPattren.length === GamePattren.length) {
setTimeout(function () {
nextSequence();
}, 1000);
}
} else {
$("body").addClass("game-over");
setTimeout(function () {
$("body").removeClass("game-over");
}, 200);
$("#level-title").text("Game Over, Press New Game to Restart");
// var worng = new Audio("sounds/worng.mp3");
// worng.play();
startOver();
}
}
function startOver() {
level = 0;
GamePattren = [];
}
function nextSequence() {
console.log("Next Sequence");
UserClickPattren = [];
level++;
$("#level-title").text("level " + level);
const randomNum = Math.floor(Math.random() * 4);
const randomColor = buttonArr[randomNum];
GamePattren.push(randomColor);
$("#" + randomColor)
.fadeIn(100)
.fadeOut(100)
.fadeIn(100);
PlaySound(randomColor);
}
function PlaySound(sound) {
var audio = new Audio("sounds/" + sound + ".mp3");
audio.play();
}
function animatePress(currColor) {
$("#" + currColor).addClass("pressed");
setTimeout(function () {
$("#" + currColor).removeClass("pressed");
}, 100);
}