-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
111 lines (99 loc) · 3.14 KB
/
script.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
let highScore = 0;
let currentScore = 0;
let isSequencePlayed = false;
const simonGame = {
sequence: [],
playerSequence: [],
sounds: {
red: new Audio("./sounds/red.mp3"),
green: new Audio("./sounds/green.mp3"),
yellow: new Audio("./sounds/yellow.mp3"),
blue: new Audio("./sounds/blue.mp3")
},
setSequence() {
const randomNumber = Math.floor(Math.random() * 4);
const colors = ["green", "red", "yellow", "blue"];
this.sequence.push(colors[randomNumber]);
},
round() {
this.setSequence();
this.playSequence();
},
playSequence() {
isSequencePlayed = false;
this.playerSequence = [];
this.disableButtons();
this.sequence.forEach((seqColor, i) => {
setTimeout(() => {
$("#" + seqColor).addClass("pressed");
if (this.sounds[seqColor]) {
this.sounds[seqColor].play();
}
setTimeout(() => {
$("#" + seqColor).removeClass("pressed");
}, 500);
}, i * 1000);
});
setTimeout(() => {
isSequencePlayed = true;
this.enableButtons();
}, this.sequence.length * 1000 + 500);
},
enableButtons() {
$(".btn").on("click", this.handleButtonClick.bind(this));
},
disableButtons() {
$(".btn").off("click");
},
handleButtonClick(event) {
const color = $(event.target).attr("id");
$(event.target).addClass("pressed");
if (this.sounds[color]) {
this.sounds[color].play();
}
setTimeout(() => {
$(event.target).removeClass("pressed");
}, 100);
this.playerSequence.push(color);
this.checkSequence();
},
checkSequence() {
const currentPlayerIndex = this.playerSequence.length - 1;
if (this.playerSequence[currentPlayerIndex] !== this.sequence[currentPlayerIndex]) {
this.gameOver();
} else if (this.playerSequence.length === this.sequence.length) {
currentScore++;
$("#current-score").text(currentScore);
if (currentScore > highScore) {
highScore = currentScore;
$("#high-score").text(highScore);
}
setTimeout(() => {
this.round();
}, 1000);
}
},
gameOver() {
$("body").addClass("game-over");
setTimeout(() => {
$("body").removeClass("game-over");
}, 200);
currentScore = 0;
$("#current-score").text(currentScore);
$("#game-over-screen").removeClass("hidden");
},
start() {
this.sequence = [];
this.playerSequence = [];
currentScore = 0;
$("#current-score").text(currentScore);
$("#game-over-screen").addClass("hidden");
this.round();
}
};
$("#start-btn").on("click", () => {
simonGame.start();
});
$("#restart-btn").on("click", () => {
simonGame.start();
});