-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
297 lines (247 loc) · 7.28 KB
/
game.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
document.addEventListener('DOMContentLoaded', () => {
const board = document.querySelector('.board');
let blocks = Array.from(document.querySelectorAll('.board div'));
const displayScore = document.getElementById('score');
const pause = document.getElementById("pause-button");
let score = 0;
const width = 10;
let nextRandom = 0
// keys
const leftKey = document.getElementById("left");
const rightKey = document.getElementById("right");
const downKey = document.getElementById("down");
const rotateKey = document.getElementById("rotate");
// Tetrominos colors
const tColors = [
'green',
'blue',
'yellow',
'purple',
'red'
]
// Tetrominoes shapes
const I = [
[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 L = [
[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 O = [
[0,1,width,width+1],
[0,1,width,width+1],
[0,1,width,width+1],
[0,1,width,width+1]
]
const T = [
[1,width,width+1,width+2],
[1,width+1,width+2,width*2+1],
[width,width+1,width+2,width*2+1],
[1,width,width+1,width*2+1]
]
const Z = [
[0,width,width+1,width*2+1],
[width+1, width+2,width*2,width*2+1],
[0,width,width+1,width*2+1],
[width+1, width+2,width*2,width*2+1]
]
const tShapes = [I, L, O, T, Z]
// draw Tetromino
function draw(){
current.forEach((index) =>{
blocks[currentPosition + index].style.background = tColors[randomT]
})
}
// undraw Tetromino
function undraw() {
current.forEach((index) => {
blocks[currentPosition + index].style.background = ''
})
}
// move down
function moveDown(){
undraw()
currentPosition += width
draw()
freeze()
}
// move right
function moveRight() {
undraw()
const rightEdge = current.some(index => (currentPosition + index) % width === width -1)
const edge = current.some(index => blocks[currentPosition + index].classList.contains('taken'))
if(!rightEdge && !edge) {
currentPosition +=1;
}
draw()
}
// move left
function moveLeft() {
undraw()
const leftEdge = current.some(index => (currentPosition + index) % width === 0)
const edge = current.some(index => blocks[currentPosition + index].classList.contains('taken'))
if(!leftEdge && !edge) {
currentPosition -=1;
}
draw()
}
// rotate function
function rotate() {
undraw()
currentRotation ++
if(currentRotation === current.length) {
currentRotation = 0
}
current = tShapes[randomT][currentRotation]
checkRotatedPosition()
draw()
}
// keyCodes
function control(e) {
if (e.keyCode === 40) {
moveDown()
}else if (e.keyCode === 37 ) {
moveLeft()
} else if (e.keyCode === 39) {
moveRight()
} else if(e.keyCode === 38) {
rotate()
}
}
window.addEventListener("keydown", control);
// move keys manually
leftKey.addEventListener("click", moveLeft);
rightKey.addEventListener("click", moveRight);
downKey.addEventListener("click", moveDown);
rotateKey.addEventListener("click", rotate)
// edge rotations checking and fixing
function isAtRight() {
return current.some(index=> (currentPosition + index + 1) % width === 0)
}
function isAtLeft() {
return current.some(index=> (currentPosition + index) % width === 0)
}
function checkRotatedPosition(P){
P = P || currentPosition
if ((P+1) % width < 4) {
if (isAtRight()){
currentPosition += 1
checkRotatedPosition(P)
}
}
else if (P % width > 5) {
if (isAtLeft()){
currentPosition -= 1
checkRotatedPosition(P)
}
}
}
// next Tetrominos
const displayblocks = document.querySelectorAll('#mini-board div')
const displayWidth = 4
const displayIndex = 0
// Tetromino shapes without rotation
const upNextTetrominoes = [
[1, displayWidth+1, displayWidth*2+1, displayWidth*3+1],
[1, displayWidth+1, displayWidth*2+1, 2],
[0, 1, displayWidth, displayWidth+1],
[1, displayWidth, displayWidth+1, displayWidth+2],
[0, displayWidth, displayWidth+1, displayWidth*2+1]
]
// get next Tetromino
function displayShape() {
displayblocks.forEach(block => {
block.style.background = ''
})
upNextTetrominoes[nextRandom].forEach( index => {
displayblocks[displayIndex + index].style.background = tColors[nextRandom]
})
}
displayShape()
// set counter
var counter = setInterval(moveDown, 1000)
// get Tetromino
function getTetromino() {
randomT = nextRandom
nextRandom = Math.floor(Math.random() * tShapes.length)
currentRotation = 0
current = tShapes[randomT][currentRotation]
currentPosition = 4
draw()
displayShape()
gameOver()
getScore()
}
// freeze
function freeze() {
if(current.some(index => blocks[currentPosition + index + width].classList.contains('taken'))) {
current.forEach(index => blocks[currentPosition + index].classList.add('taken'))
getTetromino()
}
}
// pause / play button
function pauseGame(){
if(counter){
clearInterval(counter)
counter = null;
pause.innerHTML = "play"
}
else{
draw()
counter = setInterval(moveDown, 1000);
pause.innerHTML = "pause"
}
}
pause.addEventListener("click" , pauseGame)
// add score
function getScore() {
for (let i = 0; i < 199; i +=width) {
const row = [i, i+1, i+2, i+3, i+4, i+5, i+6, i+7, i+8, i+9]
if(row.every(index => blocks[index].classList.contains('taken'))) {
score +=10
displayScore.innerHTML = score
row.forEach(index => {
blocks[index].classList.remove('taken')
blocks[index].style.background = ''
})
const blocksRemoved = blocks.splice(i,width)
blocks = blocksRemoved.concat(blocks)
blocks.forEach(block => board.appendChild(block))
}
}
}
// game over and reload the page
function gameOver() {
if(current.some(index => blocks[currentPosition + index].classList.contains('taken'))) {
document.getElementById("game-over").style.display = "block";
setTimeout(function () {
window.location.reload();
}, 3000);
clearInterval(counter)
}
}
// start game screen
/// for mobile view
const start = document.getElementById("start-button");
start.addEventListener("click", startGame);
function startGame() {
document.getElementById("start-screen").style.display = "none";
document.getElementById("game").style.display = "block";
getTetromino()
};
/// for desktop and ipad view
function playGame(event) {
if (event.keyCode === 13) {
document.removeEventListener("keydown", playGame);
document.getElementById("start-screen").style.display = "none";
document.getElementById("game").style.display = "block";
getTetromino()
}
}
document.addEventListener("keydown", playGame)
})