Skip to content

Commit 80c8afa

Browse files
committed
次ブロックの描画変更
1 parent 3164763 commit 80c8afa

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

games/tetris.html

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
</div>
4848
<div class="panel">
4949
<div class="label">NEXT</div>
50-
<canvas id="nextCanvas" width="96" height="96"></canvas>
50+
<canvas id="nextCanvas" width="136" height="136"></canvas>
5151
</div>
5252
<div class="panel">
5353
<button class="btn wide" id="btnRestart2">↻ Reset</button>
@@ -292,31 +292,47 @@
292292
this.drawPiece(this.activeG, this.cur, this.cur.y-2, 1.0, false, true);
293293
},
294294
drawPiece: function(g, piece, oy, alpha, dashed, glow){ for(var i=0;i<piece.pts.length;i++){ var p=piece.pts[i]; var x=piece.x+p[0], y=oy+p[1]; if(y<0) continue; this.drawCell(g, x, y, piece.c, alpha, dashed, glow); } },
295-
drawCell: function(g, x, y, color, alpha, dashed, glow){ if(alpha===undefined) alpha=1; if(dashed===undefined) dashed=false; if(glow===undefined) glow=false; var px=x*CELL, py=y*CELL; var c=color; g.fillStyle(Phaser.Display.Color.IntegerToColor(c).darken(25).color, alpha).fillRoundedRect(px+1,py+1,CELL-2,CELL-2,5); g.fillStyle(c, alpha).fillRoundedRect(px+3,py+3,CELL-6,CELL-6,4); g.lineStyle(2, 0xffffff, 0.35*alpha).strokeRoundedRect(px+3,py+3,CELL-6,CELL-6,4); if(glow){ g.lineStyle(3, c, 0.3*alpha).strokeRoundedRect(px+1,py+1,CELL-2,CELL-2,5); } if(dashed){ g.lineStyle(1, 0xffffff, 0.15).strokeRect(px+4,py+4,CELL-8,CELL-8); } },
295+
drawCell: function(g, x, y, color, alpha, dashed, glow, cellSize){
296+
if(alpha===undefined) alpha=1; if(dashed===undefined) dashed=false; if(glow===undefined) glow=false;
297+
var S = cellSize || CELL;
298+
var px=x*S, py=y*S; var c=color;
299+
g.fillStyle(Phaser.Display.Color.IntegerToColor(c).darken(25).color, alpha).fillRoundedRect(px+1,py+1,S-2,S-2,5);
300+
g.fillStyle(c, alpha).fillRoundedRect(px+3,py+3,S-6,S-6,4);
301+
g.lineStyle(2, 0xffffff, 0.35*alpha).strokeRoundedRect(px+3,py+3,S-6,S-6,4);
302+
if(glow){ g.lineStyle(3, c, 0.3*alpha).strokeRoundedRect(px+1,py+1,S-2,S-2,5); }
303+
if(dashed){ g.lineStyle(1, 0xffffff, 0.15).strokeRect(px+4,py+4,S-8,S-8); }
304+
},
296305
updateHUD: function(){
297306
document.getElementById('uiScore').textContent = this.score + ' / HI ' + Math.max(this.hiscore,this.score);
298307
document.getElementById('uiLevel').textContent = this.level;
299308
},
300309
drawNextPreview: function(piece){
301-
var cvs = document.getElementById('nextCanvas');
302-
if(!cvs) return;
303-
var ctx = cvs.getContext('2d');
304-
ctx.clearRect(0,0,cvs.width,cvs.height);
310+
var cvs = document.getElementById('nextCanvas'); if(!cvs) return;
311+
var ctx = cvs.getContext('2d'); ctx.clearRect(0,0,cvs.width,cvs.height);
305312
if(!piece) return;
306-
// ミノを中央寄せで描く(CELLの半分サイズでプレビュー)
307-
var s = Math.floor(CELL * 0.9);
308-
var off = Math.floor((cvs.width - 4*s)/2);
309-
ctx.fillStyle = '#fff';
310-
for(var i=0;i<piece.pts.length;i++){
311-
var p = piece.pts[i];
312-
var x = off + p[0]*s, y = off + p[1]*s;
313-
ctx.globalAlpha = 0.12;
314-
ctx.fillStyle = '#000';
315-
ctx.fillRect(x+2,y+2,s,s);
316-
ctx.globalAlpha = 1;
317-
ctx.fillStyle = '#'+('000000'+TETROS[piece.k].c.toString(16)).slice(-6);
318-
ctx.fillRect(x,y,s,s);
313+
// 1) ピースの外接矩形
314+
var minx=99,miny=99,maxx=-99,maxy=-99,i,p;
315+
for(i=0;i<piece.pts.length;i++){ p=piece.pts[i];
316+
if(p[0]<minx)minx=p[0]; if(p[0]>maxx)maxx=p[0];
317+
if(p[1]<miny)miny=p[1]; if(p[1]>maxy)maxy=p[1];
318+
}
319+
// 2) 表示セルサイズ(CELLに追従)と生成サイズ
320+
var S = Math.max(12, Math.floor(CELL * 0.9));
321+
var w = (maxx-minx+1)*S, h = (maxy-miny+1)*S;
322+
// 3) PhaserのGraphicsに「本体と同じ drawCell」で描く(オフスクリーン)
323+
var g = this.make.graphics({x:0,y:0, add:false});
324+
for(i=0;i<piece.pts.length;i++){ p=piece.pts[i];
325+
// 左上原点に揃えて描画(minx/minyで正規化)
326+
this.drawCell(g, p[0]-minx, p[1]-miny, TETROS[piece.k].c, 1.0, false, true, S);
319327
}
328+
var key='__next';
329+
g.generateTexture(key, w, h);
330+
var img = this.textures.get(key).getSourceImage();
331+
// 4) NEXTキャンバス中央に転送
332+
var dx = Math.floor((cvs.width - w)/2);
333+
var dy = Math.floor((cvs.height - h)/2);
334+
ctx.drawImage(img, dx, dy);
335+
this.textures.remove(key); g.destroy();
320336
},
321337
togglePause: function(){ this.paused=!this.paused; this.cameras.main.setAlpha(this.paused?0.6:1); },
322338
onGameOver: function(){ this.cameras.main.shake(220,0.01); this.cameras.main.flash(180,255,0,0); this.paused=true; if(this.score>this.hiscore){ setHi(this.score); this.hiscore=this.score; } this.add.text(this.scale.width/2, this.scale.height/2, 'GAME OVER\nPress Restart', {fontFamily:'monospace',fontSize:28,color:'#fff',align:'center'}).setOrigin(0.5); }

0 commit comments

Comments
 (0)