-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
272 lines (243 loc) · 8.09 KB
/
script.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
const gameBoard = document.getElementById('gameBoard');
const width = 10;
const height = 20;
let squares = [];
let currentPosition = 4;
let currentRotation = 0;
let timerId;
const colors = [
'#3498db',
'#e74c3c',
'#2ecc71',
'#f1c40f',
'#9b59b6',
'#34495e',
'#1abc9c'
];
// The Tetrominoes
const lTetromino = [
[1, width + 1, width * 2 + 1, 2],
[width, width + 1, width + 2, width * 2 + 2],
[1, width + 1, width * 2 + 1, width * 2],
[width, width * 2, width * 2 + 1, width * 2 + 2]
];
const zTetromino = [
[0, width, width + 1, width * 2 + 1],
[width + 1, width + 2, width * 2, width * 2 + 1],
[0, width, width + 1, width * 2 + 1],
[width + 1, width + 2, width * 2, width * 2 + 1]
];
const tTetromino = [
[1, width, width + 1, width + 2],
[1, width + 1, width + 2, width * 2 + 1],
[width, width + 1, width + 2, width * 2 + 1],
[1, width, width + 1, width * 2 + 1]
];
const oTetromino = [
[0, 1, width, width + 1],
[0, 1, width, width + 1],
[0, 1, width, width + 1],
[0, 1, width, width + 1]
];
const iTetromino = [
[1, width + 1, width * 2 + 1, width * 3 + 1],
[width, width + 1, width + 2, width + 3],
[1, width + 1, width * 2 + 1, width * 3 + 1],
[width, width + 1, width + 2, width + 3]
];
const theTetrominoes = [lTetromino, zTetromino, tTetromino, oTetromino, iTetromino];
// Randomly select a Tetromino and its first rotation
let random = Math.floor(Math.random() * theTetrominoes.length);
let current = theTetrominoes[random][currentRotation];
let nextRandom = 0;
// Audio Elements
const backgroundMusic = document.getElementById('backgroundMusic');
const gameOverSound = document.getElementById('gameOverSound');
// Draw the Tetromino
function draw() {
current.forEach(index => {
squares[currentPosition + index].classList.add('tetromino');
squares[currentPosition + index].style.backgroundColor = colors[random];
});
}
// Undraw the Tetromino
function undraw() {
current.forEach(index => {
squares[currentPosition + index].classList.remove('tetromino');
squares[currentPosition + index].style.backgroundColor = '';
});
}
// Make the Tetromino move down every second
function moveDown() {
undraw();
currentPosition += width;
draw();
freeze();
}
// Freeze function
function freeze() {
if (current.some(index => squares[currentPosition + index + width].classList.contains('taken') || currentPosition + index + width >= width * height)) {
current.forEach(index => squares[currentPosition + index].classList.add('taken'));
random = nextRandom;
nextRandom = Math.floor(Math.random() * theTetrominoes.length);
current = theTetrominoes[random][currentRotation];
currentPosition = 4;
draw();
addScore();
if (current.some(index => squares[currentPosition + index].classList.contains('taken'))) {
gameOver();
}
}
}
// Move the tetromino left, unless it is at the edge or there is a blockage
function moveLeft() {
undraw();
const isAtLeftEdge = current.some(index => (currentPosition + index) % width === 0);
if (!isAtLeftEdge) currentPosition -= 1;
if (current.some(index => squares[currentPosition + index].classList.contains('taken'))) {
currentPosition += 1;
}
draw();
}
// Move the tetromino right, unless it is at the edge or there is a blockage
function moveRight() {
undraw();
const isAtRightEdge = current.some(index => (currentPosition + index) % width === width - 1);
if (!isAtRightEdge) currentPosition += 1;
if (current.some(index => squares[currentPosition + index].classList.contains('taken'))) {
currentPosition -= 1;
}
draw();
}
// Rotate the tetromino
function rotate() {
undraw();
currentRotation++;
if (currentRotation === current.length) { // If the current rotation gets to 4, make it go back to 0
currentRotation = 0;
}
current = theTetrominoes[random][currentRotation];
draw();
}
// Show up-next tetromino in mini-grid display
const displaySquares = document.querySelectorAll('.mini-grid div');
const displayWidth = 4;
let displayIndex = 0;
// The Tetrominos without rotations
const upNextTetrominoes = [
[1, displayWidth + 1, displayWidth * 2 + 1, 2], // lTetromino
[0, displayWidth, displayWidth + 1, displayWidth * 2 + 1], // zTetromino
[1, displayWidth, displayWidth + 1, displayWidth + 2], // tTetromino
[0, 1, displayWidth, displayWidth + 1], // oTetromino
[1, displayWidth + 1, displayWidth * 2 + 1, displayWidth * 3 + 1] // iTetromino
];
// Display the shape in the mini-grid display
function displayShape() {
// Remove any trace of a tetromino form the entire grid
displaySquares.forEach(square => {
square.classList.remove('tetromino');
square.style.backgroundColor = '';
});
upNextTetrominoes[nextRandom].forEach(index => {
displaySquares[displayIndex + index].classList.add('tetromino');
displaySquares[displayIndex + index].style.backgroundColor = colors[nextRandom];
});
}
// Add functionality to the button
function startGame() {
if (timerId) {
clearInterval(timerId);
timerId = null;
} else {
resetGame();
draw();
timerId = setInterval(moveDown, 1000);
nextRandom = Math.floor(Math.random() * theTetrominoes.length);
displayShape();
console.log('Playing background music');
backgroundMusic.play();
}
}
// Reset the game state
function resetGame() {
squares.forEach(square => {
square.classList.remove('tetromino', 'taken');
square.style.backgroundColor = '';
});
currentPosition = 4;
currentRotation = 0;
random = Math.floor(Math.random() * theTetrominoes.length);
current = theTetrominoes[random][currentRotation];
score = 0;
scoreDisplay.innerHTML = 'Score: 0';
}
// Add score
const scoreDisplay = document.getElementById('score');
let score = 0;
function addScore() {
for (let i = 0; i < 199; i += width) {
const row = [i, i + 1, i + 2, i + 3, i + 4, i + 5, i + 6, i + 7, i + 8, i + 9];
if (row.every(index => squares[index].classList.contains('taken'))) {
score += 10;
scoreDisplay.innerHTML = 'Score: ' + score;
row.forEach(index => {
squares[index].classList.remove('taken');
squares[index].classList.remove('tetromino');
squares[index].style.backgroundColor = '';
});
const squaresRemoved = squares.splice(i, width);
squares = squaresRemoved.concat(squares);
squares.forEach(cell => gameBoard.appendChild(cell));
}
}
}
// Game Over
function gameOver() {
if (current.some(index => squares[currentPosition + index].classList.contains('taken'))) {
scoreDisplay.innerHTML = 'Game Over';
clearInterval(timerId);
backgroundMusic.pause();
backgroundMusic.currentTime = 0; // Reset the music to the beginning
gameOverSound.play();
}
}
// Create Board
function createBoard() {
for (let i = 0; i < width * height; i++) {
const square = document.createElement('div');
square.classList.add('cell');
gameBoard.appendChild(square);
squares.push(square);
}
// Create the bottom row of the board
for (let i = 0; i < width; i++) {
const square = document.createElement('div');
square.classList.add('taken');
gameBoard.appendChild(square);
squares.push(square);
}
}
createBoard();
// Assign functions to keyCodes
function control(e) {
if (e.keyCode === 37) {
moveLeft();
} else if (e.keyCode === 38) {
rotate();
} else if (e.keyCode === 39) {
moveRight();
} else if (e.keyCode === 40) {
moveDown();
}
}
document.addEventListener('keyup', control);
// Toggle Music Function
function toggleMusic() {
if (backgroundMusic.paused) {
console.log('Playing background music');
backgroundMusic.play();
} else {
console.log('Pausing background music');
backgroundMusic.pause();
}
}