-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
299 lines (195 loc) · 6.1 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
289
290
291
292
293
294
295
296
297
298
299
let canvas = document.getElementById('canvas')
let ctx = canvas.getContext('2d');
var tileset = new Image()
tileset.src = './components/tileset.png'
var index = 0
var tilesize = 30;
var tilerow = 1;
var tilecol = 4;
var maprow = 19;
var mapcol = 10;
var yView = 4
var playing = true
var score=0
var pos= {
x: 3,
y: 0
}
var shapeIndex = Math.floor(Math.random() * shape.length)
var index= Math.floor(Math.random() * shape[shapeIndex].length)
var piece = shape[shapeIndex][index]
var drawImage = function f() {
ctx.clearRect(0, 0, 300, 450)
for (var r = yView; r < maprow; r++) {
for (var c = 0; c < mapcol; c++) {
var tile = screen[10 * r + c]
var tileX = tile % tilecol
var tileY = Math.floor(tile / tilecol)
if (screen[r][c] !== 0) {
ctx.drawImage(tileset, tilesize, (0), tilesize, tilesize, (c * tilesize), ((r - yView) * tilesize), tilesize, tilesize)
} else {
ctx.drawImage(tileset, (0), (0), tilesize, tilesize, (c * tilesize), ((r - yView) * tilesize), tilesize, tilesize)
}
}
}
writeScore()
drawMatrix(piece, pos)
}
function drawMatrix(matrix, offset) {
for (var r = 0; r < matrix.length; r++) {
for (var c = 0; c < matrix[r].length; c++) {
if (matrix[r][c] !== 0) {
ctx.drawImage(tileset, tilesize, (0), tilesize, tilesize, (c * tilesize) + offset.x * tilesize, ((r - yView) * tilesize) + offset.y * tilesize, tilesize, tilesize)
}
}
}
}
setInterval(update, 1000)
function update() {
pos.y++
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height)
if (checkCollision(piece, screen, pos)) {
pos.y--
merge(piece, screen, pos);
pos.y = 0
pos.x = 3
shapeIndex = Math.floor(Math.random() * shape.length)
index = Math.floor(Math.random() * shape[shapeIndex].length)
piece = shape[shapeIndex][index]
}
drawImage()
drawMatrix(piece, pos)
var animate = requestAnimationFrame(draw)
if (gameOver()) {
cancelAnimationFrame(animate)
console.log("gamestop")
ctx.font = "40px Arial";
ctx.fillStyle = "white";
ctx.textAlign = "center";
ctx.fillText("Game Over", canvas.width/2, canvas.height/2);
}
}
function wallCollision(piece, screen, offset) {
for (var r = 0; r < piece.length; r++) {
for (var c = 0; c < piece[r].length; c++) {
if (piece[r][c] == 0) {
continue
}
console.table(piece)
console.log(screen[c + offset.x] === undefined, offset.x + c > mapcol - 1, piece[r][c] !== 0, piece[r][c] !== 0)
if ((screen[c + offset.x] === undefined || offset.x + c > mapcol - 1) || (piece[r][c] !== 0 && screen[r + offset.y][c + offset.x] !== 0)) {
console.log("collided")
return true
}
}
}
return false
}
function checkCollision(piece, screen, offset) {
for (var r = 0; r < piece.length; r++) {
for (var c = 0; c < piece[r].length; c++) {
if (piece[r][c] == 0) {
continue
}
if (piece[r][c] !== 0 && (screen[r + offset.y] && screen[r + offset.y][c + offset.x]) !== 0) {
return true
}
}
}
return false
}
function rotatePiece(piece, screen, offset) {
var temp = index
console.log(index, "-----")
index++
index %= 4
console.log(wallCollision(piece, screen, offset))
piece = shape[shapeIndex][index]
if (wallCollision(piece, screen, offset) || checkCollision(piece, screen, offset)) {
console.log("true")
index = temp
piece = shape[shapeIndex][index]
}
console.log(index, "-------------")
}
function merge(piece, screen, offset) {
// console.log("merging")
console.log(piece, screen, offset)
for (var r = 0; r < piece.length; r++) {
console.log(r)
for (var c = 0; c < piece[r].length; c++) {
if (piece[r][c] !== 0) {
screen[r + offset.y][c + offset.x] = piece[r][c];
}
}
}
// console.table(screen)
checkLines()
}
function checkLines() {
console.log("called")
for (var r = 0; r < screen.length; r++) {
// console.log(screen[r])
var flag = true
for (var c = 0; c < screen[r].length; c++) {
if (screen[r][c] != 1) {
flag = false
}
}
if (flag == true) {
screen.splice(r, 1)
screen.unshift([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
score++
console.log(score)
}
}
writeScore()
}
function writeScore(){
console.log("oinso")
var text=document.getElementById("score")
text.innerHTML=`Score : ${score}`
}
function gameOver() {
var flag = false
for (var i = 0; i < screen[yView - 1].length; i++) {
if (screen[yView - 1][i] == 1) {
playing = false
return true
}
}
return false
}
document.addEventListener('keydown', event => {
if (playing===true) {
if (event.keyCode === 37) {
pos.x--;
if (wallCollision(piece, screen, pos)) {
console.log("in")
pos.x++
}
} else if (event.keyCode === 39) {
pos.x++;
if (wallCollision(piece, screen, pos)) {
console.log("in")
pos.x--
}
} else if (event.keyCode === 40) {
pos.y++;
if (checkCollision(piece, screen, pos)) {
pos.y--
}
} else if (event.keyCode === 38) {
console.log(index)
rotatePiece(piece, screen, pos)
piece = shape[shapeIndex][index]
// piece=tempshape[index]
// console.log(index)
// console.table(piece)
}
}
});
tileset.onload = drawImage
draw()