Debian Codegraph @

website/games/columns.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>Columns</TITLE>
5 <link rel="stylesheet" href="../styles.css"></head>
6 <body onkeydown="keydown(event)" onblur="needPause()" onload="startGame()">
7 <!--<div class="sidenav" id="common"></div><div class="main">-->
8 <div class="topnav" id="myTopnav"></div><div class="main">
9
10 <h2>"Columns" game</h2>
11 <canvas id="myCanvas" width="700" height="550" onmousedown="mousedown(event)">
12 Your browser does not support the HTML5 canvas tag.</canvas></div>
13
14 <script type="text/javascript" src="../accordion.js"></script>
15 <script>
16 var ctx;
17 var bar = 25; // block size in px (default)
18 var nx = 10, ny = 20; // box size
19 var fX, fY; // figure position (lowest point)
20 var next = [];
21 var fig = [];
22 var cols = ['none', '#FF00FF', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF0000', 'silver'];
23 var dols = ['none', '#7F007F', '#007F00', '#00007F', '#7F7F00', '#007F7F', '#7F0000', 'gray'];
24 var box = [];
25
26 var level = 0;
27 var nlines = 0;
28 var score = 0;
29 var interval;
30 var hiscore = 0;
31 var paused = 0;
32 var finished = 0;
33 var marked = 0;
34 var preview = 0;
35
36 var step = 0.1; // step of down-shift (higher is more complex)
37
38 function needPause()
39 { if(paused==0) pause(); }
40
41 function pause() // useless
42 {
43 paused = 1-paused;
44 if(paused)
45 {
46 if(score > hiscore)
47 { hiscore = score;
48 localStorage.setItem("hiscoreHexris", hiscore); }
49 clearInterval(interval); draw();
50 }
51 else if(!finished) interval = setInterval(updateCanvas, 30);
52 }
53
54 function startGame()
55 {
56 ctx = document.getElementById("myCanvas").getContext("2d");
57 ctx.lineCap="round";
58
59 newFig(); newFig(); level = 0; step = 1./32;
60 score = 0; nlines = 0; paused = 0; finished = 0;
61 for(var i=0;i<nx*ny;i++) box[i] = 0;
62
63 hiscore = localStorage.getItem("hiscoreColumns");
64 if(!hiscore) hiscore = 0;
65 // document.getElementById("hiscore").innerHTML = hiscore;
66 interval = setInterval(updateCanvas, 30);
67 }
68
69 function newFig()
70 {
71 fig = next.slice();
72 next[0] = 1+Math.floor(Math.random()*(cols.length-2));
73 next[1] = 1+Math.floor(Math.random()*(cols.length-2));
74 next[2] = 1+Math.floor(Math.random()*(cols.length-2));
75 fX = Math.floor(nx/2); fY = ny-2;
76 }
77
78 function finish() // game over
79 {
80 finished = 1; draw();
81 ctx.font="40px Arial";
82 ctx.fillStyle = 'black';
83 ctx.fillText("GAME OVER", ctx.canvas.width/4, ctx.canvas.height/2);
84 if(score > hiscore)
85 { hiscore = score;
86 localStorage.setItem("hiscoreColumns", hiscore); }
87 // document.getElementById("hiscore").innerHTML = hiscore;
88 clearInterval(interval);
89 }
90
91 function moveLeft()
92 {
93 var y = Math.floor(fY);
94 if(fX>0 && box[fX-1+nx*y]==0) fX -= 1;
95 }
96
97 function moveRight()
98 {
99 var y = Math.floor(fY);
100 if(fX<nx-1 && box[fX+1+nx*y]==0) fX += 1;
101 }
102
103 function canFall()
104 {
105 var y = Math.floor(fY-1);
106 if(y<0 || box[fX+nx*y]>0) return 0;
107 return 1;
108 }
109
110 function putFig()
111 {
112 var y = Math.floor(fY);
113 box[fX+nx*y] = fig[0];
114 box[fX+nx*(y+1)] = fig[1];
115 box[fX+nx*(y+2)] = fig[2];
116 score += 1;
117 markLines();
118 }
119
120 function moveDown()
121 { if(canFall()) fY -= 1; }
122
123 function fallDown()
124 {
125 while(canFall()) fY -= 1;
126 putFig(); newFig();
127 }
128
129 function rotate()
130 {
131 var f = fig[0];
132 fig[0] = fig[1]; fig[1] = fig[2]; fig[2] = f;
133 }
134
135 function updateCanvas()
136 {
137 if(paused) { draw(); return; }
138 if(marked>0)
139 {
140 marked -= 1;
141 if(marked==0) removeLines();
142 }
143 else if(canFall())
144 fY -= step;
145 else
146 {
147 putFig();
148 newFig();
149 if(!canFall()) finish();
150 }
151 draw();
152 }
153
154 function markLines()
155 {
156 var rem = [];
157 for(var j=0;j<ny;j++) for(var i=0;i<nx;i++) rem[i+nx*j] = 0;
158 for(var j=1;j<ny-1;j++) for(var i=1;i<nx-1;i++) // main field
159 {
160 var i0 = i+nx*j, c = box[i0];
161 if(c>0)
162 {
163 if(box[i0-1]==c && box[i0+1]==c)
164 { rem[i0-1]=1; rem[i0]=1; rem[i0+1]=1; }
165 if(box[i0-nx]==c && box[i0+nx]==c)
166 { rem[i0-nx]=1; rem[i0]=1; rem[i0+nx]=1; }
167 if(box[i0-1-nx]==c && box[i0+1+nx]==c)
168 { rem[i0-1-nx]=1; rem[i0]=1; rem[i0+1+nx]=1; }
169 if(box[i0-1+nx]==c && box[i0+1-nx]==c)
170 { rem[i0-1+nx]=1; rem[i0]=1; rem[i0+1-nx]=1; }
171 }
172 }
173 for(var j=1;j<ny-1;j++) // vertical boundaries
174 {
175 var i0 = nx*j, i1 = nx-1+nx*j, c0 = box[i0], c1 = box[i1];
176 if(c0>0 && box[i0-nx]==c0 && box[i0+nx]==c0)
177 { rem[i0-nx]=1; rem[i0]=1; rem[i0+nx]=1; }
178 if(c1>0 && box[i1-nx]==c1 && box[i1+nx]==c1)
179 { rem[i1-nx]=1; rem[i1]=1; rem[i1+nx]=1; }
180 }
181 for(var i=1;i<nx-1;i++) // horizontal boundary
182 {
183 var c = box[i];
184 if(c>0 && box[i-1]==c && box[i+1]==c)
185 { rem[i-1]=1; rem[i]=1; rem[i+1]=1; }
186 }
187 var cr = cols.length-1;
188 for(var j=0;j<ny;j++) for(var i=0;i<nx;i++)
189 {
190 if(rem[i+nx*j]>0) { box[i+nx*j] = cr; marked = 5; }
191 }
192 }
193
194 function removeLines()
195 {
196 var ok=1, c = cols.length-1, rem = 0, items=0;
197 while(ok)
198 {
199 ok = 0;
200 for(var i=0;i<nx;i++) for(var j=0;j<ny;j++)
201 {
202 if(box[i+nx*j]==c)
203 {
204 ok = 1; items += 1;
205 for(var k=j+1;k<ny;k++) box[i+nx*(k-1)] = box[i+nx*k];
206 box[i+nx*(ny-1)] = 0;
207 }
208 }
209 }
210 if(preview)
211 score += items*items*4*level;
212 else
213 score += items*items*10*level;
214 nlines += items;
215 if(nlines>=30*level)
216 { level += 1; step = Math.pow(2,level/4-5);
217 if(level>15) level = 15; }
218 markLines(); // TODO summary score ?!!
219 }
220
221 function drawSq(i,j,col,col2)
222 {
223 cwidth = ctx.canvas.width;
224 cheight = ctx.canvas.height;
225 var s = cheight/22/bar, r = bar*s;
226 var x = r*(i+1), y = r*(ny-j);
227 ctx.beginPath();
228 if(col!='none')
229 {
230 var grd=ctx.createRadialGradient(x+0.5*r,y-r/3,r/5,x+0.5*r,y,r);
231 grd.addColorStop(0,"white");
232 grd.addColorStop(1,col);
233 ctx.fillStyle = grd;
234 }
235 ctx.rect(x,y,r,r); if(col!='none') ctx.fill();
236 ctx.strokeStyle = col2;
237 ctx.stroke();
238 }
239
240 function draw()
241 {
242 cwidth = ctx.canvas.width;
243 cheight = ctx.canvas.height;
244 var s = cheight/22/bar;
245 ctx.clearRect(0, 0, cwidth, cheight);
246 // boundary
247 for(var i=0;i<=ny;i++)
248 {
249 drawSq(-1,i,'gray','black');
250 drawSq(nx,i,'gray','black');
251 }
252 for(var i=0;i<=nx+1;i++) drawSq(i-1,-1,'gray','black');
253 // existing bricks
254 for(var i=0;i<nx;i++) for(var j=0;j<ny;j++)
255 {
256 var c = box[i+nx*j];
257 if(c) drawSq(i,j,cols[c],'black');
258 else drawSq(i,j,'none','lightgray');
259 }
260 // figure
261 var x = Math.floor(fX), y = Math.floor(fY);
262 drawSq(x,y,cols[fig[0]],'black');
263 drawSq(x,y+1,cols[fig[1]],'black');
264 drawSq(x,y+2,cols[fig[2]],'black');
265
266 // preview figure
267 if(preview)
268 {
269 drawSq(nx+7.5,10,cols[next[0]],'black');
270 drawSq(nx+7.5,11,cols[next[1]],'black');
271 drawSq(nx+7.5,12,cols[next[2]],'black');
272 ctx.strokeStyle = 'black';
273 ctx.strokeRect((nx+5)*bar*s,6*bar*s,8*bar*s,7*bar*s);
274
275 var x = Math.floor(fX), y = Math.floor(fY), dy;
276 for(dy=0;dy<=y;dy++) if(box[x+nx*(y-dy)]>0) break;
277 drawSq(x,y-dy+1,'none','black');
278 drawSq(x,y-dy+2,'none','black');
279 drawSq(x,y-dy+3,'none','black');
280 }
281
282
283 ctx.font="24px Arial"; // score
284 ctx.fillStyle = 'black';
285 ctx.fillText("Score: "+score, (nx+4)*s*bar, bar*s);
286 ctx.fillText("Level: "+level, (nx+11)*s*bar, s*bar);
287 ctx.fillStyle = 'darkgreen';
288 ctx.fillText("HiScore: "+hiscore, (nx+4)*s*bar, 2*bar*s);
289
290 var dh = s*100, b = dh/10;
291 var y0 = cheight-2*dh, x0 = s*bar*(nx+3);
292 ctx.strokeStyle = 'blue'; ctx.fillStyle = 'lightblue';
293
294 ctx.strokeRect(x0+b, y0+b, dh-2*b, dh-2*b); // left
295 ctx.beginPath();
296 ctx.moveTo(x0+dh/4,y0+dh/2);
297 ctx.lineTo(x0+3*dh/4,y0+3*dh/4);
298 ctx.lineTo(x0+3*dh/4,y0+dh/4);
299 ctx.lineTo(x0+dh/4,y0+dh/2); ctx.fill(); ctx.stroke();
300
301 ctx.strokeRect(x0+dh+b, y0+b, dh-2*b, dh-2*b); // rotate
302 ctx.beginPath();
303 ctx.arc(x0+3*dh/2, y0+dh/2, 0.3*dh, 0, 2*Math.PI);
304 ctx.fill(); ctx.stroke();
305 ctx.beginPath(); ctx.fillStyle = 'white';
306 ctx.arc(x0+3*dh/2, y0+dh/2, 0.2*dh, 0, 2*Math.PI);
307 ctx.fill(); ctx.stroke();
308
309 ctx.strokeRect(x0+2*dh+b, y0+b, dh-2*b, dh-2*b); // right
310 ctx.beginPath(); ctx.fillStyle = 'lightblue';
311 ctx.moveTo(x0+2*dh+3*dh/4,y0+dh/2);
312 ctx.lineTo(x0+2*dh+dh/4,y0+3*dh/4);
313 ctx.lineTo(x0+2*dh+dh/4,y0+dh/4);
314 ctx.lineTo(x0+2*dh+3*dh/4,y0+dh/2); ctx.fill(); ctx.stroke();
315
316 y0 += dh;
317 ctx.strokeStyle = 'green'; ctx.fillStyle = 'lightgreen';
318 /* ctx.strokeRect(x0+b, y0+b, dh-2*b, dh-2*b); // fall left
319 ctx.beginPath();
320 ctx.moveTo(x0+3*dh/4,y0+0.2*dh);
321 ctx.lineTo(x0+5*dh/8,y0+0.2*dh);
322 ctx.lineTo(x0+5*dh/8,y0+dh/2);
323 ctx.lineTo(x0+0.4*dh,y0+dh/2);
324 ctx.lineTo(x0+0.4*dh,y0+3*dh/8);
325 ctx.lineTo(x0+0.2*dh,y0+5*dh/8);
326 ctx.lineTo(x0+0.4*dh,y0+7*dh/8);
327 ctx.lineTo(x0+0.4*dh,y0+6*dh/8);
328 ctx.lineTo(x0+3*dh/4,y0+6*dh/8);
329 ctx.lineTo(x0+3*dh/4,y0+0.2*dh); ctx.fill(); ctx.stroke();*/
330
331 x0 += dh;
332 ctx.strokeRect(x0+b, y0+b, dh-2*b, dh-2*b); // fall down
333 ctx.beginPath();
334 ctx.moveTo(x0+dh/2,y0+3*dh/4);
335 ctx.lineTo(x0+3*dh/4,y0+dh/4);
336 ctx.lineTo(x0+dh/4,y0+dh/4);
337 ctx.lineTo(x0+dh/2,y0+3*dh/4); ctx.fill(); ctx.stroke();
338 ctx.beginPath();
339 ctx.rect(x0+dh/4,y0+3*dh/4,dh/2,dh/16);
340 ctx.fill(); ctx.stroke();
341
342 x0 += dh;
343 /* ctx.strokeRect(x0+b, y0+b, dh-2*b, dh-2*b); // fall right
344 ctx.beginPath();
345 ctx.moveTo(x0+dh/4,y0+0.2*dh);
346 ctx.lineTo(x0+3*dh/8,y0+0.2*dh);
347 ctx.lineTo(x0+3*dh/8,y0+dh/2);
348 ctx.lineTo(x0+0.6*dh,y0+dh/2);
349 ctx.lineTo(x0+0.6*dh,y0+3*dh/8);
350 ctx.lineTo(x0+0.8*dh,y0+5*dh/8);
351 ctx.lineTo(x0+0.6*dh,y0+7*dh/8);
352 ctx.lineTo(x0+0.6*dh,y0+6*dh/8);
353 ctx.lineTo(x0+dh/4,y0+6*dh/8);
354 ctx.lineTo(x0+dh/4,y0+0.2*dh); ctx.fill(); ctx.stroke();*/
355
356 x0 -= 2*dh; y0 -= 4*dh; // preview
357 ctx.strokeStyle = 'magenta'; ctx.fillStyle = 'violet';
358 if(preview)
359 {
360 ctx.fillStyle = 'lightpink';
361 ctx.fillRect(x0+b, y0+b, dh-2*b, dh-2*b);
362 }
363 ctx.strokeRect(b+x0, y0+b, dh-2*b, dh-2*b);
364 ctx.beginPath();
365 ctx.arc(x0+dh/2,y0+dh/4,dh/2,Math.PI/4,3*Math.PI/4);
366 ctx.stroke();
367 ctx.beginPath();
368 ctx.arc(x0+dh/2,y0+3*dh/4,dh/2,-3*Math.PI/4,-Math.PI/4);
369 ctx.stroke();
370 ctx.beginPath();
371 ctx.arc(x0+dh/2,y0+dh/2,dh/5,0,2*Math.PI);
372 ctx.fill(); ctx.stroke();
373
374 x0 += dh; ctx.strokeStyle = 'red';
375 if(paused)
376 {
377 ctx.fillStyle = 'pink';
378 ctx.fillRect(x0+b, y0+b, dh-2*b, dh-2*b);
379 }
380 ctx.strokeRect(b+x0, y0+b, dh-2*b, dh-2*b);
381 ctx.beginPath(); ctx.fillStyle = 'salmon';
382 ctx.rect(x0+dh/4,y0+dh/4,dh/8,dh/2);
383 ctx.rect(x0+5*dh/8,y0+dh/4,dh/8,dh/2); ctx.fill(); ctx.stroke();
384
385 x0 += dh;
386 ctx.strokeRect(b+x0, y0+b, dh-2*b, dh-2*b); // run
387 ctx.beginPath(); ctx.fillStyle = 'salmon';
388 ctx.moveTo(x0+dh/4,y0+dh/4);
389 ctx.lineTo(x0+dh/4,y0+3*dh/4);
390 ctx.lineTo(x0+dh/2,y0+dh/2); ctx.fill(); ctx.stroke();
391 ctx.moveTo(x0+dh/2,y0+dh/4);
392 ctx.lineTo(x0+dh/2,y0+3*dh/4);
393 ctx.lineTo(x0+3*dh/4,y0+dh/2); ctx.fill(); ctx.stroke();
394 }
395
396 function keydown(event) // key is pressed
397 {
398 if(event.key == ' ') fallDown();
399 else if(event.key == 'ArrowUp') rotate();
400 else if(event.key == 'ArrowDown') moveDown();
401 else if(event.key == 'ArrowLeft') moveLeft();
402 else if(event.key == 'ArrowRight') moveRight();
403 // else if(event.key == 'End') fallRight();
404 // else if(event.key == 'Home') fallLeft();
405 }
406
407 function mousedown(event) // mouse button is pressed
408 {
409 cwidth = ctx.canvas.width;
410 cheight = ctx.canvas.height;
411 var s = cheight/22/bar;
412 var dh = s*100, b = dh/10, y0 = cheight-2*dh;
413 var xx = event.offsetX-s*bar*(nx+3), yy = event.offsetY-y0;
414
415 if(xx<0) pause();
416 else if(yy>-0.5*dh && yy<dh) // main buttons
417 {
418 if(xx<dh) moveLeft();
419 else if(xx<2*dh) rotate();
420 else moveRight();
421 }
422 else if(yy>dh) // fall buttons
423 {
424 // if(xx<dh) fallLeft();
425 // else if(xx<2*dh)
426 fallDown();
427 // else fallRight();
428 }
429 else if(yy<-2*dh) // setup buttons
430 {
431 if(xx<dh) preview = 1-preview;
432 else if(xx<2*dh) pause();
433 else
434 { clearInterval(interval); startGame(); }
435 }
436 }
437 </script>
438 </body>
439 </html>