Debian Codegraph @

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