-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.js
390 lines (350 loc) · 8.64 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
const COLS = 10
const ROWS = 20
const CANVAS_WIDTH = 100
const CANVAS_HEIGHT = 200
const SQUARE_WIDTH = parseInt(CANVAS_WIDTH / COLS)
const SQUARE_HEIGHT = parseInt(CANVAS_HEIGHT / ROWS)
const COLORS = {
cyan: "#00ffff",
blue: "#0000ff",
orange: "#ff7f00",
yellow: "#ffff00",
green: "#00ff00",
purple: "#800080",
red: "#ff0000"
}
const PIECES = [
{
shape: [
[0, 0, 0, 0],
[1, 1, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
color: COLORS.cyan
},
{
shape: [
[1, 0, 0],
[1, 1, 1],
[0, 0, 0]
],
color: COLORS.blue
},
{
shape: [
[0, 0, 1],
[1, 1, 1],
[0, 0, 0]
],
color: COLORS.orange
},
{
shape: [
[1, 1],
[1, 1]
],
color: COLORS.yellow
},
{
shape: [
[0, 1, 1],
[1, 1, 0],
[0, 0, 0]
],
color: COLORS.green
},
{
shape: [
[0, 1, 0],
[1, 1, 1],
[0, 0, 0]
],
color: COLORS.purple
},
{
shape: [
[1, 1, 0],
[0, 1, 1],
[0, 0, 0]
],
color: COLORS.red
}
]
let gameIsOver = false
let score = 0
let scoreP = document.getElementById("score")
let levelP = document.getElementById("level")
let delayP = document.getElementById("delay")
let totalLinesCompleted = 0
let level = 1
let dropDelay = 500
let currentPiece = {
piece: null,
pos: {
row: null,
col: null
}
}
let board = []
for(let r = 0; r < ROWS; ++r) {
board[r] = []
for(let c = 0; c < COLS; ++c) {
board[r][c] = 0
}
}
createNewPiece()
function timer() {
drop()
setTimeout(timer, dropDelay)
}
setTimeout(timer, dropDelay)
document.addEventListener('keydown', keyPressed)
function keyPressed(e) {
switch(e.code) {
case "KeyA":
case "ArrowLeft":
moveLeft()
break
case "KeyD":
case "ArrowRight":
moveRight()
break
case "KeyS":
case "ArrowDown":
drop()
break
case "KeyQ":
case "ArrowUp":
rotateCCW()
break
case "KeyE":
rotateCW()
break
case "Space":
fastDrop()
break
}
}
const mtranspose = m => m[0].map((val, colIndex) => m.map(row => row[colIndex]))
const mrotateCCW = m => mtranspose(m).reverse()
const mrotateCW = m => mtranspose(m.reverse())
function getRandRange(min, max) { return Math.floor(Math.random() * (max - min + 1) + min) }
function isInBoard(col, row) {
if(col >= 0 && col < COLS && row >= 0 && row < ROWS) {
return true
} else {
return false
}
}
function addSquareWithBoundaryCheck(col, row) {
if(isInBoard(col, row)) {
board[row][col] = currentPiece.piece.color
}
}
function addPieceToBoard() {
for(let r = 0; r < currentPiece.piece.shape.length; ++r) {
for(let c = 0; c < currentPiece.piece.shape[0].length; ++c) {
if(currentPiece.piece.shape[r][c]) {
addSquareWithBoundaryCheck(currentPiece.pos.col + c, currentPiece.pos.row + r)
}
}
}
}
function computeScore() {
if((totalLinesCompleted > 0) && (totalLinesCompleted <= 90)) {
level = Math.floor(1 + ((totalLinesCompleted - 1) / 10))
} else if( totalLinesCompleted > 90) {
level = 10
}
dropDelay = ((11 - level) * 50)
}
function gameOver() {
document.removeEventListener('keydown', keyPressed)
gameIsOver = true
}
function createNewPiece() {
let pieceIndex = getRandRange(0,PIECES.length - 1)
currentPiece.piece = JSON.parse(JSON.stringify(PIECES[pieceIndex]))
currentPiece.pos.col = Math.floor(COLS/2) - 1
currentPiece.pos.row = 0
if(pieceIndex === 0) {
currentPiece.pos.col = Math.floor(COLS/2) - 2
currentPiece.pos.row = -1
}
for(let r = 0; r < currentPiece.piece.shape.length; ++r) {
for(let c = 0; c < currentPiece.piece.shape[0].length; ++c) {
if(currentPiece.piece.shape[r][c]) {
if(board[currentPiece.pos.row + r][currentPiece.pos.col + c]) {
gameOver()
}
}
}
}
}
function moveLeft() {
for(let r = 0; r < currentPiece.piece.shape.length; ++r) {
for(let c = 0; c < currentPiece.piece.shape[0].length; ++c) {
if(currentPiece.piece.shape[r][c]) {
if(((currentPiece.pos.col + c) === 0) || board[currentPiece.pos.row + r][currentPiece.pos.col + c - 1]) {
return false
}
}
}
}
--currentPiece.pos.col
return true
}
function moveRight() {
for(let r = 0; r < currentPiece.piece.shape.length; ++r) {
for(let c = 0; c < currentPiece.piece.shape[0].length; ++c) {
if(currentPiece.piece.shape[r][c]) {
if(((currentPiece.pos.col + c) === COLS - 1) || board[currentPiece.pos.row + r][currentPiece.pos.col + c + 1]) {
return false
}
}
}
}
++currentPiece.pos.col
return true
}
function rotateCW() {
currentPiece.piece.shape = mrotateCW(currentPiece.piece.shape)
for(let r = 0; r < currentPiece.piece.shape.length; ++r) {
for(let c = 0; c < currentPiece.piece.shape[0].length; ++c) {
if(currentPiece.piece.shape[r][c]) {
if(!isInBoard(currentPiece.pos.col + c, currentPiece.pos.row + r) || board[currentPiece.pos.row + r][currentPiece.pos.col + c]) {
currentPiece.piece.shape = mrotateCCW(currentPiece.piece.shape)
return false
}
}
}
}
return true
}
function rotateCCW() {
currentPiece.piece.shape = mrotateCCW(currentPiece.piece.shape)
for(let r = 0; r < currentPiece.piece.shape.length; ++r) {
for(let c = 0; c < currentPiece.piece.shape[0].length; ++c) {
if(currentPiece.piece.shape[r][c]) {
if(!isInBoard(currentPiece.pos.col + c, currentPiece.pos.row + r) || board[currentPiece.pos.row + r][currentPiece.pos.col + c]) {
currentPiece.piece.shape = mrotateCW(currentPiece.piece.shape)
return false
}
}
}
}
return true
}
function fastDrop() {
while(drop()) {}
}
function drop() {
for(let r = 0; r < currentPiece.piece.shape.length; ++r) {
for(let c = 0; c < currentPiece.piece.shape[0].length; ++c) {
if(currentPiece.piece.shape[r][c]) {
if(((currentPiece.pos.row + r) === ROWS - 1) || board[currentPiece.pos.row + r + 1][currentPiece.pos.col + c]) {
addPieceToBoard()
clearRows()
createNewPiece()
return false
}
}
}
}
++currentPiece.pos.row
return true
}
// recursive, less efficient
/*
function clearRows(start = (ROWS-1)) {
for(let r = start; r >= 0; --r) {
if(!board[r].includes(0)) {
for(let i = r; i > 0; --i) {
board[i] = board[i-1]
}
board[0].fill(0)
clearRows(r)
break;
}
}
}
*/
function clearRows() {
let rowsToClear = [] // [19, 18, 17, 16]
for(let r = ROWS - 1; r >= 0; --r) {
if(!board[r].includes(0)) {
rowsToClear.push(r)
if(rowsToClear.length === 4) break
}
}
if(rowsToClear) {
for(let r = rowsToClear[0]; r > rowsToClear.length - 1; --r) {
board[r] = board[r - rowsToClear.length].slice()
}
for(let r = 0; r < rowsToClear.length; ++r) {
board[r].fill(0)
}
}
switch(rowsToClear.length) {
case 1:
score += 40
break
case 2:
score += 100
break
case 3:
score += 300
break
case 4:
score += 1200
break
}
totalLinesCompleted += rowsToClear.length
}
function init() {
window.requestAnimationFrame(draw)
}
const draw = () => {
var cv = document.getElementById('cv');
if (cv.getContext) {
var ctx = cv.getContext('2d');
const drawBoard = () => {
ctx.fillStyle = "black"
ctx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT)
for(let r = 0; r < ROWS; ++r) {
for(let c = 0; c < COLS; ++c) {
if(board[r][c]) {
ctx.fillStyle = board[r][c]
ctx.fillRect(c * SQUARE_WIDTH, r * SQUARE_HEIGHT, SQUARE_WIDTH, SQUARE_HEIGHT)
ctx.strokeStyle = "rgba(0,0,0,0.5)"
ctx.strokeRect(c * SQUARE_WIDTH, r * SQUARE_HEIGHT, SQUARE_WIDTH, SQUARE_HEIGHT)
}
}
}
}
const drawCurrentPiece = () => {
ctx.fillStyle = currentPiece.piece.color
for(let r = 0; r < currentPiece.piece.shape.length; ++r) {
for(let c = 0; c < currentPiece.piece.shape[0].length; ++c) {
if(currentPiece.piece.shape[r][c] && isInBoard(c + currentPiece.pos.col, r + currentPiece.pos.row)) {
ctx.fillRect((c+currentPiece.pos.col) * SQUARE_WIDTH, (r + currentPiece.pos.row) * SQUARE_HEIGHT, SQUARE_WIDTH, SQUARE_HEIGHT)
ctx.strokeStyle = "rgba(0,0,0,0.5)"
ctx.strokeRect((c+currentPiece.pos.col) * SQUARE_WIDTH, (r + currentPiece.pos.row) * SQUARE_HEIGHT, SQUARE_WIDTH, SQUARE_HEIGHT)
}
}
}
}
drawBoard()
drawCurrentPiece()
computeScore()
if(gameIsOver) {
scoreP.innerHTML = "Game over! Score: " + score
} else {
scoreP.innerHTML = "Score: " + score
levelP.innerHTML = "Level: " + level
delayP.innerHTML = "Delay: " + dropDelay + " ms"
}
if(!gameIsOver) window.requestAnimationFrame(draw)
}
}