-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.js
324 lines (274 loc) · 7.46 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
const BOARD_ROWS = 15
const BOARD_COLUMNS = 10
const SIDEBOARD_ROWS = 4
const SIDEBOARD_COLUMNS = 5
const SQUARE = 40
const VACANT = 'white'
const BORDER = 'black'
const START_SPEED = 1000
const SPEED_DECREMENT = 100
const canvas = init(BOARD_ROWS, BOARD_COLUMNS, SQUARE)
const bodyElement = document.querySelector('body')
const scoreElement = document.querySelector('#score')
const levelElement = document.querySelector('#level')
const gameoverElement = document.querySelector('#gameover')
const guiElement=document.querySelector('#gui')
const newGameButton = gameoverElement.querySelector('button')
const ctx = canvas.getContext('2d')
document.addEventListener('keydown', handleInput)
newGameButton.addEventListener('click', handleNewGame)
function handleInput (event) {
if(!gameOver) {
if (event.keyCode == 37) {
p.moveLeft()
dropStart = Date.now()
} else if (event.keyCode == 38) {
p.rotate()
dropStart = Date.now()
} else if (event.keyCode == 39) {
p.moveRight()
dropStart = Date.now()
} else if (event.keyCode == 40) {
p.moveDown()
}
}
}
function handleNewGame () {
runTime = Date.now()
difficulty = START_SPEED
score = 0
gameOver = false
emptyBoard()
drawBoard()
drawSideBoard()
p = randomPiece()
nextPiece = randomPiece()
nextPiece.draw()
scoreElement.innerHTML = score
gameoverElement.classList.add('hide')
guiElement.classList.add('hide')
bodyElement.className = ''
drop()
}
const pieces = [
[Z, 'red'],
[S, 'yellow'],
[T, 'blue'],
[O, 'black'],
[L, 'orange'],
[I, 'green'],
[J, 'brown']
]
let nextPiece
let p
let difficulty
let runTime
let score = 0
let board = []
emptyBoard()
function emptyBoard () {
for (row = 0; row < BOARD_ROWS; row++) {
board[row] = []
for (col = 0; col < BOARD_COLUMNS; col++) {
board[row][col] = VACANT
}
}
}
function init (rows, cols, square) {
const canvas = document.querySelector('#tetris')
canvas.width = (cols * square) + (SIDEBOARD_COLUMNS + 2) * square
canvas.height = rows * square
return canvas
}
function adjustDifficulty (runTime) {
if (runTime <= 10000) { return START_SPEED }
if (runTime > 10000 && runTime <= 20000) { return START_SPEED - SPEED_DECREMENT }
else if (runTime > 20000 && runTime <= 30000) { return START_SPEED - 2*SPEED_DECREMENT }
else if (runTime > 30000 && runTime <= 40000) { return START_SPEED - 3*SPEED_DECREMENT }
else if (runTime > 40000 && runTime <= 50000) { return START_SPEED - 4*SPEED_DECREMENT }
else if (runTime > 50000 && runTime <= 60000) { return START_SPEED - 5*SPEED_DECREMENT }
else if (runTime > 60000 && runTime <= 70000) { return START_SPEED - 6*SPEED_DECREMENT }
else if (runTime > 70000 && runTime <= 80000) { return START_SPEED - 7*SPEED_DECREMENT }
else if (runTime > 80000 && runTime <= 100000) { return START_SPEED - 8*SPEED_DECREMENT }
else if (runTime > 100000 && runTime <= 110000) { return START_SPEED - 9*SPEED_DECREMENT }
else if (runTime >= 110000) { return difficulty }
}
function drawSquare (x, y, color) {
ctx.fillStyle = color
ctx.fillRect(x*SQUARE, y*SQUARE, SQUARE, SQUARE)
ctx.strokeStyle = BORDER
ctx.strokeRect(x*SQUARE, y*SQUARE, SQUARE, SQUARE)
}
function drawBoard () {
for (row = 0; row < BOARD_ROWS; row++) {
for (col = 0; col < BOARD_COLUMNS; col++) {
drawSquare(col, row, board[row][col])
}
}
}
function drawSideBoard () {
const offset = 2
for (row = 0; row < (SIDEBOARD_ROWS); row++) {
for (col = BOARD_COLUMNS+offset; col < (BOARD_COLUMNS+SIDEBOARD_COLUMNS+offset); col++) {
drawSquare(col, row, VACANT)
}
}
}
drawBoard()
drawSideBoard()
function randomPiece () {
let random = Math.floor(Math.random() * pieces.length)
return new Piece(pieces[random][0], pieces[random][1])
}
function Piece (shape, color) {
this.shape = shape
this.color = color
this.shapeIndex = 0
this.activeShape = this.shape[this.shapeIndex]
this.x = 13
this.y = 1
if (this.activeShape[0].length > 3) {
this.x = 12
this.y = 0
}
}
Piece.prototype.fill = function (color) {
for (row = 0; row < this.activeShape.length; row++) {
for (col = 0; col < this.activeShape.length; col++) {
if (this.activeShape[row][col]) {
drawSquare(this.x + col, this.y + row, color)
}
}
}
}
Piece.prototype.draw = function () {
this.fill(this.color)
}
Piece.prototype.clear = function () {
this.fill(VACANT)
}
Piece.prototype.moveDown = function () {
if (!this.collision(0, 1, this.activeShape)) {
this.clear()
this.y++
this.draw()
} else {
// lock piece and make new piece
this.lock()
p = nextPiece
p.x = 3
p.y = -2
if (!gameOver) {
nextPiece = randomPiece()
nextPiece.draw()
difficulty = adjustDifficulty(Date.now() - runTime)
const level = (START_SPEED - difficulty) / 100
levelElement.innerHTML = level
bodyElement.classList.add(`level-${level}`)
bodyElement.classList.remove(`level-${level-1}`)
}
}
}
Piece.prototype.moveRight = function () {
if (!this.collision(1, 0, this.activeShape)) {
this.clear()
this.x++
this.draw()
}
}
Piece.prototype.moveLeft = function () {
if (!this.collision(-1, 0, this.activeShape)) {
this.clear()
this.x--
this.draw()
}
}
Piece.prototype.rotate = function () {
let nextIndex = (this.shapeIndex + 1) %this.shape.length
let nextPattern = this.shape[nextIndex]
let kick = 0
if (this.collision(0, 0, nextPattern)) {
if (this.x > BOARD_COLUMNS / 2) {
kick = -1
} else {
kick = 1
}
}
if (!this.collision(kick, 0, nextPattern)) {
this.clear()
this.x += kick
this.shapeIndex = nextIndex
this.activeShape = this.shape[this.shapeIndex]
this.draw()
}
}
Piece.prototype.lock = function () {
for (row = 0; row < this.activeShape.length; row++) {
for (col = 0; col < this.activeShape.length; col++) {
if (!this.activeShape[row][col]) {
continue
}
if (this.y + row < 0) {
gameoverElement.classList.remove('hide')
guiElement.classList.remove('hide')
gameOver = true
break
}
board[this.y + row][this.x + col] = this.color
}
}
for (row = 0; row < BOARD_ROWS; row++) {
let isRowFull = true
for (col = 0; col < BOARD_COLUMNS; col++) {
isRowFull = isRowFull && (board[row][col] != VACANT)
}
if (isRowFull) {
for (y = row; y > 1; y--) {
for (col = 0; col < BOARD_COLUMNS; col++) {
board[y][col] = board[y-1][col]
}
}
for (col = 0; col < BOARD_COLUMNS; col++) {
board[0][col] = VACANT
}
score += 10
}
}
drawBoard()
drawSideBoard()
scoreElement.innerHTML = score
}
Piece.prototype.collision = function (x, y, piece) {
for (row = 0; row < piece.length; row++) {
for (col = 0; col < piece.length; col++) {
if (!piece[row][col]) {
continue
}
let futureX = this.x + col + x
let futureY = this.y + row + y
if (futureX < 0 || futureX >= BOARD_COLUMNS || futureY >= BOARD_ROWS) {
return true
}
if (futureY < 0) {
continue
}
if (board[futureY][futureX] != VACANT) {
return true
}
}
}
return false
}
let dropStart = Date.now()
let gameOver = true
function drop () {
let now = Date.now()
let delta = now - dropStart
if (delta > difficulty) {
p.moveDown()
dropStart = Date.now()
}
if (!gameOver) {
requestAnimationFrame(drop)
}
}