1
1
var canvas = document . getElementById ( "game" )
2
2
var ctx = canvas . getContext ( "2d" )
3
+ ctx . globalAlpha = 1
3
4
var canvas2 = document . getElementById ( "next" )
4
5
var ctx2 = canvas2 . getContext ( "2d" )
5
6
var canvas3 = document . getElementById ( "store" )
6
7
var ctx3 = canvas3 . getContext ( "2d" )
7
8
var defurl = 'https://coderpro1234-2.github.io'
8
- var fx = 0
9
- var fy = 0
10
- var nxtt = 0
11
- var musicplay = 0
12
- var timer = 0
13
- var useswap = false
14
- var running = true
15
- var instout = false
16
- const gblocks = [
17
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
18
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
19
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
20
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
21
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
22
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
23
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
24
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
25
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
26
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
27
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
28
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
29
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
30
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
31
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
32
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
33
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
34
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
35
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
36
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
37
- ]
38
- fblock = [
39
- 0 , 0 , 0 , 0 ,
40
- 0 , 0 , 0 , 0 ,
41
- 0 , 0 , 0 , 0 ,
42
- 0 , 0 , 0 , 0
43
- ]
44
- sblock = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
45
9
const tetrominoes = [
46
10
[ 0 , 5 , 0 , 0 , 0 , 5 , 0 , 0 , 0 , 5 , 0 , 0 , 0 , 5 , 0 , 0 ] ,
47
11
[ 0 , 0 , 0 , 0 , 0 , 3 , 3 , 0 , 0 , 3 , 3 , 0 , 0 , 0 , 0 , 0 ] ,
@@ -51,10 +15,63 @@ const tetrominoes = [
51
15
[ 0 , 0 , 0 , 0 , 0 , 0 , 6 , 0 , 0 , 0 , 6 , 0 , 0 , 6 , 6 , 0 ] ,
52
16
[ 0 , 0 , 0 , 0 , 7 , 7 , 7 , 0 , 0 , 7 , 0 , 0 , 0 , 0 , 0 , 0 ]
53
17
]
18
+ var fx = 0
19
+ var fy = 0
20
+ var nxtt = 0
21
+ var musicplay = 0
22
+ var timer = 0
23
+ var lines = 0
24
+ var score = 0
25
+ var useswap = true
26
+ var running = true
27
+ var instout = true
28
+ gblocks = [ ]
29
+ fblock = [ ]
30
+ sblock = [ ]
31
+ nxtt = 0
32
+ function reset_vars ( ) {
33
+ fx = 0
34
+ fy = 0
35
+ nxtt = 0
36
+ lines = 0
37
+ score = 0
38
+ useswap = false
39
+ running = true
40
+ instout = false
41
+ gblocks = [
42
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
43
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0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+ nxtt = randnumber ( 0 , 6 )
71
+ }
54
72
function randnumber ( min , max ) {
55
73
return Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ;
56
74
}
57
- nxtt = randnumber ( 0 , 6 )
58
75
function checkmusic ( ) {
59
76
if ( musicplay == 0 ) {
60
77
tetrismusic . play ( )
@@ -143,6 +160,22 @@ function draw_store() {
143
160
i ++
144
161
}
145
162
}
163
+ function draw_ghost ( ) {
164
+ o_fy = fy
165
+ i = 0
166
+ while ( i != - 1 ) {
167
+ fy = fy + 1
168
+ if ( f_coll_g ( ) ) {
169
+ fy = fy - 1
170
+ i = - 2
171
+ }
172
+ i ++
173
+ }
174
+ ctx . globalAlpha = 0.5
175
+ draw_fblock ( )
176
+ ctx . globalAlpha = 1
177
+ fy = o_fy
178
+ }
146
179
function g_xy ( x , y ) {
147
180
if ( y > 19 || x < 0 || x > 9 ) {
148
181
return ( 1 )
@@ -198,25 +231,54 @@ function sro(off, color) {
198
231
function you_died ( ) {
199
232
c1 = 0
200
233
c2 = 0
201
- setInterval ( function ( ) {
202
- sro ( c1 , c2 + 1 )
203
- c1 += 1
204
- c2 += 1
205
- c2 = c2 % 7
206
- if ( c1 == 20 ) {
207
- c1 = 0
208
- c2 = 0
209
- setTimeout ( function ( ) {
210
- window . location . reload ( )
211
- set_f ( true )
212
- } , 400 )
234
+ const deathinter = setInterval ( function ( ) {
235
+ if ( running == false ) {
236
+ sro ( c1 , c2 + 1 )
237
+ c1 += 1
238
+ c2 += 1
239
+ c2 = c2 % 7
240
+ if ( c1 == 20 ) {
241
+ c1 = 0
242
+ c2 = 0
243
+ setTimeout ( function ( ) {
244
+ reset_vars ( )
245
+ running = true
246
+ gblocks = [
247
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
248
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
249
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
250
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
251
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
252
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
253
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
254
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
255
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
256
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
257
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
258
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
259
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
260
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
261
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
262
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
263
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
264
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
265
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
266
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
267
+ ]
268
+ set_f ( )
269
+ update_screen ( )
270
+ } , 400 )
271
+ }
272
+ }
273
+ else {
274
+ clearInterval ( deathinter )
275
+ return
213
276
}
214
- } , 100 )
215
-
277
+ } , 70 )
216
278
}
217
279
function swapinst ( ) {
218
280
instout = ! instout
219
- txt = "Instrunctions:<br>Up Arrow / W to snap block to bottom.<br>Left or Right Arrows / A or D to move block left or right.<br>Down Arrow / S to move block one tile down.<br> X to rotate block clockwise.<br>Z to rotate block counter-clockwise.<br>Q to save block to store if it is empty.<br>E to swap block from store if it exists ."
281
+ txt = "Instrunctions:<br>Up Arrow / W to snap block to bottom.<br>Left or Right Arrows / A or D to move block left or right.<br>Down Arrow / S to move block one tile down.<br> X to rotate block clockwise.<br>Z to rotate block counter-clockwise.<br>Q or E tp add or swap the block in the store ."
220
282
if ( instout ) {
221
283
document . getElementById ( "instructions" ) . innerHTML = txt
222
284
}
@@ -261,6 +323,7 @@ function set_f(t = false) {
261
323
}
262
324
}
263
325
function check_line ( ) {
326
+ lcc = 0
264
327
i = 0
265
328
while ( i < 20 ) {
266
329
line = 0
@@ -272,18 +335,35 @@ function check_line() {
272
335
i2 ++
273
336
}
274
337
if ( line == 10 ) {
338
+ lines ++
339
+ lcc ++
275
340
gblocks . splice ( i * 10 , 10 )
276
341
gblocks . unshift ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
277
342
}
278
343
i ++
279
344
}
345
+ if ( lcc == 1 ) {
346
+ score += 40
347
+ }
348
+ else if ( lcc == 2 ) {
349
+ score += 100
350
+ }
351
+ else if ( lcc == 3 ) {
352
+ score += 300
353
+ }
354
+ else if ( lcc == 4 ) {
355
+ score += 1200
356
+ }
280
357
}
281
358
function update_screen ( ) {
282
359
check_line ( )
283
360
draw_gblocks ( )
361
+ draw_ghost ( )
284
362
draw_fblock ( )
285
363
draw_next ( )
286
364
draw_store ( )
365
+ document . getElementById ( "lines" ) . innerHTML = ( lines . toString ( ) ) . padStart ( 6 , "0" )
366
+ document . getElementById ( "score" ) . innerHTML = ( score . toString ( ) ) . padStart ( 6 , "0" )
287
367
}
288
368
function mglt ( ) {
289
369
fy += 1
@@ -293,7 +373,29 @@ function mglt() {
293
373
set_f ( )
294
374
}
295
375
}
376
+ function check_swap ( ) {
377
+ i = 0
378
+ e = 0
379
+ while ( i < 16 ) {
380
+ if ( sblock [ i ] != 0 ) {
381
+ e += 1
382
+ }
383
+ i ++
384
+ }
385
+ if ( e == 0 ) {
386
+ sblock = fblock
387
+ set_f ( )
388
+ useswap = true
389
+ }
390
+ else if ( e != 0 && ( ! useswap ) ) {
391
+ tmp = sblock
392
+ sblock = fblock
393
+ fblock = tmp
394
+ useswap = true
395
+ }
396
+ }
296
397
window . onload = function ( ) {
398
+ reset_vars ( )
297
399
set_f ( )
298
400
update_screen ( )
299
401
setInterval ( function ( ) {
@@ -355,36 +457,8 @@ window.onload = function(){
355
457
r_c ( )
356
458
}
357
459
}
358
- if ( event . key == "q" ) {
359
- i = 0
360
- e = 0
361
- while ( i < 16 ) {
362
- if ( sblock [ i ] != 0 ) {
363
- e += 1
364
- }
365
- i ++
366
- }
367
- if ( e == 0 ) {
368
- sblock = fblock
369
- set_f ( )
370
- useswap = true
371
- }
372
- }
373
- if ( event . key == "e" ) {
374
- i = 0
375
- e = 0
376
- while ( i < 16 ) {
377
- if ( sblock [ i ] != 0 ) {
378
- e += 1
379
- }
380
- i ++
381
- }
382
- if ( e != 0 && ( ! useswap ) ) {
383
- tmp = sblock
384
- sblock = fblock
385
- fblock = tmp
386
- useswap = true
387
- }
460
+ if ( event . key == "q" || event . key == "e" ) {
461
+ check_swap ( )
388
462
}
389
463
update_screen ( )
390
464
}
0 commit comments