-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtetris.js
371 lines (342 loc) · 8.78 KB
/
tetris.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
class Tetris {
constructor(imageX, imageY, template) {
this.imageY = imageY;
this.imageX = imageX;
this.template = template;
this.x = squareCountX / 2;
this.y = 0;
}
checkBottom() {
for (let i = 0; i < this.template.length; i++) {
for (let j = 0; j < this.template.length; j++) {
if (this.template[i][j] == 0) continue;
let realX = i + this.getTruncedPosition().x;
let realY = j + this.getTruncedPosition().y;
if (realY + 1 >= squareCountY) {
return false;
}
if (gameMap[realY + 1][realX].imageX != -1) {
return false;
}
}
}
return true;
}
getTruncedPosition() {
return { x: Math.trunc(this.x), y: Math.trunc(this.y) };
}
checkLeft() {
for (let i = 0; i < this.template.length; i++) {
for (let j = 0; j < this.template.length; j++) {
if (this.template[i][j] == 0) continue;
let realX = i + this.getTruncedPosition().x;
let realY = j + this.getTruncedPosition().y;
if (realX - 1 < 0) {
return false;
}
if (gameMap[realY][realX - 1].imageX != -1) return false;
}
}
return true;
}
checkRight() {
for (let i = 0; i < this.template.length; i++) {
for (let j = 0; j < this.template.length; j++) {
if (this.template[i][j] == 0) continue;
let realX = i + this.getTruncedPosition().x;
let realY = j + this.getTruncedPosition().y;
if (realX + 1 >= squareCountX) {
return false;
}
if (gameMap[realY][realX + 1].imageX != -1) return false;
}
}
return true;
}
moveRight() {
if (this.checkRight()) {
this.x += 1;
}
}
moveLeft() {
if (this.checkLeft()) {
this.x -= 1;
}
}
moveBottom() {
if (this.checkBottom()) {
this.y += 1;
score += 1;
}
}
changeRotation() {
let tempTemplate = [];
for (let i = 0; i < this.template.length; i++)
tempTemplate[i] = this.template[i].slice();
let n = this.template.length;
for (let layer = 0; layer < n / 2; layer++) {
let first = layer;
let last = n - 1 - layer;
for (let i = first; i < last; i++) {
let offset = i - first;
let top = this.template[first][i];
this.template[first][i] = this.template[i][last]; // top = right
this.template[i][last] = this.template[last][last - offset]; //right = bottom
this.template[last][last - offset] =
this.template[last - offset][first];
//bottom = left
this.template[last - offset][first] = top; // left = top
}
}
for (let i = 0; i < this.template.length; i++) {
for (let j = 0; j < this.template.length; j++) {
if (this.template[i][j] == 0) continue;
let realX = i + this.getTruncedPosition().x;
let realY = j + this.getTruncedPosition().y;
if (
realX < 0 ||
realX >= squareCountX ||
realY < 0 ||
realY >= squareCountY
) {
this.template = tempTemplate;
return false;
}
}
}
}
}
const imageSquareSize = 24;
const size = 40;
const framePerSecond = 24;
const gameSpeed = 5;
const canvas = document.getElementById("canvas");
const nextShapeCanvas = document.getElementById("nextShapeCanvas");
const scoreCanvas = document.getElementById("scoreCanvas");
const image = document.getElementById("image");
const ctx = canvas.getContext("2d");
const nctx = nextShapeCanvas.getContext("2d");
const sctx = scoreCanvas.getContext("2d");
const squareCountX = canvas.width / size;
const squareCountY = canvas.height / size;
const shapes = [
new Tetris(0, 120, [
[0, 1, 0],
[0, 1, 0],
[1, 1, 0],
]),
new Tetris(0, 96, [
[0, 0, 0],
[1, 1, 1],
[0, 1, 0],
]),
new Tetris(0, 72, [
[0, 1, 0],
[0, 1, 0],
[0, 1, 1],
]),
new Tetris(0, 48, [
[0, 0, 0],
[0, 1, 1],
[1, 1, 0],
]),
new Tetris(0, 24, [
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 0],
]),
new Tetris(0, 0, [
[1, 1],
[1, 1],
]),
new Tetris(0, 48, [
[0, 0, 0],
[1, 1, 0],
[0, 1, 1],
]),
];
let gameMap;
let gameOver;
let currentShape;
let nextShape;
let score;
let initialTwoDArr;
let whiteLineThickness = 4;
let gameLoop = () => {
setInterval(update, 1000 / gameSpeed);
setInterval(draw, 1000 / framePerSecond);
};
let deleteCompleteRows = () => {
for (let i = 0; i < gameMap.length; i++) {
let t = gameMap[i];
let isComplete = true;
for (let j = 0; j < t.length; j++) {
if (t[j].imageX == -1) isComplete = false;
}
if (isComplete) {
console.log("complete row");
score += 1000;
for (let k = i; k > 0; k--) {
gameMap[k] = gameMap[k - 1];
}
let temp = [];
for (let j = 0; j < squareCountX; j++) {
temp.push({ imageX: -1, imageY: -1 });
}
gameMap[0] = temp;
}
}
};
let update = () => {
if (gameOver) return;
if (currentShape.checkBottom()) {
currentShape.y += 1;
} else {
for (let k = 0; k < currentShape.template.length; k++) {
for (let l = 0; l < currentShape.template.length; l++) {
if (currentShape.template[k][l] == 0) continue;
gameMap[currentShape.getTruncedPosition().y + l][
currentShape.getTruncedPosition().x + k
] = { imageX: currentShape.imageX, imageY: currentShape.imageY };
}
}
deleteCompleteRows();
currentShape = nextShape;
nextShape = getRandomShape();
if (!currentShape.checkBottom()) {
gameOver = true;
}
score += 100;
}
};
let drawRect = (x, y, width, height, color) => {
ctx.fillStyle = color;
ctx.fillRect(x, y, width, height);
};
let drawBackground = () => {
drawRect(0, 0, canvas.width, canvas.height, "#bca0dc");
for (let i = 0; i < squareCountX + 1; i++) {
drawRect(
size * i - whiteLineThickness,
0,
whiteLineThickness,
canvas.height,
"white"
);
}
for (let i = 0; i < squareCountY + 1; i++) {
drawRect(
0,
size * i - whiteLineThickness,
canvas.width,
whiteLineThickness,
"white"
);
}
};
let drawCurrentTetris = () => {
for (let i = 0; i < currentShape.template.length; i++) {
for (let j = 0; j < currentShape.template.length; j++) {
if (currentShape.template[i][j] == 0) continue;
ctx.drawImage(
image,
currentShape.imageX,
currentShape.imageY,
imageSquareSize,
imageSquareSize,
Math.trunc(currentShape.x) * size + size * i,
Math.trunc(currentShape.y) * size + size * j,
size,
size
);
}
}
};
let drawSquares = () => {
for (let i = 0; i < gameMap.length; i++) {
let t = gameMap[i];
for (let j = 0; j < t.length; j++) {
if (t[j].imageX == -1) continue;
ctx.drawImage(
image,
t[j].imageX,
t[j].imageY,
imageSquareSize,
imageSquareSize,
j * size,
i * size,
size,
size
);
}
}
};
let drawNextShape = () => {
nctx.fillStyle = "#bca0dc";
nctx.fillRect(0, 0, nextShapeCanvas.width, nextShapeCanvas.height);
for (let i = 0; i < nextShape.template.length; i++) {
for (let j = 0; j < nextShape.template.length; j++) {
if (nextShape.template[i][j] == 0) continue;
nctx.drawImage(
image,
nextShape.imageX,
nextShape.imageY,
imageSquareSize,
imageSquareSize,
size * i,
size * j + size,
size,
size
);
}
}
};
let drawScore = () => {
sctx.clearRect(0, 0, scoreCanvas.width, scoreCanvas.height);
sctx.font = "64px Poppins";
sctx.fillStyle = "black";
sctx.fillText(score, 10, 50);
};
let drawGameOver = () => {
ctx.font = "64px Poppins";
ctx.fillStyle = "black";
ctx.fillText("Game Over!", 10, canvas.height / 2);
};
let draw = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBackground();
drawSquares();
drawCurrentTetris();
drawNextShape();
drawScore();
if (gameOver) {
drawGameOver();
}
};
let getRandomShape = () => {
return Object.create(shapes[Math.floor(Math.random() * shapes.length)]);
};
let resetVars = () => {
initialTwoDArr = [];
for (let i = 0; i < squareCountY; i++) {
let temp = [];
for (let j = 0; j < squareCountX; j++) {
temp.push({ imageX: -1, imageY: -1 });
}
initialTwoDArr.push(temp);
}
score = 0;
gameOver = false;
currentShape = getRandomShape();
nextShape = getRandomShape();
gameMap = initialTwoDArr;
};
window.addEventListener("keydown", (event) => {
if (event.keyCode == 37) currentShape.moveLeft();
else if (event.keyCode == 38) currentShape.changeRotation();
else if (event.keyCode == 39) currentShape.moveRight();
else if (event.keyCode == 40) currentShape.moveBottom();
});
resetVars();
gameLoop();