-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
88 lines (82 loc) · 2.38 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
var gamePattern=[];
var userClickedPattern=[];
var buttonColours=["red", "blue", "green", "yellow"];
var level=0;
var button=-1;
$(".start").click(function(){
nextSequence();
$(".start").addClass("visibility");
$(".btn").click(function(){
button++;
var userChosenColour=$(this).attr("id");
animatePress(userChosenColour);
playSound(userChosenColour);
userClickedPattern.push(userChosenColour);
if(gamePattern[button]!=userClickedPattern[button])
{
$("#level-title").text("Game over, Press restart to begin again");
console.log("game pattern: "+gamePattern);
console.log("user pattern: "+userClickedPattern);
$(".start").removeClass("visibility");
gamePattern=[];
level=0;
button=-1;
}
else
{
if (level-1==button)
{
button=-1;
console.log("moving to next sequence "+userClickedPattern);
userClickedPattern=[];
setTimeout(nextSequence,1200);
// nextSequence();
}
else
{
console.log("waiting for next button "+userClickedPattern);
console.log("level-1: "+(level-1));
console.log("button: "+button);
}
}
});
});
function playaudio(color)
{
var audio=new Audio("sounds/"+color+".mp3");
audio.play();
}
function nextSequence()
{
level++;
$("#level-title").text("Level "+level);
var randomNumber=Math.round(Math.random()*3);
var randomChosenColour=buttonColours[randomNumber];
gamePattern.push(randomChosenColour);
$("#"+randomChosenColour).fadeOut(100).fadeIn(100);
playaudio(randomChosenColour);
console.log("game pattern "+ gamePattern);
}
function playSound(name)
{
if(name==null)
{
var randomNumber=Math.round(Math.random()*3);
var randomChosenColour=buttonColours[randomNumber];
gamePattern.push(randomChosenColour);
$("#"+randomChosenColour).fadeOut(100).fadeIn(100);
playaudio(randomChosenColour);
}
else
{
playaudio(name);
}
}
function animatePress(currentColour)
{
var active="#"+currentColour;
$(active).addClass("pressed");
setTimeout(function(){
$(active).removeClass("pressed");
},100);
}