-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
255 lines (231 loc) · 7.96 KB
/
app.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
document.addEventListener('DOMContentLoaded', () => {
const grid = document.querySelector('.grid')
let squares = Array.from(document.querySelectorAll('.grid div'))
const scoreDisplay = document.querySelector('#score')
const linesDisplay = document.querySelector('#lines_count')
const startBtn = document.querySelector('#start-button')
const width = 10
let score = 0
let lines_count = 0
let timeID
const colors = [
'firebrick',
'yellowgreen',
'coral',
'seagreen',
'orange',
'teal',
'blueviolet'
]
// 7 tetrominoes of different shape along with their rotations
const rlt = [
[1, width+1, width*2+1, 2],
[width, width+1, width+2, width*2+2],
[1, width+1, width*2+1, width*2],
[width, width*2, width*2+1, width*2+2]
]
const lt = [
[0, 1, width+1, width*2+1],
[width*2, width*2+1, width*2+2, width+2],
[0, width, width*2, width*2+1],
[width, width+1, width+2, width*2]
]
const rzt = [
[width+1, width+2, width*2+1, width*2],
[0, width, width+1, width*2+1],
[width+1, width+2, width*2+1, width*2],
[0, width, width+1, width*2+1]
]
const zt = [
[width, width+1, width*2+1, width*2+2],
[1, width+1, width, width*2],
[width, width+1, width*2+1, width*2+2],
[1, width+1, width, width*2]
]
const tt = [
[1, width, width+1, width+2],
[1, width+1, width*2+1, width+2],
[width, width+1, width+2, width*2+1],
[1, width, width+1, width*2+1]
]
const ot = [
[0, 1, width, width+1],
[0, 1, width, width+1],
[0, 1, width, width+1],
[0, 1, width, width+1]
]
const it = [
[1, width+1, width*2+1, width*3+1],
[width, width+1, width+2, width+3],
[1, width+1, width*2+1, width*3+1],
[width, width+1, width+2, width+3]
]
const tetrominoes = [rlt, lt, rzt, zt, tt, ot, it]
let curPos = 0
let curRot = 0
let rndT = 0
let nextRnd = 0
let curT = 0
// randomly select a tetromino and put its first rotation in the initial position
function setNew() {
curPos = 4
rndT = nextRnd
nextRnd = Math.floor(Math.random()*tetrominoes.length)
curT = tetrominoes[rndT][curRot]
}
setNew()
// draw the tetromino
function draw() {
curT.forEach(index => {
squares[curPos + index].classList.add('tetromino')
squares[curPos + index].style.backgroundColor = colors[rndT]
})
}
// undraw the tetromino
function undraw() {
curT.forEach(index => {
squares[curPos + index].classList.remove('tetromino')
squares[curPos + index].style.backgroundColor = ''
})
}
// map functions to keycodes
function control(e) {
switch(e.keyCode) {
case 37:
moveLeft()
break
case 38:
rotate()
break
case 39:
moveRight()
break
case 40:
moveDown()
break
}
}
document.addEventListener('keydown', control)
// move down the tetromino
function moveDown() {
undraw()
curPos += width
draw()
freeze()
}
// freeze the tetromino as it reaches the bottom line
function freeze() {
if (curT.some(index => squares[curPos + index + width].classList.contains('taken'))) {
curT.forEach(index => squares[curPos + index].classList.add('taken'))
setNew()
draw()
displayShape()
addScore()
gameOver()
}
}
// move the tetromino left unless it is at the edge or there is an obstacle
function moveLeft() {
undraw()
const reachedLeftEdge = curT.some(index => (curPos + index) % width === 0)
if (!reachedLeftEdge) curPos -= 1
if (curT.some(index => squares[curPos + index].classList.contains('taken'))) {
// if the position has been taken by another figure, push the tetromino back
curPos += 1
}
draw()
}
// move the tetromino right unless it is at the edge or there is a blockage
function moveRight() {
undraw()
const reachedRightEdge = curT.some(index => (curPos + index) % width === width - 1)
if (!reachedRightEdge) curPos += 1
if (curT.some(index => squares[curPos + index].classList.contains('taken'))) {
// if the position has been taken by another figure, push the tetromino back
curPos -= 1
}
draw()
}
// rotate the tetromino
function rotate() {
undraw()
const prevRot = curRot
curRot ++
if (curRot === curT.length) curRot = 0
curT = tetrominoes[rndT][curRot]
const reachedLeftEdge = curT.some(index => (curPos + index) % width === 0)
const reachedRightEdge = curT.some(index => (curPos + index) % width === width - 1)
const overlap = curT.some(index => squares[curPos + index].classList.contains('taken'))
if (reachedLeftEdge && reachedRightEdge || overlap) {
curRot = prevRot
curT = tetrominoes[rndT][curRot]
}
draw()
}
// show an upcoming tetromino in the mini-grid window
const displaySquares = document.querySelectorAll('.mini-grid div')
const displayW = 5
const displayIndex = 0
// rlt, lt, rzt, zt, tt, ot, it
const nextTs = [
[displayW+1, displayW*2+1, displayW*2+2, displayW*2+3],
[displayW+3, displayW*2+1, displayW*2+2, displayW*2+3],
[displayW+2, displayW+3, displayW*2+1, displayW*2+2],
[displayW+1, displayW+2, displayW*2+2, displayW*2+3],
[displayW*2+1, displayW*2+2, displayW*2+3, displayW+2],
[displayW+1, displayW+2, displayW*2+1, displayW*2+2],
[2, displayW+2, displayW*2+2, displayW*3+2]
]
// display the shape of next tetromino in the mini-grid
function displayShape() {
displaySquares.forEach(square => {
square.classList.remove('tetromino')
square.style.backgroundColor = ''
})
nextTs[nextRnd].forEach(index => {
displaySquares[displayIndex + index].classList.add('tetromino')
displaySquares[displayIndex + index].style.backgroundColor = colors[nextRnd]
})
}
// add functionality to the start button
startBtn.addEventListener('click', () => {
if (timeID) {
// the pause option
clearInterval(timeID)
timeID = null
} else {
// the start option
draw()
// move the tetromino down every second
timeID = setInterval(moveDown, 1000)
nextRnd = Math.floor(Math.random()*tetrominoes.length)
displayShape()
}
})
// calculate the player's score
function addScore() {
for (let i = 0; i < 199; i += width) {
const row = Array.from(new Array(width), (a,b) => b + i)
if (row.every(index => squares[index].classList.contains('taken'))) {
score += 10
lines_count += 1
scoreDisplay.innerHTML = score
linesDisplay.innerHTML = lines_count
row.forEach(index => {
squares[index].classList.remove('taken', 'tetromino')
squares[index].style.backgroundColor = ''
})
const squaresRemoved = squares.splice(i, width)
squares = squaresRemoved.concat(squares)
squares.forEach(cell => grid.appendChild(cell))
}
}
}
// game over
function gameOver() {
if (curT.some(index => squares[curPos + index].classList.contains('taken'))) {
scoreDisplay.innerHTML = ' end... '
clearInterval(timeID)
}
}
})