-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (114 loc) · 4.27 KB
/
index.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
////////////////////////////////// Global Variables ///////////////////////////////////
// Game level
var level = 1;
var goalDelay = 500; // Delay between each button in the goal sequence (in ms)
var afterResponseDelay = 1250; // Delay after user's response (in ms)
// Get all buttons (div.col's) in this order: maroon, turquoise, seagreen, gold
var buttons = [];
buttons.push($('.col')[0], $('.col')[1], $('.col')[2], $('.col')[3]);
// Array to store the goal sequence
var goalSequence = [];
// Index to check the user's response: incremented on each call of checkResponse()
var checkIndex = -1;
///////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////// Utility Functions ///////////////////////////////////
function game() {
// Remove the rules class
$('.rules').hide();
// Remove the Start-Game listener and user-response listener
$(document).off('keypress');
$(document).off('click');
$('.col').off('click');
// Dislay the level
$('h1').text(`Level ${level}`);
// Update the goal sequence
updateGoalSequence();
// Start playing the buttons in goal sequence
for(let i = 0; i < goalSequence.length; i++) {
setTimeout(function () { playButton(goalSequence[i].id); }, goalDelay*i);
}
increaseDifficultyByTime();
// Start listening for user's response
setTimeout(function () {
$('.col').on('click', function () { checkResponse(this) });
$(document).on('keypress', function (event) { wesdButtonsPressed(event) });
}, goalDelay*goalSequence.length);
}
function increaseDifficultyByTime() {
if (goalDelay > 100) {
goalDelay -= 20;
afterResponseDelay -= 20;
}
}
function updateGoalSequence() {
var randomIndex = Math.floor(Math.random() * 4);
goalSequence.push(buttons[randomIndex]);
}
function playButton(elementId) {
$(`#${elementId}`).addClass('pressed');
window.setTimeout(function () { $(`#${elementId}`).removeClass('pressed'); }, 150);
playSound(elementId);
}
function gameOver() {
playSound('gameOver');
$('h1').text('Game Over, Tap/Press Any Key to Restart');
$('.level').text(`Level Reached: ${level}`);
level = 1;
window.setTimeout(function () {
$(document).on('keypress', function () { location.reload();} ); // Reload the page
$(document).on('click', function () { location.reload();} ); // Reload the page
}, 1000);
}
function playSound(fileName) {
var audio = new Audio(`sounds/${fileName}.wav`);
audio.play();
}
function checkResponse(htmlElement) {
checkIndex++;
if (htmlElement == goalSequence[checkIndex]) {
playButton(htmlElement.id);
} else {
$('.col').off('click');
$(document).off('keypress');
goalSequence = [];
gameOver();
}
if (checkIndex == goalSequence.length - 1) {
checkIndex = -1;
level++;
$('.col').off('click');
$(document).off('keypress');
setTimeout(function () { game(); }, afterResponseDelay);
}
}
function wesdButtonsPressed(event) {
switch (event.key) {
case 'w':
checkResponse(buttons[0]);
break;
case 'e':
checkResponse(buttons[1]);
break;
case 's':
checkResponse(buttons[2]);
break;
case 'd':
checkResponse(buttons[3]);
break;
default:
alert('Invalid key pressed! Press only w, e, s, d keys.');
break;
}
}
//////////////////////////////////////////////////////////////////////////////////////
// Executable code begins here (like main())
/*************************************** MAIN ***************************************/
// Press any key to start Listener
$(document).on('keypress', game); // Call game() on keypress or...
$(document).on('click', game); // Call game() on screen-click.
// Drop-down menu listener
$('#drop-down-btn').click(function (event) {
event.stopPropagation();
$('.drop-down-menu').slideToggle();
});
/************************************************************************************/