-
Notifications
You must be signed in to change notification settings - Fork 0
/
game2.js
157 lines (144 loc) · 4.21 KB
/
game2.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var gameOver = 1;
lastPattern = [];
buttonColours = [ "red", "blue", "green", "yellow" ];
function chooseColour()
{
return buttonColours[Math.floor(Math.random()*4)];
}
var randomChosenColour; // chosen by computer
var arrayEndPointer = -1; // points to last element of last successful pattern(its length-1 actually)
var arrayPointer=-1; // temporary variable, changes accorging to user click.
$(".bigbtn").click(function(){ startGame(); });
$(".red").click( function(){ pressed("red"); } );
$(".blue").click( function(){ pressed("blue"); } );
$(".green").click( function(){ pressed("green"); } );
$(".yellow").click( function(){ pressed("yellow"); } );
function pressed(colour)
{ colourBlink(colour,100);
if(gameOver==0)
{ $(".bigbtn p").slideUp();
++arrayPointer;
if(arrayPointer > arrayEndPointer || lastPattern[arrayPointer]!=colour)
{ console.log("it's over ")
$(".bigbtn h4").text("Game Over");
$(".bigbtn h4").fadeOut().fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
$(".bigbtn p").text("to start,");
setTimeout(function()
{
$(".bigbtn p").text("to start,");
$(".bigbtn p").slideDown();
$(".bigbtn h4").text("click here");
},2000);
gameOver = 1;
gameOverSound();
}
else if(arrayPointer == arrayEndPointer)
{ $(".bigbtn h4").text("complete match").fadeOut().fadeIn().fadeOut().fadeIn();
arrayPointer = -1;
randomChosenColour = chooseColour();
colourBlinkAndAssign(randomChosenColour,1500);
}
else
{
$(".bigbtn h4").text("match").fadeOut().fadeIn();
console.log("match");
}
}
else
{
gameOverSound();
}
}
function startGame()
{
if(gameOver==1)
{
$(".bigbtn").addClass("pressed");
setTimeout(function(){
$(".bigbtn").removeClass("pressed");
}, 100);
$(".bigbtn p").text("wait");
$(".bigbtn h4").text("watch your left");
wordAnime("wait");
gameOver = 0
randomChosenColour = chooseColour();
lastPattern = [];
arrayEndPointer = -1;
arrayPointer = -1;
colourBlinkAndAssign(randomChosenColour,3000);
}
}
function colourBlink(colour,time)
{ setTimeout(function()
{
$("." + colour).addClass("pressed");
setTimeout(function(){
$("." + colour).removeClass("pressed");
var audio = new Audio("sounds/" + colour + ".mp3");
audio.play();
}, 300);
},time);
}
function colourBlinkAndAssign(colour,time)
{
setTimeout(function()
{
$("." + colour).addClass("pressed");
setTimeout(function(){
$("." + colour).removeClass("pressed");
var audio = new Audio("sounds/" + colour + ".mp3");
audio.play();
}, 300);
lastPattern.push(colour);
arrayEndPointer++;
},time);
}
function gameOverSound()
{
var audio = new Audio("sounds/wrong.mp3");
audio.play();
}
function wordAnime(word)
{
setTimeout(function(){
$(".bigbtn p").text(word + ".")
}, 500);
setTimeout(function(){
$(".bigbtn p").text(word + "..")
}, 1000);
setTimeout(function(){
$(".bigbtn p").text(word + "...")
}, 1500);
setTimeout(function(){
$(".bigbtn p").text(word + "....")
}, 2000);
setTimeout(function(){
$(".bigbtn p").text(word + ".....")
}, 2500);
}
var vpw = $(window).width();
var vph = $(window).height();
console.log(String(vpw)+" "+String(vph));
if(vpw < 600)
{
$(".bigbtn h3,.bigbtn h4,.bigbtn p").css("font-size","2vw")
}
if(vpw < 550)
{
$(".end").css("height", String(0.6*vph) + "px");
}
else if(vpw > 550 && vpw < 800)
{
$(".end").css("height", String(0.5*vph) + "px");
$("h6").css("padding-bottom", "50px");
}
else if(vpw > 800 && vpw < 1080)
{
$(".end").css("height", String(0.5*vpw) + "px");
$("h6").css("padding-bottom", "50px");
}
else
{
$(".end").css("height", String(0.4*vpw) + "px");
$("h6").css("padding-bottom", "30px");
}