-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
57 lines (52 loc) · 1.29 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
let sequence = [];
let level = 0;
let score = 0;
let gameOver = false;
$('#start').click(function (e) {
e.preventDefault();
resetGame();
});
function resetGame() {
sequence = [];
level = 0;
score = 0;
gameOver = false;
$('#score').text(`Score: ${score}`);
$('#message').text('');
execute();
}
function execute() {
level++;
$('#message').text(`Level ${level}`);
const randomNum = Math.floor(Math.random() * 4);
const array = ['red', 'yellow', 'blue', 'darkgreen'];
const color = array[randomNum];
sequence.push(color);
console.log(sequence);
let val = '#' + color;
$(val)
.animate({ opacity: 0.5 })
.animate({ opacity: 1 }, () => {
if (!gameOver) {
addClickHandlers();
}
});
}
function addClickHandlers() {
let clicked = 0;
$('.container button').click(function () {
const clickedColor = $(this).attr('data-action');
const expectedColor = sequence[clicked];
if (clickedColor === expectedColor) {
clicked++;
if (clicked === sequence.length) {
score++;
$('#score').text(`Score: ${score}`);
setTimeout(execute, 1000);
}
} else {
gameOver = true;
$('#message').text(`Game over. Your score was ${score}.`);
}
});
}