forked from kubowania/Tetris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
268 lines (236 loc) · 8.39 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
256
257
258
259
260
261
262
263
264
265
266
267
268
document.addEventListener('DOMContentLoaded', () => {
// TODO: we can also get the grid size from user
const GRID_WIDTH = 10
const GRID_HEIGHT = 20
const GRID_SIZE = GRID_WIDTH * GRID_HEIGHT
// no need to type 200 divs :)
const grid = createGrid();
let squares = Array.from(grid.querySelectorAll('div'))
const startBtn = document.querySelector('.button')
const hamburgerBtn = document.querySelector('.toggler')
const menu = document.querySelector('.menu')
const span = document.getElementsByClassName('close')[0]
const scoreDisplay = document.querySelector('.score-display')
const linesDisplay = document.querySelector('.lines-score')
let currentIndex = 0
let currentRotation = 0
const width = 10
let score = 0
let lines = 0
let timerId
let nextRandom = 0
const colors = [
'url(images/blue_block.png)',
'url(images/pink_block.png)',
'url(images/purple_block.png)',
'url(images/peach_block.png)',
'url(images/yellow_block.png)'
]
function createGrid() {
// the main grid
let grid = document.querySelector(".grid")
for (let i = 0; i < GRID_SIZE; i++) {
let gridElement = document.createElement("div")
grid.appendChild(gridElement)
}
// set base of grid
for (let i = 0; i < GRID_WIDTH; i++) {
let gridElement = document.createElement("div")
gridElement.setAttribute("class", "block3")
grid.appendChild(gridElement)
}
let previousGrid = document.querySelector(".previous-grid")
// Since 16 is the max grid size in which all the Tetrominoes
// can fit in we create one here
for (let i = 0; i < 16; i++) {
let gridElement = document.createElement("div")
previousGrid.appendChild(gridElement);
}
return grid;
}
//assign functions to keycodes
function control(e) {
if (e.keyCode === 39)
moveright()
else if (e.keyCode === 38)
rotate()
else if (e.keyCode === 37)
moveleft()
else if (e.keyCode === 40)
moveDown()
}
// the classical behavior is to speed up the block if down button is kept pressed so doing that
document.addEventListener('keydown', control)
//The Tetrominoes
const lTetromino = [
[1, GRID_WIDTH + 1, GRID_WIDTH * 2 + 1, 2],
[GRID_WIDTH, GRID_WIDTH + 1, GRID_WIDTH + 2, GRID_WIDTH * 2 + 2],
[1, GRID_WIDTH + 1, GRID_WIDTH * 2 + 1, GRID_WIDTH * 2],
[GRID_WIDTH, GRID_WIDTH * 2, GRID_WIDTH * 2 + 1, GRID_WIDTH * 2 + 2]
]
const zTetromino = [
[0, GRID_WIDTH, GRID_WIDTH + 1, GRID_WIDTH * 2 + 1],
[GRID_WIDTH + 1, GRID_WIDTH + 2, GRID_WIDTH * 2, GRID_WIDTH * 2 + 1],
[0, GRID_WIDTH, GRID_WIDTH + 1, GRID_WIDTH * 2 + 1],
[GRID_WIDTH + 1, GRID_WIDTH + 2, GRID_WIDTH * 2, GRID_WIDTH * 2 + 1]
]
const tTetromino = [
[1, GRID_WIDTH, GRID_WIDTH + 1, GRID_WIDTH + 2],
[1, GRID_WIDTH + 1, GRID_WIDTH + 2, GRID_WIDTH * 2 + 1],
[GRID_WIDTH, GRID_WIDTH + 1, GRID_WIDTH + 2, GRID_WIDTH * 2 + 1],
[1, GRID_WIDTH, GRID_WIDTH + 1, GRID_WIDTH * 2 + 1]
]
const oTetromino = [
[0, 1, GRID_WIDTH, GRID_WIDTH + 1],
[0, 1, GRID_WIDTH, GRID_WIDTH + 1],
[0, 1, GRID_WIDTH, GRID_WIDTH + 1],
[0, 1, GRID_WIDTH, GRID_WIDTH + 1]
]
const iTetromino = [
[1, GRID_WIDTH + 1, GRID_WIDTH * 2 + 1, GRID_WIDTH * 3 + 1],
[GRID_WIDTH, GRID_WIDTH + 1, GRID_WIDTH + 2, GRID_WIDTH + 3],
[1, GRID_WIDTH + 1, GRID_WIDTH * 2 + 1, GRID_WIDTH * 3 + 1],
[GRID_WIDTH, GRID_WIDTH + 1, GRID_WIDTH + 2, GRID_WIDTH + 3]
]
const theTetrominoes = [lTetromino, zTetromino, tTetromino, oTetromino, iTetromino]
//Randomly Select Tetromino
let random = Math.floor(Math.random() * theTetrominoes.length)
let current = theTetrominoes[random][currentRotation]
//move the Tetromino moveDown
let currentPosition = 4
//draw the shape
function draw() {
current.forEach(index => {
squares[currentPosition + index].classList.add('block')
squares[currentPosition + index].style.backgroundImage = colors[random]
})
}
//undraw the shape
function undraw() {
current.forEach(index => {
squares[currentPosition + index].classList.remove('block')
squares[currentPosition + index].style.backgroundImage = 'none'
})
}
//move down on loop
function moveDown() {
undraw()
currentPosition = currentPosition += width
draw()
freeze()
}
startBtn.addEventListener('click', () => {
if (timerId) {
clearInterval(timerId)
timerId = null
} else {
draw()
timerId = setInterval(moveDown, 1000)
nextRandom = Math.floor(Math.random() * theTetrominoes.length)
displayShape()
}
})
//move left and prevent collisions with shapes moving left
function moveright() {
undraw()
const isAtRightEdge = current.some(index => (currentPosition + index) % width === width - 1)
if (!isAtRightEdge) currentPosition += 1
if (current.some(index => squares[currentPosition + index].classList.contains('block2'))) {
currentPosition -= 1
}
draw()
}
//move right and prevent collisions with shapes moving right
function moveleft() {
undraw()
const isAtLeftEdge = current.some(index => (currentPosition + index) % width === 0)
if (!isAtLeftEdge) currentPosition -= 1
if (current.some(index => squares[currentPosition + index].classList.contains('block2'))) {
currentPosition += 1
}
draw()
}
//freeze the shape
function freeze() {
// if block has settled
if (current.some(index => squares[currentPosition + index + width].classList.contains('block3') || squares[currentPosition + index + width].classList.contains('block2'))) {
// make it block2
current.forEach(index => squares[index + currentPosition].classList.add('block2'))
// start a new tetromino falling
random = nextRandom
nextRandom = Math.floor(Math.random() * theTetrominoes.length)
current = theTetrominoes[random][currentRotation]
currentPosition = 4
draw()
displayShape()
addScore()
gameOver()
}
}
freeze()
//Rotate the Tetromino
function rotate() {
undraw()
currentRotation++
if (currentRotation === current.length) {
currentRotation = 0
}
current = theTetrominoes[random][currentRotation]
draw()
}
//Game Over
function gameOver() {
if (current.some(index => squares[currentPosition + index].classList.contains('block2'))) {
scoreDisplay.innerHTML = 'end'
clearInterval(timerId)
}
}
//show previous tetromino in scoreDisplay
const displayWidth = 4
const displaySquares = document.querySelectorAll('.previous-grid div')
let displayIndex = 0
const smallTetrominoes = [
[1, displayWidth + 1, displayWidth * 2 + 1, 2], /* lTetromino */
[0, displayWidth, displayWidth + 1, displayWidth * 2 + 1], /* zTetromino */
[1, displayWidth, displayWidth + 1, displayWidth + 2], /* tTetromino */
[0, 1, displayWidth, displayWidth + 1], /* oTetromino */
[1, displayWidth + 1, displayWidth * 2 + 1, displayWidth * 3 + 1] /* iTetromino */
]
function displayShape() {
displaySquares.forEach(square => {
square.classList.remove('block')
square.style.backgroundImage = 'none'
})
smallTetrominoes[nextRandom].forEach(index => {
displaySquares[displayIndex + index].classList.add('block')
displaySquares[displayIndex + index].style.backgroundImage = colors[nextRandom]
})
}
//Add score
function addScore() {
for (currentIndex = 0; currentIndex < GRID_SIZE; currentIndex += GRID_WIDTH) {
const row = [currentIndex, currentIndex + 1, currentIndex + 2, currentIndex + 3, currentIndex + 4, currentIndex + 5, currentIndex + 6, currentIndex + 7, currentIndex + 8, currentIndex + 9]
if (row.every(index => squares[index].classList.contains('block2'))) {
score += 10
lines += 1
scoreDisplay.innerHTML = score
linesDisplay.innerHTML = lines
row.forEach(index => {
squares[index].style.backgroundImage = 'none'
squares[index].classList.remove('block2') || squares[index].classList.remove('block')
})
//splice array
const squaresRemoved = squares.splice(currentIndex, width)
squares = squaresRemoved.concat(squares)
squares.forEach(cell => grid.appendChild(cell))
}
}
}
//Styling eventListeners
hamburgerBtn.addEventListener('click', () => {
menu.style.display = 'flex'
})
span.addEventListener('click', () => {
menu.style.display = 'none'
})
})