Debian Codegraph @

website/games/shiftix.html

raw
1 <!DOCTYPE html>
2 <html><head><meta name="viewport" content="width=device-width, initial-scale=1.0">
3 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
4 <TITLE>Shiftix</TITLE>
5 <link rel="stylesheet" href="../styles.css"></head>
6 <body onkeydown="keydown(event)" onblur="needPause()" onload="startGame()">
7 <div class="topnav" id="myTopnav"></div><div class="main">
8
9 <h2>"Shiftix" game</h2>
10 <canvas id="myCanvas" width="700" height="550" onmousedown="mousedown(event)">
11 Your browser does not support the HTML5 canvas tag.</canvas></div>
12
13 <script type="text/javascript" src="../accordion.js"></script>
14 <script>
15 var ctx;
16 var bar = 25; // block size in px (default)
17 var nx = 10, ny = 20; // box size
18 var fX, fY; // figure position (lowest point)
19 var next = [];
20 var fig = [];
21 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13
22 var cols = ['none', '#FF0080', '#FF00FF', '#8000FF', '#8080FF', '#0080FF', '#00FFFF', '#00FF80', '#00FF00', '#80FF00', '#FFFF00', '#FF8000', '#FF0000', '#FF8080',
23 '#7F4040', '#7F0000', '#7F4000', '#7F7F00', '#407F00', '#007F00', '#007F40', '#007F7F', '#00407F', '#40407F', '#40007F', '#7F007F', '#7F0040', 'black', 'silver'];
24 var box = [];
25 var fp1 = [1, 1, 0,0, 0,0]; // line 1x1
26 var fp2 = [2,1, 0.5,0, 0,0,1, 1,0,1]; // line 2x1
27 var fp3 = [3,1, 0,0, -1,0,1, 0,0,1, 1,0,1]; // line 3x1
28 var fp4 = [3,4, 0,0, 0,1,1, 0,0,1, 1,0,1];
29 //var figs = [f1, f2, f3, f4, f5, f6, f7];
30 var figs = [fp1, fp2, fp3, fp4];
31
32 var level = 0;
33 var score = 0;
34 var nlines = 0;
35 var interval;
36 var hiscore = 0;
37 var paused = 0;
38 var finished = 0;
39 var marked = 0;
40 var preview = 0;
41
42 var step = 0.1; // step of down-shift (higher is more complex)
43
44 function needPause()
45 { if(paused==0) pause(); }
46
47 function pause() // useless
48 {
49 paused = 1-paused;
50 if(paused)
51 {
52 if(score > hiscore)
53 { hiscore = score;
54 localStorage.setItem("hiscoreHexris", hiscore); }
55 clearInterval(interval); draw();
56 }
57 else if(!finished) interval = setInterval(updateCanvas, 30);
58 }
59
60 function startGame()
61 {
62 ctx = document.getElementById("myCanvas").getContext("2d");
63 ctx.lineCap="round";
64
65 newFig(); newFig(); level = 0; step = 1./32;
66 score = 0; nlines = 0; paused = 0; finished = 0;
67 for(var i=0;i<nx*ny;i++) box[i] = 0;
68
69 hiscore = localStorage.getItem("hiscoreColumns");
70 if(!hiscore) hiscore = 0;
71 // document.getElementById("hiscore").innerHTML = hiscore;
72 interval = setInterval(updateCanvas, 30);
73 }
74
75 function newFig()
76 {
77 var i = Math.floor(Math.random()*figs.length);
78 fig = next.slice(); next = figs[i].slice();
79 fX = Math.floor(nx/2); fY = ny-1;
80 for(var j=0;j<next[0];j++)
81 next[6+3*j] = 1+Math.floor(Math.random()*5)
82 }
83
84 function finish() // game over
85 {
86 finished = 1; draw();
87 ctx.font="40px Arial";
88 ctx.fillStyle = 'black';
89 ctx.fillText("GAME OVER", ctx.canvas.width/4, ctx.canvas.height/2);
90 if(score > hiscore)
91 { hiscore = score;
92 localStorage.setItem("hiscoreColumns", hiscore); }
93 // document.getElementById("hiscore").innerHTML = hiscore;
94 clearInterval(interval);
95 }
96
97 function moveLeft()
98 {
99 var n = fig[0];
100 for(var i=0;i<n;i++)
101 {
102 var x = Math.floor(fX+fig[4+3*i]-1);
103 var y = Math.floor(fY+fig[5+3*i]);
104 if(x<0 || box[x+nx*y]>0) return;
105 }
106 fX -= 1;
107 }
108
109 function moveRight()
110 {
111 var n = fig[0];
112 for(var i=0;i<n;i++)
113 {
114 var x = Math.floor(fX+fig[4+3*i]+1);
115 var y = Math.floor(fY+fig[5+3*i]);
116 if(x>=nx || box[x+nx*y]>0) return;
117 }
118 fX += 1;
119 }
120
121 function canFall()
122 {
123 var n = fig[0];
124 for(var i=0;i<n;i++)
125 {
126 var x = Math.floor(fX+fig[4+3*i]);
127 var y = Math.floor(fY+fig[5+3*i]-1);
128 if(y<0 || box[x+nx*y]>0) return 0;
129 }
130 return 1;
131 }
132
133 function putFig()
134 {
135 var n = fig[0], ok = 1;
136 for(var i=0;i<n;i++)
137 {
138 var x = Math.floor(fX+fig[4+3*i]);
139 var y = Math.floor(fY+fig[5+3*i]);
140 if(x>=0 && y>=0 && x<nx && y<ny) box[x+nx*y] = fig[6+3*i];
141 }
142 while(ok) // remove empty blocks
143 {
144 ok = 0;
145 for(var i=0;i<nx;i++) for(var j=0;j<ny;j++)
146 {
147 if(box[i+nx*j]==0 && box[i+nx*j+nx]>0)
148 {
149 ok = 1;
150 for(var k=j+1;k<ny;k++) box[i+nx*(k-1)] = box[i+nx*k];
151 box[i+nx*(ny-1)] = 0;
152 }
153 }
154 }
155 score += 1;
156 markLines();
157 }
158
159 function moveDown()
160 {
161 if(canFall()) fY -= 1;
162 }
163
164 function fallDown()
165 {
166 while(canFall()) fY -= 1;
167 putFig(); newFig();
168 }
169
170 function fallLeft()
171 {
172 var n = fig[0], ok = 1;
173 while(canFall()) fY -= 1;
174 for(var i=0;i<n;i++)
175 {
176 var x = Math.floor(fX+fig[4+3*i]-1);
177 var y = Math.floor(fY+fig[5+3*i]);
178 if(x<0 || box[x+nx*y]>0) ok = 0;
179 }
180 if(ok) fX -= 1;
181 // putFig(); newFig();
182 }
183
184 function fallRight()
185 {
186 var n = fig[0], ok = 1;
187 while(canFall()) fY -= 1;
188 for(var i=0;i<n;i++)
189 {
190 var x = Math.floor(fX+fig[4+3*i]+1);
191 var y = Math.floor(fY+fig[5+3*i]);
192 if(x<0 || box[x+nx*y]>0) ok = 0;
193 }
194 if(ok) fX += 1;
195 // putFig(); newFig();
196 }
197
198 function rotate()
199 {
200 var f = fig.slice(), n = fig[0], dx=0;
201 for(var i=0;i<n;i++)
202 {
203 f[4+3*i] = fig[2]-fig[5+3*i]+fig[3];
204 f[5+3*i] = fig[4+3*i]-fig[2]+fig[3];
205 var x = Math.floor(fX+f[4+3*i]);
206 var y = Math.floor(fY+f[5+3*i]);
207 if(x<0 && x+dx<0) dx = -x;
208 if(x>=nx && x+dx>=nx) dx = nx-1-x;
209 }
210 for(var i=0;i<n;i++)
211 {
212 f[4+3*i] = fig[2]-fig[5+3*i]+fig[3];
213 f[5+3*i] = fig[4+3*i]-fig[2]+fig[3];
214 var x = Math.floor(fX+f[4+3*i]+dx);
215 var y = Math.floor(fY+f[5+3*i]);
216 if(box[x+nx*y]>0) return;
217 }
218 fX += dx; fig = f;
219 }
220
221 function updateCanvas()
222 {
223 if(paused) { draw(); return; }
224 if(marked>0)
225 {
226 marked -= 1;
227 if(marked==0) removeLines();
228 }
229 else if(canFall())
230 fY -= step;
231 else
232 {
233 putFig();
234 newFig();
235 if(!canFall()) finish();
236 }
237 draw();
238 }
239
240 function mark(i,j,c,lst)
241 {
242 var i0 = i+nx*j, cr = cols.length-1;
243 lst.push(i0);
244 if(i>0 && box[i0-1]==c) { box[i0-1]=cr; mark(i-1,j,c,lst); }
245 if(i<nx-1 && box[i0+1]==c) { box[i0+1]=cr; mark(i+1,j,c,lst); }
246 if(j>0 && box[i0-nx]==c) { box[i0-nx]=cr; mark(i,j-1,c,lst); }
247 if(j<ny-1 && box[i0+nx]==c) { box[i0+nx]=cr; mark(i,j+1,c,lst); }
248 }
249
250 function markLines()
251 {
252 var cr = cols.length-1;
253 var flst=[]; // all found lists
254 var clst=[]; // color for found lists
255 for(var j=0;j<ny;j++) for(var i=0;i<nx;i++)
256 {
257 var i0 = i+nx*j, c = box[i0], lst = [];
258 if(c>0 && c<cr)
259 mark(i,j,c,lst);
260 if(lst.length>1)
261 { box[i0]==cr; flst.push(lst); clst.push(c); }
262 }
263 for(var j=0;j<flst.length;j++)
264 {
265 var lst = flst[j], n = lst.length;
266 var x0 = -100, i0 = -1;
267 for(var i=0;i<n;i++)
268 {
269 var x = lst[i]%nx;
270 if(Math.abs(x-fX)<Math.abs(x0-fX)) { x0 = x; i0 = lst[i]; }
271 }
272 box[i0] = clst[j]-2+n; marked = 5;
273 nlines += n-1;
274 if(preview)
275 score += Math.floor(box[i0]*(n-1)*level/2+0.5);
276 else
277 score += box[i0]*(n-1)*level;
278 }
279 if(nlines>=100*level)
280 { level += 1; step = Math.pow(2,level/4-5);
281 if(level>15) level = 15; }
282 removeLines();
283 }
284
285 function removeLines()
286 {
287 var ok=1, cr = cols.length-1, rem = 0;
288 while(ok)
289 {
290 ok = 0;
291 for(var i=0;i<nx;i++) for(var j=0;j<ny;j++)
292 {
293 if(box[i+nx*j]==cr)
294 {
295 ok = 1; rem = 1;
296 for(var k=j+1;k<ny;k++) box[i+nx*(k-1)] = box[i+nx*k];
297 box[i+nx*(ny-1)] = 0;
298 }
299 }
300 }
301 if(rem) markLines();
302 }
303
304 function drawSq(i,j,col,val)
305 {
306 cwidth = ctx.canvas.width;
307 cheight = ctx.canvas.height;
308 var s = cheight/22/bar, r = bar*s;
309 var x = r*(i+1), y = r*(ny-j);
310 var cc = col!='none'?1:0;
311 ctx.beginPath();
312 if(cc)
313 {
314 var grd=ctx.createRadialGradient(x+0.5*r,y-r/3,r/5,x+0.5*r,y,r);
315 grd.addColorStop(0,"white");
316 grd.addColorStop(1,col);
317 ctx.fillStyle = grd;
318 }
319 ctx.rect(x,y,r,r); if(cc) ctx.fill();
320 ctx.strokeStyle = (cc || val!=0)?'black':'lightgray'; ctx.stroke();
321 if(cc)
322 {
323 ctx.font="18px Arial"; // score
324 ctx.fillStyle = val+1<cols.length/2?'black':'white';
325 if(val<10)
326 ctx.fillText(val, x+r/3,y+0.7*r);
327 else
328 ctx.fillText(val, x+r/12,y+0.7*r);
329 }
330 }
331
332 function draw()
333 {
334 cwidth = ctx.canvas.width;
335 cheight = ctx.canvas.height;
336 var s = cheight/22/bar;
337 ctx.clearRect(0, 0, cwidth, cheight);
338 // boundary
339 for(var i=0;i<=ny;i++)
340 {
341 drawSq(-1,i,'gray','');
342 drawSq(nx,i,'gray','');
343 }
344 for(var i=0;i<=nx+1;i++) drawSq(i-1,-1,'gray','');
345 // existing bricks
346 for(var i=0;i<nx;i++) for(var j=0;j<ny;j++)
347 {
348 var c = box[i+nx*j];
349 if(c) drawSq(i,j,cols[c],c);
350 else drawSq(i,j,'none',0);
351 }
352 // figure
353 var n = fig[0];
354 for(var i=0;i<n;i++)
355 {
356 var x = Math.floor(fX+fig[4+3*i]);
357 var y = Math.floor(fY+fig[5+3*i]);
358 drawSq(x,y, cols[fig[6+3*i]], fig[6+3*i]);
359 }
360
361 // preview figure
362 if(preview)
363 {
364 var n = next[0];
365 for(var i=0;i<n;i++)
366 {
367 var x = Math.floor(next[4+3*i]);
368 var y = Math.floor(next[5+3*i]);
369 drawSq(x+nx+7,y+11, cols[next[6+3*i]], next[6+3*i]);
370 }
371 ctx.strokeStyle = 'black';
372 ctx.strokeRect((nx+5)*bar*s,6*bar*s,8*bar*s,7*bar*s);
373
374 var dy = []; n = fig[0];
375 for(var i=0;i<nx;i++) dy[i]=ny;
376 for(var i=0;i<n;i++)
377 {
378 var x = Math.floor(fX+fig[4+3*i]), y = Math.floor(fY+fig[5+3*i]), j;
379 for(j=0;j<=y;j++) if(box[x+nx*(y-j)]>0) break;
380 if(j<dy[x]) dy[x] = j;
381 }
382 for(var i=0;i<n;i++)
383 {
384 var x = Math.floor(fX+fig[4+3*i]), y = Math.floor(fY+fig[5+3*i]);
385 drawSq(x,y-dy[x]+1,'none',-1);
386 }
387 }
388
389
390 ctx.font="24px Arial"; // score
391 ctx.fillStyle = 'black';
392 ctx.fillText("Score: "+score, (nx+4)*s*bar, bar*s);
393 ctx.fillText("Level: "+level, (nx+11)*s*bar, s*bar);
394 ctx.fillStyle = 'darkgreen';
395 ctx.fillText("HiScore: "+hiscore, (nx+4)*s*bar, 2*bar*s);
396
397 var dh = s*100, b = dh/10;
398 var y0 = cheight-2*dh, x0 = s*bar*(nx+3);
399 ctx.strokeStyle = 'blue'; ctx.fillStyle = 'lightblue';
400
401 ctx.strokeRect(x0+b, y0+b, dh-2*b, dh-2*b); // left
402 ctx.beginPath();
403 ctx.moveTo(x0+dh/4,y0+dh/2);
404 ctx.lineTo(x0+3*dh/4,y0+3*dh/4);
405 ctx.lineTo(x0+3*dh/4,y0+dh/4);
406 ctx.lineTo(x0+dh/4,y0+dh/2); ctx.fill(); ctx.stroke();
407
408 ctx.strokeRect(x0+dh+b, y0+b, dh-2*b, dh-2*b); // rotate
409 ctx.beginPath();
410 ctx.arc(x0+3*dh/2, y0+dh/2, 0.3*dh, 0, 2*Math.PI);
411 ctx.fill(); ctx.stroke();
412 ctx.beginPath(); ctx.fillStyle = 'white';
413 ctx.arc(x0+3*dh/2, y0+dh/2, 0.2*dh, 0, 2*Math.PI);
414 ctx.fill(); ctx.stroke();
415
416 ctx.strokeRect(x0+2*dh+b, y0+b, dh-2*b, dh-2*b); // right
417 ctx.beginPath(); ctx.fillStyle = 'lightblue';
418 ctx.moveTo(x0+2*dh+3*dh/4,y0+dh/2);
419 ctx.lineTo(x0+2*dh+dh/4,y0+3*dh/4);
420 ctx.lineTo(x0+2*dh+dh/4,y0+dh/4);
421 ctx.lineTo(x0+2*dh+3*dh/4,y0+dh/2); ctx.fill(); ctx.stroke();
422
423 y0 += dh;
424 ctx.strokeStyle = 'green'; ctx.fillStyle = 'lightgreen';
425 /* ctx.strokeRect(x0+b, y0+b, dh-2*b, dh-2*b); // fall left
426 ctx.beginPath();
427 ctx.moveTo(x0+3*dh/4,y0+0.2*dh);
428 ctx.lineTo(x0+5*dh/8,y0+0.2*dh);
429 ctx.lineTo(x0+5*dh/8,y0+dh/2);
430 ctx.lineTo(x0+0.4*dh,y0+dh/2);
431 ctx.lineTo(x0+0.4*dh,y0+3*dh/8);
432 ctx.lineTo(x0+0.2*dh,y0+5*dh/8);
433 ctx.lineTo(x0+0.4*dh,y0+7*dh/8);
434 ctx.lineTo(x0+0.4*dh,y0+6*dh/8);
435 ctx.lineTo(x0+3*dh/4,y0+6*dh/8);
436 ctx.lineTo(x0+3*dh/4,y0+0.2*dh); ctx.fill(); ctx.stroke();*/
437
438 x0 += dh;
439 ctx.strokeRect(x0+b, y0+b, dh-2*b, dh-2*b); // fall down
440 ctx.beginPath();
441 ctx.moveTo(x0+dh/2,y0+3*dh/4);
442 ctx.lineTo(x0+3*dh/4,y0+dh/4);
443 ctx.lineTo(x0+dh/4,y0+dh/4);
444 ctx.lineTo(x0+dh/2,y0+3*dh/4); ctx.fill(); ctx.stroke();
445 ctx.beginPath();
446 ctx.rect(x0+dh/4,y0+3*dh/4,dh/2,dh/16);
447 ctx.fill(); ctx.stroke();
448
449 x0 += dh;
450 /* ctx.strokeRect(x0+b, y0+b, dh-2*b, dh-2*b); // fall right
451 ctx.beginPath();
452 ctx.moveTo(x0+dh/4,y0+0.2*dh);
453 ctx.lineTo(x0+3*dh/8,y0+0.2*dh);
454 ctx.lineTo(x0+3*dh/8,y0+dh/2);
455 ctx.lineTo(x0+0.6*dh,y0+dh/2);
456 ctx.lineTo(x0+0.6*dh,y0+3*dh/8);
457 ctx.lineTo(x0+0.8*dh,y0+5*dh/8);
458 ctx.lineTo(x0+0.6*dh,y0+7*dh/8);
459 ctx.lineTo(x0+0.6*dh,y0+6*dh/8);
460 ctx.lineTo(x0+dh/4,y0+6*dh/8);
461 ctx.lineTo(x0+dh/4,y0+0.2*dh); ctx.fill(); ctx.stroke();*/
462
463 x0 -= 2*dh; y0 -= 4*dh; // preview
464 ctx.strokeStyle = 'magenta'; ctx.fillStyle = 'violet';
465 if(preview)
466 {
467 ctx.fillStyle = 'lightpink';
468 ctx.fillRect(x0+b, y0+b, dh-2*b, dh-2*b);
469 }
470 ctx.strokeRect(b+x0, y0+b, dh-2*b, dh-2*b);
471 ctx.beginPath();
472 ctx.arc(x0+dh/2,y0+dh/4,dh/2,Math.PI/4,3*Math.PI/4);
473 ctx.stroke();
474 ctx.beginPath();
475 ctx.arc(x0+dh/2,y0+3*dh/4,dh/2,-3*Math.PI/4,-Math.PI/4);
476 ctx.stroke();
477 ctx.beginPath();
478 ctx.arc(x0+dh/2,y0+dh/2,dh/5,0,2*Math.PI);
479 ctx.fill(); ctx.stroke();
480
481 x0 += dh; ctx.strokeStyle = 'red';
482 if(paused)
483 {
484 ctx.fillStyle = 'pink';
485 ctx.fillRect(x0+b, y0+b, dh-2*b, dh-2*b);
486 }
487 ctx.strokeRect(b+x0, y0+b, dh-2*b, dh-2*b);
488 ctx.beginPath(); ctx.fillStyle = 'salmon';
489 ctx.rect(x0+dh/4,y0+dh/4,dh/8,dh/2);
490 ctx.rect(x0+5*dh/8,y0+dh/4,dh/8,dh/2); ctx.fill(); ctx.stroke();
491
492 x0 += dh;
493 ctx.strokeRect(b+x0, y0+b, dh-2*b, dh-2*b); // run
494 ctx.beginPath(); ctx.fillStyle = 'salmon';
495 ctx.moveTo(x0+dh/4,y0+dh/4);
496 ctx.lineTo(x0+dh/4,y0+3*dh/4);
497 ctx.lineTo(x0+dh/2,y0+dh/2); ctx.fill(); ctx.stroke();
498 ctx.moveTo(x0+dh/2,y0+dh/4);
499 ctx.lineTo(x0+dh/2,y0+3*dh/4);
500 ctx.lineTo(x0+3*dh/4,y0+dh/2); ctx.fill(); ctx.stroke();
501 }
502
503 function keydown(event) // key is pressed
504 {
505 if(event.key == ' ') fallDown();
506 else if(event.key == 'ArrowUp') rotate();
507 else if(event.key == 'ArrowDown') moveDown();
508 else if(event.key == 'ArrowLeft') moveLeft();
509 else if(event.key == 'ArrowRight') moveRight();
510 // else if(event.key == 'End') fallRight();
511 // else if(event.key == 'Home') fallLeft();
512 }
513
514 function mousedown(event) // mouse button is pressed
515 {
516 cwidth = ctx.canvas.width;
517 cheight = ctx.canvas.height;
518 var s = cheight/22/bar;
519 var dh = s*100, b = dh/10, y0 = cheight-2*dh;
520 var xx = event.offsetX-s*bar*(nx+3), yy = event.offsetY-y0;
521
522 if(xx<0) pause();
523 else if(yy>-0.5*dh && yy<dh) // main buttons
524 {
525 if(xx<dh) moveLeft();
526 else if(xx<2*dh) rotate();
527 else if(xx>2*dh) moveRight();
528 }
529 else if(yy>dh) // fall buttons
530 {
531 // if(xx<dh) fallLeft();
532 // else if(xx<2*dh)
533 fallDown();
534 // else fallRight();
535 }
536 else if(yy<-2*dh) // setup buttons
537 {
538 if(xx<dh) preview = 1-preview;
539 else if(xx<2*dh) pause();
540 else
541 { clearInterval(interval); startGame(); }
542 }
543 }
544 </script>
545 </body>
546 </html>