-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
64 lines (49 loc) · 1.37 KB
/
game.js
File metadata and controls
64 lines (49 loc) · 1.37 KB
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
var colours = ["colour1", "colour2", "colour3", "colour4"];
var sequence = [];
var seqIndex = 0;
var started = false;
var level = 0;
/* Start Game by Keypress */
$(document).keypress(function() {
if (started === true) return;
nextColour();
started = true;
});
/* Play Game through Clicks */
$(".btn").click(function() {
if (started === false) return;
var clickedColour = this.id;
animatePress(clickedColour);
if (clickedColour === sequence[seqIndex]) {
seqIndex++;
if (seqIndex === sequence.length) {
nextColour();
seqIndex = 0;
}
} else {
restart();
}
});
function nextColour() {
var randIndex = Math.floor(Math.random() * 4);
var randColour = colours[randIndex];
$("#" + randColour).fadeOut().fadeIn();
new Audio("./sounds/" + randColour + ".mp3").play();
sequence.push(randColour);
$("#level-title").text("Level " + level).css('color', 'white');
level++;
}
function animatePress(colour) {
$('#' + colour).addClass("pressed");
setTimeout(function() {
$('#' + colour).removeClass("pressed");
}, 100);
new Audio("./sounds/" + colour + ".mp3").play();
}
function restart() {
$("#level-title").text("Game Over. Press any Key to Start.").css('color', 'red');
sequence = [];
seqIndex = 0;
level = 0;
started = false;
}