-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
507 lines (466 loc) · 16.6 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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
const GRID_WIDTH = 10;
const GRID_HEIGHT = 20;
const SHAPES = {
'J': [{x: -1, y: -1}, {x: -1, y: 0}, {x: 0, y: 0}, {x: 1, y: 0}],
'L': [{x: -1, y: 0}, {x: 0, y: 0}, {x: 1, y: 0}, {x: -1, y: 1}],
'I': [{x: -1, y: 0}, {x: 0, y: 0}, {x: 1, y: 0}, {x: 2, y: 0}],
'S': [{x: 0, y: 0}, {x: 1, y: 0}, {x: -1, y: 1}, {x: 0, y: 1}],
'Z': [{x: -1, y: 0}, {x: 0, y: 0}, {x: 0, y: 1}, {x: 1, y: 1}],
'T': [{x: -1, y: 0}, {x: 0, y: 0}, {x: 1, y: 0}, {x: 0, y: 1}],
};
const COLORS = ['#0FEBE3', '#05B6C5', '#F43086', '#FF89B4'];
var game = null;
var ctx = null;
var nextCtx = null;
var shadowCtx = null;
var nextShadowCtx = null;
var gameOverScreen = null;
class Game {
constructor() {
this._grid = Array.from({length: GRID_WIDTH * GRID_HEIGHT}).fill(0);
this._clearedLines = 0;
this._score = 0;
this._droppingPiece = newPiece();
this._nextPiece = newPiece();
this._highestBlockY = GRID_HEIGHT;
this._gameOver = false;
this._timeoutId = null;
this.tick = this.tick.bind(this);
this.handleInput = this.handleInput.bind(this);
this.render = this.render.bind(this);
this._updateScores();
window.addEventListener('keydown', this.handleInput);
window.requestAnimationFrame(this.render);
this._scheduleTick();
}
tick() {
this._timeoutId = null;
if (this._isPieceResting(this._droppingPiece)) {
this._commitAndSetupNewPiece();
} else {
this._droppingPiece.position.y += 1;
}
window.requestAnimationFrame(this.render);
this._scheduleTick();
}
_scheduleTick() {
if (this._gameOver) {
return;
}
if (this._timeoutId !== null) {
window.clearTimeout(this._timeoutId);
}
const level = Math.floor(this._clearedLines / 10);
let frames;
if (level < 9) {
frames = 48 - (level * 5);
} else if (level < 10) {
frames = 6;
} else if (level < 13) {
frames = 5;
} else if (level < 16) {
frames = 4;
} else if (level < 19) {
frames = 3;
} else if (level < 29) {
frames = 2;
} else {
frames = 1;
}
this._timeoutId = window.setTimeout(this.tick, (frames / 60) * 1000);
}
endGame() {
this._gameOver = true;
window.clearTimeout(this._timeoutId);
window.removeEventListener('keydown', this.handleInput);
gameOverScreen.querySelector('#game-over-score').textContent = this._score.toLocaleString();
gameOverScreen.classList.remove('disabled');
gameOverScreen.classList.add('enabled');
window.addEventListener('keydown', gameOverInputHandler);
document.getElementById('play-again-btn').addEventListener('click', playAgain);
}
_updateScores() {
for (const span of document.querySelectorAll('.score-span')) {
span.textContent = this._score.toLocaleString();
}
for (const span of document.querySelectorAll('.level-span')) {
span.textContent = Math.floor(this._clearedLines / 10);
}
}
_commitAndSetupNewPiece() {
this._commitPieceToGrid(this._droppingPiece);
if (this._collision(this._nextPiece)) {
this.endGame();
} else {
this._droppingPiece = this._nextPiece;
this._nextPiece = newPiece();
}
}
_lookupGrid(x, y) {
return this._grid[(y * GRID_WIDTH) + x];
}
_setGrid(x, y, cell) {
this._grid[(y * GRID_WIDTH) + x] = cell;
}
_checkMovement(piece, dir) {
for (const coord of piece.data) {
const x = coord.x + piece.position.x + dir;
const y = coord.y + piece.position.y;
if (x < 0 || x >= GRID_WIDTH || this._lookupGrid(x, y) != 0) {
return false;
}
}
return true;
}
_collision(piece) {
for (const coord of piece.data) {
const x = coord.x + piece.position.x;
const y = coord.y + piece.position.y;
if (this._lookupGrid(x, y) != 0) {
return true;
}
}
return false;
}
_isPieceResting(piece) {
for (const coord of piece.data) {
const x = coord.x + piece.position.x;
const y = coord.y + piece.position.y + 1;
if (y >= GRID_HEIGHT || this._lookupGrid(x, y) != 0) {
return true;
}
}
return false;
}
_rotateAndCheck(shape, dir) {
let testShape = {
data: shape.data.map(c => { return {...c}; }),
position: {...shape.position}
};
rotate(testShape, dir);
let [top, right, bottom, left] = measurePiece(testShape);
top += testShape.position.y;
right += testShape.position.x;
bottom += testShape.position.y;
left += testShape.position.x;
if (top < 0) {
testShape.position.y -= top;
top = 0;
bottom -= top;
}
if (left < 0) {
testShape.position.x -= left;
left = 0;
right -= left;
} else if (right >= GRID_WIDTH) {
testShape.position.x -= (right - GRID_WIDTH) + 1;
right = GRID_WIDTH - 1;
left -= (right - GRID_WIDTH) + 1;
}
for (let i = 0; i < testShape.data.length; i += 1) {
const coord = testShape.data[i];
const x = coord.x + testShape.position.x;
const y = coord.y + testShape.position.y;
if (y >= GRID_HEIGHT || this._lookupGrid(x, y) != 0) {
return false;
}
}
shape.data = testShape.data;
shape.position = testShape.position;
return true;
}
_findCompletedLines(minY, maxY) {
// Check for complete rows
let completedLines = [];
for (let y = minY; y <= maxY; y += 1) {
let complete = true;
for (let x = 0; x < GRID_WIDTH; x += 1) {
if (this._lookupGrid(x, y) == 0) {
complete = false;
break;
}
}
if (complete) {
completedLines.push(y);
}
}
return completedLines;
}
_calculatePoints(linesCleared) {
const level = Math.floor(this._clearedLines / 10) + 1;
switch (linesCleared) {
case 1: return 40 * level;
case 2: return 100 * level;
case 3: return 300 * level;
case 4: return 1200 * level;
}
}
_commitPieceToGrid(piece) {
let minY = GRID_HEIGHT;
let maxY = -1;
for (const coord of piece.data) {
const x = coord.x + piece.position.x;
const y = coord.y + piece.position.y;
this._setGrid(x, y, piece.color);
minY = Math.min(minY, y);
maxY = Math.max(maxY, y);
}
this._highestBlockY = Math.min(this._highestBlockY, minY);
const completedLines = this._findCompletedLines(minY, maxY);
if (completedLines.length > 0) {
this._clearedLines += completedLines.length;
this._score += this._calculatePoints(completedLines.length);
this._updateScores();
// Clear the completed lines
let completedLine = completedLines.pop();
let wy = completedLine;
let ry = completedLine;
while (wy >= this._highestBlockY) {
if (ry === completedLine) {
// Move the read pointer up a row to skip this line.
ry -= 1;
completedLine = completedLines.pop();
} else {
// Copy the read row to the write row.
if (ry < 0) {
this._grid.fill(0, wy * GRID_WIDTH, (wy + 1) * GRID_WIDTH);
} else {
this._grid.copyWithin(wy * GRID_WIDTH, ry * GRID_WIDTH, (ry + 1) * GRID_WIDTH);
}
wy -= 1;
ry -= 1;
}
}
}
}
render() {
const CELL_SIZE = ctx.canvas.height / GRID_HEIGHT;
this._renderImpl(ctx, CELL_SIZE);
this._renderImpl(shadowCtx, CELL_SIZE);
this._renderNextPiece(nextCtx, CELL_SIZE);
this._renderNextPiece(nextShadowCtx, CELL_SIZE);
}
_renderImpl(ctx, CELL_SIZE) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
// Draw the grid.
ctx.save();
ctx.strokeStyle = 'black';
ctx.lineWidth = 0.5;
for (let y = 0; y < GRID_HEIGHT; y += 1) {
for (let x = 0; x < GRID_WIDTH; x += 1) {
const cell = this._lookupGrid(x, y);
if (cell != 0) {
ctx.beginPath();
ctx.fillStyle = COLORS[cell - 1];
ctx.rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
ctx.fill();
ctx.stroke();
}
}
}
ctx.restore();
// Draw the shadow.
const shadow = {
...this._droppingPiece,
position: {...this._droppingPiece.position}
};
while (!this._isPieceResting(shadow)) {
shadow.position.y += 1;
}
ctx.save();
ctx.globalCompositeOperation = 'screen';
ctx.globalAlpha = 0.2;
ctx.fillStyle = COLORS[this._droppingPiece.color - 1];
ctx.shadowBlur = 20;
ctx.shadowColor = COLORS[this._droppingPiece.color - 1];
ctx.beginPath();
for (const coord of shadow.data) {
const x = (shadow.position.x + coord.x) * CELL_SIZE;
const y = (shadow.position.y + coord.y) * CELL_SIZE;
ctx.rect(x, y, CELL_SIZE, CELL_SIZE);
}
ctx.fill();
ctx.globalAlpha = 1.0;
ctx.globalCompositeOperation = 'normal';
ctx.restore();
// Draw the falling piece.
ctx.save();
ctx.fillStyle = COLORS[this._droppingPiece.color - 1];
ctx.strokeStyle = 'black';
ctx.lineWidth = 0.5;
ctx.beginPath();
for (const coord of this._droppingPiece.data) {
const x = (this._droppingPiece.position.x + coord.x) * CELL_SIZE;
const y = (this._droppingPiece.position.y + coord.y) * CELL_SIZE;
ctx.rect(x, y, CELL_SIZE, CELL_SIZE);
}
ctx.fill();
ctx.stroke();
ctx.restore();
}
_renderNextPiece(ctx, CELL_SIZE) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
const viewportOriginX = Math.floor(ctx.canvas.width * 0.5);
const viewportOriginY = Math.floor(ctx.canvas.height * 0.5);
const [originX, originY] = findPieceOrigin(this._nextPiece, CELL_SIZE);
const dx = viewportOriginX - originX;
const dy = viewportOriginY - originY;
ctx.save();
ctx.translate(dx, dy);
ctx.lineWidth = 0.5;
ctx.fillStyle = COLORS[this._nextPiece.color - 1];
ctx.beginPath();
for (const coord of this._nextPiece.data) {
ctx.rect(coord.x * CELL_SIZE, coord.y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
ctx.fill();
ctx.stroke();
ctx.restore();
}
handleInput(event) {
if (this._gameOver) {
// There may be events in the pipeline that we receive even
// after the keyevent listener has been detached.
return;
}
switch (event.keyCode) {
case 37:
// Left
event.preventDefault();
if (this._checkMovement(this._droppingPiece, -1)) {
this._droppingPiece.position.x -= 1;
window.requestAnimationFrame(this.render);
}
break;
case 38:
// Up
event.preventDefault();
if (this._rotateAndCheck(this._droppingPiece, -1)) {
window.requestAnimationFrame(this.render);
}
break;
case 39:
// Right
event.preventDefault();
if (this._checkMovement(this._droppingPiece, 1)) {
this._droppingPiece.position.x += 1;
window.requestAnimationFrame(this.render);
}
break;
case 40:
// Down
event.preventDefault();
if (this._isPieceResting(this._droppingPiece)) {
this._commitAndSetupNewPiece();
} else {
this._droppingPiece.position.y += 1;
}
window.requestAnimationFrame(this.render);
this._scheduleTick();
break;
case 32:
// Space
event.preventDefault();
while (!this._isPieceResting(this._droppingPiece)) {
this._droppingPiece.position.y += 1;
}
this._commitAndSetupNewPiece();
window.requestAnimationFrame(this.render);
this._scheduleTick();
break;
}
}
}
function rotate(shape, dir) {
for (let i = 0; i < shape.data.length; i += 1) {
let coord = shape.data[i];
const x = (-dir) * coord.y;
const y = dir * coord.x;
coord.x = x;
coord.y = y;
}
}
function newPiece() {
const keys = Object.keys(SHAPES);
const shapeName = keys[Math.floor(Math.random() * keys.length)];
let shape = {
color: Math.floor(Math.random() * COLORS.length) + 1,
position: {x: Math.floor(GRID_WIDTH / 2), y: 0},
data: SHAPES[shapeName].map(coord => { return {x: coord.x, y: coord.y}; }),
};
const rotations = Math.floor(Math.random() * 4);
for (let i = 0; i < rotations; i += 1) {
rotate(shape, 1);
}
const [top, _right, _bottom, _left] = measurePiece(shape);
shape.position.y -= top;
return shape;
}
function measurePiece(piece) {
let left = 100;
let right = -100;
let top = 100;
let bottom = -100;
for (const coord of piece.data) {
left = Math.min(left, coord.x);
right = Math.max(right, coord.x);
top = Math.min(top, coord.y);
bottom = Math.max(bottom, coord.y);
}
return [top, right, bottom, left];
}
function findPieceOrigin(piece, CELL_SIZE) {
let [top, right, bottom, left] = measurePiece(piece);
top = top * CELL_SIZE;
right = (right + 1) * CELL_SIZE;
bottom = (bottom + 1) * CELL_SIZE;
left = left * CELL_SIZE;
const width = right - left;
const height = bottom - top;
const originX = Math.floor(width * 0.5) + left;
const originY = Math.floor(height * 0.5) + top;
return [originX, originY];
}
function setup() {
ctx = document.getElementById('canvas').getContext('2d');
nextCtx = document.getElementById('next').getContext('2d');
shadowCtx = document.getElementById('shadow').getContext('2d');
nextShadowCtx = document.getElementById('next-shadow').getContext('2d');
gameOverScreen = document.getElementById('game-over-screen');
document.getElementById('start-btn').addEventListener('click', () => {
const startScreen = document.getElementById('start-screen');
startScreen.classList.remove('enabled');
startScreen.classList.add('disabled');
game = new Game();
});
document.getElementById('toggle-theme').addEventListener('click', () => {
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const isDark = (prefersDark && !document.body.classList.contains('light')) || document.body.classList.contains('dark');
if (isDark) {
document.body.classList.remove('dark');
if (prefersDark) {
document.body.classList.add('light');
}
} else {
document.body.classList.remove('light');
if (!prefersDark) {
document.body.classList.add('dark');
}
}
});
}
function playAgain() {
game = new Game();
gameOverScreen.classList.remove('enabled');
gameOverScreen.classList.add('disabled');
window.removeEventListener('keydown', gameOverInputHandler);
document.getElementById('play-again-btn').removeEventListener('click', playAgain);
}
function gameOverInputHandler(event) {
// Shortcut: spacebar restarts the game.
if (event.keyCode === 32) {
event.preventDefault();
playAgain();
}
}
window.onload = setup;