-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
288 lines (253 loc) · 6.33 KB
/
script.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
let canvas = document.getElementsByClassName("game-cont")[0]
let start = document.getElementsByClassName("start-btn")[0]
let stop = document.getElementsByClassName("stop-btn")[0]
let restart = document.getElementsByClassName("restart-btn")[0]
let menu = document.getElementsByClassName("start")[0]
let highscore = document.getElementsByClassName("high")[0]
let scr = document.getElementsByClassName("points")[0]
//geting context of the canvas as we cant draw on the dom object
let grid = canvas.getContext("2d");
grid.scale(20, 20)
let high = [];
if (localStorage.getItem("high") !== null) {
high = localStorage.getItem("high")
highscore.innerHTML =localStorage.getItem("high") ;
}
let score = 0;
//creting a tetris piece
const Attribute_Piece = [
[
[0,0,0],
[1,1,1],
[0,1,0]
],
[
[2,2],
[2,2],
],
[
[0,3,0,0],
[0,3,0,0],
[0,3,0,0],
[0,3,0,0]
],
[
[0,4,4],
[4,4,0],
[0,0,0]
],
[
[5,5,0],
[0,5,5],
[0,0,0]
],
[
[0,6,0],
[0,6,0],
[6,6,0]
],
[
[0,7,0],
[0,7,0],
[0,7,7]
],
]
//when a line fills
const gameGridCheck = () => {
let count = 1;
outer: for (let i = gameGrid.length - 1; i >1 ; i--) {
for (let j = 0; j < gameGrid[i].length; j++) {
if (gameGrid[i][j] === 0) {
break outer;
}
}
// let row = gameGrid.splice(i, 1)[0].fill(0);
gameGrid.unshift(gameGrid.splice(i, 1)[0].fill(0))
i++;
score += 10 * count;
count*=2;
}
}
//collision detection funtion
const collisionDetect = (gameGrid,player) => {
let piece = player.piece
let position = player.pos
for (let i = 0; i < piece.length; i++) {
for (let j = 0; j < piece[i].length; j++) {
if (piece[i][j] !== 0 && (gameGrid[i + position.y] && gameGrid[i + position.y][j + position.x]) !== 0) {
return true;
}
}
}
return false;
}
//creating grid for pieces
const createGrid = (h, w) => {
let gameGrid = [];
while (h > 0) {
gameGrid.push(new Array(w).fill(0));
h--;
}
return gameGrid;
}
//drawing the piece with every animation request
let draw = () => {
//Repainting the canvas
grid.fillStyle="#202020"
grid.fillRect(0, 0, canvas.width, canvas.height);
//drawing the piece
drawPiece(player.piece, player.pos)
drawPiece(gameGrid, {x:0,y:0})
}
//colors for
const colors = [null,"#d7263d","#ffff00","#0c8e36","#4361ee","#c55df6","#80ffdb","#ffffff"]
//drawing the piece
const drawPiece = (Piece, pos) => {
Piece.forEach((row ,y) => {
row.forEach((col, x) => {
if (col !== 0) {
grid.fillStyle = colors[col];
grid.fillRect(x+pos.x, y+pos.y, 1,1);
}
})
})
}
//moving the piece every second down
let moveTime = 0;
let movePos = 0;
const pieceMover = (time = 0) => {
//diffrence of time elapsed
let diffTime = time - moveTime;
moveTime = time;
//total time elaped
movePos+=diffTime
if (movePos > 1000) {
dMove()
}
draw()
//calling animation frame for rerendering page
animation = requestAnimationFrame(pieceMover)
}
//left and right movement
const lrMove = (dir) => {
player.pos.x += dir;
if (collisionDetect(gameGrid, player)) {
player.pos.x -= dir;
}
}
//down movement
const dMove = () => {
player.pos.y++;
scr.innerHTML=score;
score++;
//setting up high score
if (score > localStorage.getItem("high")) {
localStorage.setItem("high", score);
highscore.innerHTML =localStorage.getItem("high") ;
}
//when moving down collision check
if (collisionDetect(gameGrid, player)) {
player.pos.y--;
setPosition(player,gameGrid)
newPiece()
gameGridCheck()
}
movePos = 0;
}
//new piece
const newPiece = () => {
player.piece = Attribute_Piece[Math.floor(Math.random() * 7)];
player.pos.y = 1;
player.pos.x = Math.floor(gameGrid[0].length / 2) - Math.floor(player.piece[0].length / 2);
if (collisionDetect(gameGrid, player)) {
stopAnimation()
for (let i of gameGrid) {
i.fill(0);
}
score = -50;
// start.innerHTML="Restart"
// menu.style.display = "flex";
// exit(0)
}
}
//player attributes
const player = {
pos: {
x: 7,
y: 0,
},
piece:Attribute_Piece[0],
}
//rotate funtion
const rotate = (dir, piece) => {
let rp = piece;
for (let i = 0; i < piece.length; i++){
for (let j = 0; j < i; j++) {
let temp = piece[i][j];
piece[i][j] = piece[j][i];
piece[j][i] = temp;
}
}
if (dir !== 1) {
for (let i = 0; i < piece.length; i++) {
piece[i].reverse();
}
} else {
piece.reverse();
}
//if rotates out
let offset = 1;
while(collisionDetect(gameGrid, player)) {
player.pos.x += offset;
offset = -(offset + (offset > 0 ? 1 : -1));
if (offset>5) {
return;
}
}
}
//seting position of a piece
let gameGrid = createGrid(25, 15);
const setPosition = (player, gameGrid) => {
for (let i = 0; i < player.piece.length; i++){
for (let j = 0; j < player.piece[i].length; j++) {
if (player.piece[i][j] !== 0) {
gameGrid[i + player.pos.y][j + player.pos.x] = player.piece[i][j];
}
}
}
}
// menu.style.display="none";
document.addEventListener('keydown', (e) => {
// console.log(e);
if (e.keyCode === 37) {
lrMove(-1)
}else if (e.keyCode === 39) {
lrMove(1)
}else if (e.keyCode === 40) {
dMove()
} else if (e.keyCode === 82) {
rotate(1,player.piece)
}else if (e.keyCode === 69) {
rotate(0,player.piece)
}
})
//adding keyboard event when game starts
//start on click
start.addEventListener('click', (e) => {
pieceMover();
})
const stopAnimation = () => {
cancelAnimationFrame(animation)
cancelAnimationFrame(animation)
document.animation=false
}
stop.addEventListener('click',stopAnimation
)
restart.addEventListener('click', () => {
for (let i of gameGrid) {
i.fill(0)
}
score = 0;
pieceMover();
})
scr.innerHTML=score;