-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
178 lines (175 loc) · 4.87 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* @property {HTMLDivElement} green
* @property {HTMLDivElement} red
* @property {HTMLDivElement} blue
* @property {HTMLDivElement} yellow
*
*/
const buttons = {
green: document.querySelector("#green"),
red: document.querySelector("#red"),
blue: document.querySelector("#blue"),
yellow: document.querySelector("#yellow"),
};
/**
* @property {string} a
* @property {string} s
* @property {string} d
* @property {string} f
*/
const keybindings = {
a: "green",
s: "red",
d: "yellow",
f: "blue",
};
const audioFiles = {
green: new Audio('sounds/green.mp3'),
red: new Audio('sounds/red.mp3'),
blue: new Audio('sounds/blue.mp3'),
yellow: new Audio('sounds/yellow.mp3'),
wrong: new Audio('sounds/wrong.mp3')
}
const DELAY_MS = 200;
const levelTitle = document.querySelector("#level-title");
const movesElement = document.querySelector("#moves");
let commands = [];
let userChoices = [];
let gameover = false;
let level = 1;
let duringSimonShows = false;
/**
* updates moves element to show user how many moves they did
* @param {Number} moves num of moves
*/
function updateMoves(moves) {
movesElement.innerHTML = "";
for (let move of moves) {
let color = move.id;
let span = document.createElement("span");
span.style.backgroundColor = color;
span.className = "move";
movesElement.appendChild(span);
}
}
/**
* chooses random color for command
*/
function chooseRandomColor() {
const colors = Object.keys(buttons);
let randomIndex = Math.floor(Math.random() * colors.length);
return colors[randomIndex];
}
/**
* flashes each button for user
* @param {HTMLDivElement} btn
* @param {Number} pressDelay
* @param {Number} unpressDelay
* @param {boolean} last
*/
function flash(btn, pressDelay, unpressDelay, last = false) {
setTimeout(() => {
btn.classList.add("pressed");
}, pressDelay);
setTimeout(() => {
btn.classList.remove("pressed");
if (last) {
duringSimonShows = false;
}
}, unpressDelay);
}
/**
* what simon, or the game, shows the user to do. aka simons commands
*/
function simonShows() {
duringSimonShows = true;
levelTitle.innerHTML = `Level ${level}`;
let color = chooseRandomColor();
let button = buttons[color];
commands.push(button);
for (let i = 0; i < commands.length; i++) {
let btn = commands[i];
let pressDelay = i * DELAY_MS * 2;
let unpressDelay = pressDelay + 200;
if (i == commands.length - 1) {
flash(btn, pressDelay, unpressDelay, true);
} else {
flash(btn, pressDelay, unpressDelay);
}
}
}
/**
* function that runs once game is over
*/
function lost() {
levelTitle.innerHTML =
"You lost! Listen to Simon next time...<hr><h2>Press any key to start again.<h2/>";
gameover = true;
document.addEventListener("keydown", () => location.reload());
audioFiles.wrong.play();
}
/**
* function that runs on game start
*/
function onGameStart() {
gameover = false;
commands = [];
userChoices = [];
level = 1;
simonShows();
}
/**
* progress user to the next level
*/
function nextLevel() {
level++;
userChoices = [];
simonShows();
duringSimonShows = true;
updateMoves(userChoices);
}
/**
* main game function
*/
function game() {
/**
* Tracks the amount of moves user has done for the particular level, also aides in checking if move is valid.
* @type {Number}
*/
let userMovesCount = 0;
updateMoves(userChoices);
onGameStart();
for (let [_color, button] of Object.entries(buttons)) {
button.addEventListener("click", (evt) => {
if (!duringSimonShows && !gameover) {
let btnElement = evt.target;
audioFiles[btnElement.id].seek = 0;
audioFiles[btnElement.id].play();
flash(btnElement, 0, 100);
userChoices.push(btnElement);
if (commands[userMovesCount] != userChoices[userMovesCount]) {
// is the user move the same as Simon's move?
lost();
} else if (commands.length == userChoices.length) {
// is the level over?
updateMoves(userChoices);
setTimeout(() => {
nextLevel();
}, 200);
userMovesCount = 0;
} else {
// if the user is between a level, and his move was right
userMovesCount++;
updateMoves(userChoices);
}
}
});
}
document.addEventListener("keyup", (evt) => {
let color = keybindings[evt.key];
if (color) {
buttons[color].click();
}
});
}
document.addEventListener("keydown", game, { once: true });