-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (60 loc) · 2.14 KB
/
index.js
File metadata and controls
74 lines (60 loc) · 2.14 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
// alert("Simon is a game of short-term memory skill. It creates a series of tones and lights and requires a user to repeat the sequence. If the user succeeds, the series becomes progressively longer and more complex. Once the user messes up sequence, the game is over. ENJOY!")
var buttonColors = ["green","red","yellow","blue"];
var gamePattern = [];
var userPattern=[];
var red = new Audio("sounds/red.mp3");
var blue = new Audio("sounds/blue.mp3");
var green = new Audio("sounds/green.mp3");
var yellow = new Audio("sounds/yellow.mp3");
var fail = new Audio("sounds/wrong.mp3");
var level = 0;
var start = false;
function nextSequence(){
userPattern = [];
level++;
$("h1").text("Level "+level);
var ran = Math.floor(Math.random()*4);
var randomChosenColour = buttonColors[ran];
gamePattern.push(randomChosenColour);
$("#"+randomChosenColour).fadeIn(100).fadeOut(100).fadeIn(100);
var audio = new Audio("sounds/"+randomChosenColour+".mp3");
audio.play();
}
function animatePress(currentColor){
$("#"+currentColor).addClass("button-animation");
setTimeout(function(){
$("#"+currentColor).removeClass("button-animation");
},100);
}
function playSound(name) {
var audio = new Audio("sounds/" + name + ".mp3");
audio.play();
}
$(document).keypress(function(){
if(!start) {
nextSequence();
start = true;
}
});
$(".square").click(function(){
var userSelectedColor = $(this).attr("id");
animatePress(userSelectedColor);
userPattern.push(userSelectedColor);
playSound(userSelectedColor);
if(gamePattern[userPattern.length-1]===userPattern[userPattern.length-1]){
if(gamePattern.length===userPattern.length){
setTimeout(function(){
nextSequence();
},700);
}
}
else{
fail.play();
$("body").css("background-color","red");
$("h1").css("color","azure");
$("h1").text("GameOver, Your score: "+level+". Press any key to restart!");
$(document).keypress(function(){
location.reload();
});
}
});