-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
95 lines (65 loc) · 1.75 KB
/
index.js
File metadata and controls
95 lines (65 loc) · 1.75 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
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 buttomColours = ["red","blue","green","yellow"];
var gamePattern =[];
var userChosenPattern = [];
var started = false;
$(document).on("keydown" , function(){
if(!started) {
nextSequence();
started = true;
}
});
$(".Ball").click(
function (){
var userChosenColour = $(this).attr("id");
userChosenPattern.push(userChosenColour);
$("#"+ userChosenColour).fadeIn(200).fadeOut(200).fadeIn(200);
var audio = new Audio ("./sounds/"+userChosenColour+".mp3");
audio.play();
checkAnswer(userChosenPattern.length - 1);
});
function checkAnswer(value)
{
if(gamePattern[value] === userChosenPattern[value])
{
console.log("Success!");
if(userChosenPattern.length === gamePattern.length)
{
setTimeout(
function() {nextSequence();}
, 1000);
}
}
else {
$(".words").text("Game Over. Press A key to Restart");
$("body").removeClass("background");
setTimeout(function(){
$("body").addClass("background");
},200);
var audio = new Audio ("./sounds/wrong.mp3");
audio.play();
startOver();
}
}
var level = 1;
function nextSequence ()
{
userChosenPattern =[];
$(".words").text("Level " + level);
level++;
var random = Math.random();
var randomNumber = Math.floor(4*random);
var randomChosenColour = buttomColours[randomNumber];
gamePattern.push(randomChosenColour);
$("#" + randomChosenColour).addClass("pressed");
setTimeout( function()
{
$("#" + randomChosenColour).removeClass("pressed")}
,1000);
var audio = new Audio ("./sounds/"+ randomChosenColour+".mp3");
audio.play();
}
function startOver(){
level = 0;
gamePattern = [];
started = false;
}