-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
95 lines (77 loc) · 2.4 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
83
84
85
86
87
88
89
90
91
92
93
94
95
var buttonColours = ["red", "blue", "green", "yellow"];
var gamePattern = []
var userClickedPattern = []
var level = 0;
var started = false;
var winning = 1;
var currentindex = 0;
$(document).keypress(function () {
if (!started) {
$("#level-title").text("Level " + level);
nextSequence();
started = true;
}
});
$(".btn").click(function () {
userClickedPattern.push(this.id);
console.log(userClickedPattern);
play(this.id);
animatePress(this.id);
checkanswer(level, currentindex);
});
function nextSequence() {
userClickedPattern = [];
level++;
$(".container").show();
$("body").css("background-color", "#011F3F");
$("#level-title").text("LEVEL " + level);
randomNumber = Math.floor(Math.random(0, 1) * 4);
randomChosenColour = buttonColours[randomNumber];
gamePattern.push(randomChosenColour);
$("#" + randomChosenColour).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
play(randomChosenColour);
console.log(gamePattern);
}
function play(color) {
switch (color) {
case "green": (new Audio("sounds/green.mp3")).play(); break;
case "red": (new Audio("sounds/red.mp3")).play(); break;
case "yellow": (new Audio("sounds/yellow.mp3")).play(); break;
case "blue": (new Audio("sounds/blue.mp3")).play(); break;
default: (new Audio("sounds/wrong.mp3")).play();
}
}
function restart() {
$("body").css("background-color", "red");
$("#level-title").html("GAME OVER<br> PRESS ANY KEY TO RESTART");
$(".container").hide();
play();
gamePattern = []
started = false;
level = 0;
currentindex = 0;
}
function animatePress(currentColour) {
$("#" + currentColour).addClass("pressed");
setTimeout(function () {
$("#" + currentColour).removeClass("pressed");
}, 100);
}
function checkanswer() {
if (currentindex < level) {
if (userClickedPattern[currentindex] == gamePattern[currentindex]) {
currentindex++;
}
else { restart(); }
}
if (currentindex == level) {
if (userClickedPattern[currentindex] == gamePattern[currentindex]) {
currentindex = 0;
userClickedPattern = [];
setTimeout(nextSequence, 1000);
}
else {
restart();
}
}
}