Debian Codegraph @

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