-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetris.html
460 lines (430 loc) · 15.9 KB
/
tetris.html
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Tetris</title>
<style>
#tetris {
/* tetris field table should have a ratio 2:1 */
border: 1px solid black;
width: 300px;
height: 600px;
empty-cells: show;
}
#controls {
border-spacing: 10px;
}
#flex-container {
display: flex;
}
#side-info {
margin-left: 50px;
}
#game-over {
color: red;
}
</style>
</head>
<body>
<h1>Tetris</h1>
<div id="flex-container">
<!-- empty table, this will be the tetris field -->
<table id="tetris"></table>
<div id="side-info">
<h2 hidden id="game-over">Game Over!</h2>
<h3>Score: <span id="score"></span></h3>
<h3>Level: <span id="level"></span></h3>
<h3>High score: <span id="high-score"></span></h3>
<h3>Controls:</h3>
<table id="controls">
<tr>
<td>Left</td>
<td>←</td>
</tr>
<tr>
<td>Right</td>
<td>→</td>
</tr>
<tr>
<td>Down</td>
<td>↓</td>
</tr>
<tr>
<td>Drop</td>
<td>Space bar</td>
</tr>
<tr>
<td>Rotate clockwise</td>
<td>↑</td>
</tr>
<tr>
<td>Rotate counter clockwise</td>
<td>Q</td>
</tr>
<tr>
<td>Pause</td>
<td>P</td>
</tr>
<tr>
<td>New Game</td>
<td>N</td>
</tr>
<tr>
<td>Set level</td>
<td>[0 - 9]</td>
</tr>
</table>
</div>
</div>
<script>
// constants
const nr_rows = 20;
const nr_columns = 10;
const tetris_table = document.getElementById("tetris");
const colors = ["green", "blue", "red", "purple", "orange", "magenta", "cyan"];
// global variables
let drop_speed = 1000; // drop speed at level 0
let game_paused = true;
let game_over = false;
let score, level, total_lines, interval_id;
// retrieve high score from memory
let high_score;
try {
high_score = localStorage.getItem("high_score");
if (high_score == null) {
high_score = 0;
}
} catch (error) {
high_score = 0;
}
update_highscore(high_score);
// block prototypes
const blocks = [
{
shape: [
[0, 0, 0],
[1, 1, 1],
[0, 1, 0]
],
position: [-1, 4],
color_nr: 0
},
{
shape: [
[1, 1],
[1, 1]
],
position: [0, 4],
color_nr: 1
},
{
shape: [
[0, 0, 0, 0],
[1, 1, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
position: [-1, 3],
color_nr: 2
},
{
shape: [
[1, 1, 0],
[0, 1, 1],
[0, 0, 0]
],
position: [0, 4],
color_nr: 3
},
{
shape: [
[0, 1, 1],
[1, 1, 0],
[0, 0, 0]
],
position: [0, 4],
color_nr: 4
},
{
shape: [
[1, 0, 0],
[1, 1, 1],
[0, 0, 0]
],
position: [0, 4],
color_nr: 5
},
{
shape: [
[0, 0, 1],
[1, 1, 1],
[0, 0, 0]
],
position: [0, 4],
color_nr: 6
}
];
// start the game!
new_game();
function new_game() {
document.getElementById("game-over").setAttribute("hidden", true);
document.getElementById("high-score").innerHTML = high_score;
// reset score
add_score(0, true);
new_level(0, true);
total_lines = 0;
// create the field
field = [];
for (let i = 0; i < nr_rows; i++) {
field[i] = new Array(nr_columns);
}
// add field to DOM
tetris_table.innerHTML = "";
for (let i = 0; i < nr_rows; i++) {
let newRow = tetris_table.insertRow(0);
for (j = 0; j < nr_columns; j++) {
newRow.insertCell(0);
}
}
create_new_block();
game_paused = false;
game_over = false;
}
function create_new_block() {
// create block from prototype (convert via JSON to create a deep clone)
block = JSON.parse(JSON.stringify(blocks[Math.floor(Math.random() * blocks.length)]));
print_field_to_DOM();
// check if game over
if (check_block(block.shape, block.position) === false) {
// game over
game_over = true;
document.getElementById("game-over").removeAttribute("hidden");
}
}
function print_field_to_DOM() {
// print field to DOM
for (let row = 0; row < nr_rows; row++) {
let DOM_row = document.querySelectorAll("tr")[row];
for (let column = 0; column < nr_columns; column++) {
let DOM_block = DOM_row.querySelectorAll("td")[column];
if (field[row][column] >= 0) {
DOM_block.setAttribute("bgcolor", colors[field[row][column]]);
} else {
DOM_block.removeAttribute("bgcolor");
}
}
}
// print the partial blocks of the active Block to the DOM
for (let i = 0; i < block.shape.length; i++) {
let DOM_row = document.querySelectorAll("tr")[block.position[0] + i];
for (let j = 0; j < block.shape[i].length; j++) {
if (block.shape[i][j] === 1) {
let DOM_block = DOM_row.querySelectorAll("td")[block.position[1] + j];
DOM_block.setAttribute("bgcolor", colors[block.color_nr]);
}
}
}
}
function move(delta_row, delta_column) {
// check if move is legal
if (check_block(block.shape, [block.position[0] + delta_row, block.position[1] + delta_column])) {
// update position of the block
block.position[0] = block.position[0] + delta_row;
block.position[1] = block.position[1] + delta_column;
// print new field
print_field_to_DOM();
return true;
} else {
return false;
}
}
function long_drop() {
let keep_going = true;
while (keep_going) {
keep_going = move(1, 0);
}
fix_block();
}
function rotate(direction) {
// check if rotation is legal
if (check_block(transpose(block.shape, direction), block.position)) {
// update shape of the block
block.shape = transpose(block.shape, direction);
// print new field
print_field_to_DOM();
}
}
function transpose(matrix, direction) {
// pure
let transposed_shape = [];
for (let i = 0; i < matrix[0].length; i++) {
transposed_shape[i] = Array(matrix.length);
}
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[0].length; j++) {
if (direction == 0) {
// transpose clockwise:
transposed_shape[j][matrix.length - 1 - i] = matrix[i][j];
}
if (direction == 1) {
// transpose counter clockwise:
transposed_shape[matrix[0].length - 1 - j][i] = matrix[i][j];
}
}
}
return transposed_shape;
}
function check_block(shape, position) {
// loop over block elements, to check if they fit in the field
for (let i = 0; i < shape.length; i++) {
for (let j = 0; j < shape[i].length; j++) {
if (shape[i][j] === 1) {
if (validate_cell(position[0] + i, position[1] + j) === false) {
return false;
}
}
}
}
return true;
}
function validate_cell(row, column) {
if (row >= nr_rows || row < 0 || column >= nr_columns || column < 0) {
// cell is outside the field
return false;
}
if (field[row][column] != null) {
// cell is already occupied
return false;
} else {
// cell is available
return true;
}
}
function remove_full_rows() {
let nr_full_rows = 0;
for (let i = 0; i < nr_rows; i++) {
if (validate_fullrow(i)) {
remove_row(i);
nr_full_rows++;
}
}
return nr_full_rows;
}
function validate_fullrow(row) {
for (let column = 0; column < nr_columns; column++) {
if (field[row][column] == null) {
return false;
}
}
return true;
}
function remove_row(row) {
// remove full row from field, and add a new one
field.splice(row, 1);
field.unshift(new Array(nr_columns));
}
function tick() {
if (!game_paused && !game_over) {
// if possible, drop block
if (move(1, 0) == false) {
fix_block();
}
}
}
function fix_block() {
add_block_to_field();
let nr_full_rows = remove_full_rows();
total_lines = total_lines + nr_full_rows;
let points = (1 + level / 10) * 100 * nr_full_rows ** 2;
if (points > 0) {
add_score(points);
}
create_new_block();
}
function add_block_to_field() {
for (let i = 0; i < block.shape.length; i++) {
for (let j = 0; j < block.shape[i].length; j++) {
if (block.shape[i][j] === 1) {
field[block.position[0] + i][block.position[1] + j] = block.color_nr;
}
}
}
}
function add_score(new_points, reset) {
if (reset) {
score = 0;
}
score = score + new_points;
document.getElementById("score").innerHTML = score;
new_level(min_level());
if (score > high_score) {
update_highscore(score);
}
}
function update_highscore(new_highscore) {
high_score = new_highscore;
localStorage.setItem("high_score", new_highscore);
document.getElementById("high-score").innerHTML = new_highscore;
}
function new_level(new_level, force) {
if (new_level > level || force) {
level = new_level;
document.getElementById("level").innerHTML = level;
// drop the active block at the correct speed
drop_speed = 1000 * Math.pow(0.75, level); // each level speed inscreases ~33%%
if (interval_id) {
window.clearInterval(interval_id);
}
interval_id = window.setInterval(tick, drop_speed);
}
}
function min_level() {
return Math.floor(total_lines / 5);
}
// event listeners:
window.addEventListener("keyup", function(e) {
// console.log(e.keyCode);
if (!game_paused && !game_over) {
// left: 37
if (e.keyCode === 37) {
move(0, -1);
}
// right: 39
if (e.keyCode === 39) {
move(0, 1);
}
// down: 40
if (e.keyCode === 40) {
move(1, 0);
}
// q: 81
if (e.keyCode === 81) {
rotate(1);
}
// up: 38
if (e.keyCode === 38) {
rotate(0);
}
// space bar: 32
if (e.keyCode === 32) {
long_drop();
}
// p: 80
if (e.keyCode === 80) {
game_paused = true;
}
// numerical keys [0 - 9] have keyCodes [48 - 57]
if (e.keyCode >= 48 && e.keyCode <= 57) {
new_level(e.keyCode - 48);
}
} else {
game_paused = false;
}
// n: 78
if (game_over && e.keyCode === 78) {
new_game();
}
});
</script>
</body>
</html>