[html] ; 俄罗斯方块 <br> body{幅:530px;背景:#F7F7F7; margin:20px auto} <br> table#gameBoard{ border:1px ソリッドブラック;境界崩壊: 崩壊; float:left;} <br> table#gameBoard td{ width:30px;高さ:30ピクセル; border: 1px 点線 #0cc;} <br> div#gameControl{ widows:160;フロート:右;高さ: 200ピクセル;; line-height: 200px;} <br> .font{ font-family:'微软雅黑';フォントサイズ:18px; text-align:center;} <br> div input { width: 60px;高さ:25ピクセル; } <br> <br> <br> <br> <br> var T = TETRIS = { <br> aBoardGrids : [], <br> aShapes: [ <br> [0xCC00], <br> 0 xF00]、 <br> [0x8C40, 0x6C00], <br> [0x4C80, 0xC600]、 <br> [0x44C0, 0x8E00] 、0xC880、0xE200]、 <br> [0x88C0、0xE800、0xC440、0x2E00]、 <br> [0x4E00、0x8C80、0xE400] <br> ], // 万方块の形状数 <br> init : function(){ <br> this.oDomBoard = document.getElementById("ゲームボード"); <br> this.oDomScore = document.getElementById("スコア"); <br> this.aBoardGrids = new Array(18); <br> for (var rows = 0 ; rows this.aBoardGrids[rows] = new Array(10); <br> var oDomTr = this.oDomBoard.insertRow(-1); <br> for (varcols = 0 ;cols this.aBoardGrids[rows][cols] = 0; <br> oDomTr.insertCell(cols); <br> } <br> } <br> <br> document.onkeydown = function(keyEvent){ <br> keyEvent = keyEvent ||ウィンドウ.イベント; <br> var ikeyNum = keyEvent.that ||キーイベント.キーコード; <br> switch(ikeyNum){ <br> case 37://← <br> T.oBlock.move("左"); <br> 休憩。 <br> case 38://↑ <br> T.oBlock.rotate((function (){ <br> var vShape = T.iShapeIdx][ (++T.index)%T.aShapes[T.iShapeIdx].length ]; <br> var sShape = vShape.toString(2); <br> sShape = new Array(17 - sShape.length).join(0) + sShape; <br> T.matrix = sShape.match(/d{4}/g) ; <br> T.matrix を返す; <br> })); //变形 <br> 休憩; <br> case 39://→ <br> T.oBlock.move("right"); <br> 休憩。 <br> case 40://↓ <br> T.oBlock.move("down"); <br> 休憩。 <br> } <br> } <br> }, <br> next : function (){ <br> <br> this.iShapeIdx = th.random() * this.aShapes.length); <br> this.index = 0; <br> var vShape = this.aShapes[this.iShapeIdx][this.index]; <br> var sShape = vShape.toString(2); //将16进制转换は二进制 <br> sShape = new Array(17 - sShape.length).join(0) + sShape; //不够16位,在前用0补全 <br>this.matrix = sshape.match(/d {4}/g); ; (){ // パネル <br> を更新します。 for(var i = 0; i var iLines = 0; If(this.aBoardGrids[j][ i] == 1) m num ++ ; if(num ==){var m = 0; m ++) ] j++; <br> } <br> } <br> iLine を返します。 <br> }, <br> setScore : function (iLines){ <br> var iScore = parseInt(this.oDomScore.innerHTML); <br> if(iLines == 1){ <br> iScore += 100; <br> } else if(iLines == 2){ <br> iScore += 300; <br> } else if(iLines == 3){ <br> iScore += 500; <br> } else if(iLines == 4){ <br> iScore += 1000; <br> } <br> this.oDomScore.innerHTML = iScore; <br> } <br> } <br> <br> TETRIS.Block = function (行列){ <br> <br> this.shape = (function(){ <br> <br> var aShape = [] ; <br> for(var i = 0 ; i var sValue = math[i]; <br> for(var j = 0 ; j if(sValue.charAt(j) == "1"){ <br> aShape.push( { x : j+3 , y : i }); <br> } <br> } <br> } <br> <br>ペ; <br> <br> })(); <br> this.draw(); <br> } <br> <br> TETRIS.Block.prototype.move = function (direction){//移動 <br> if(this.checkBlock(this.shape,direction)){ <br> this.draw("clear"); <br> for(var i = 0 ; i switch(方向){ <br> case "左"://← <br> this .shape[i].x--; <br> 休憩。 <br> 「正しい」ケース: <br> this.shape[i].x++; <br> 休憩。 <br> case "down": <br> this.shape[i].y++; <br> 休憩。 <br> } <br> } <br> this.draw(); <br> } else { <br> if(direction == "下"){ <br> this.draw(); <br> T.updateBoard(); //面板更新 <br> var iLines = T.eraseLines(); <br> <br> if(iLines > 0){ <br> <br> T.setScore(iLines); <br> <br> } <br> <br> T.next(); //再生成一个新的方块 <br> } <br> } <br> <br> } <br> TETRIS.Block.prototype.rotate = function (matrix){//变形 <br> <br> this.shape = (function(oBlock){ <br> <br> var aX = [ ]; <br> var aY = []; <br> <br> for(var i = 0 ; i aX.push(oBlock.shape[i].x); aY.push(oBlock.shape[ i].y); <br> } <br> var iMinX = aX.getMin(); <br> var iMinY = aY.getMin(); <br> <br> <br> var aShape = []; <br> for(var i = 0 ; i var sValue = 行列[i]; <br> for(var j = 0 ; j if(sValue.charAt(j) ){ <br> aShape.push({ x : j+iMinX , y : i+iMinY }); <br>}<br>}}}<br>if(!(oblock.checkblock(ashape)))<br>oblock.shapeを返します。 <br> oBlock.draw("clear"); <br> aShape を返します。 <br> <br> })(これ); <br> <br> this.draw(); <br> } <br> <br> TETRIS.Block.prototype.draw = function (opt){//绘图 <br> for(var i = 0 ; i < this.shape.length ; i++){ <br/> var oShape = this.shape[i] ; <br/> T.oDomBoard.rows[oShape.y].cells[oShape.x].style.background = (opt==unknown?"#09F":""); <br/> } <br/> } <br/> TETRIS.Block.prototype.checkBlock = 関数 (形状、方向){ <br/> 形状 = 形状 ||この形; <br/> for(var i = 0 ; i < 4 ; i++){ <br/> if(direction == "left"){ <br/> if(shape[i].x == 0 || T.aBoardGrids[shape[i]. y][shape[i].x - 1] == 1){ <br/> falseを返します。 <br/> } <br/> } else if(direction == "right"){ <br/> if(shape[i].x == 9 || T.aBoardGrids[shape[i].y][shape[i].x + 1 】 == 1){ <br/> falseを返します。 <br/> } <br/> } else if(direction == "down"){ <br/> if(shape[i].y == 17 || T.aBoardGrids[shape[i].y + 1][shape[i]. ×] ==1){ <br/> falseを返します。 <br/> } <br/> } <br/> if(shape[i].x < 0 || Shape[i].x > 9 || Shape[i].y 17) <br> false を返します。 <br> if(T.aBoardGrids[shape[i].y][shape[i].x] == 1){ <br> false を返す; <br> } <br> } <br> trueを返します。 <br> <br> } <br> Array.prototype.getMin = function (){ <br> var iMin = this[0]; <br> for(var i = 0 ; i if(this[i] < iMin) <br/> iMin = this[i]; <br/> } <br/> iMin を返します。 <br/> } <br/> window.onload = function(){ <br/> <br/> T.init(); <br/> <br/> var oBtnPlay = document.getElementById("btnPlay"); <br/> oBtnPlay.onclick = function(){ <br/> if(this.value == "begin"){ <br/> T.next(); <br/> this.value = "以上"; <br/> } else { <br/> this.value = "開始"; <br/> アラート("ゲームオーバー~"); <br/> clearInterval(T.timer); <br/> } <br/> <br/> } <br/> var oBtnPause = document.getElementById("btnPause"); <br/> oBtnPause.onclick = function (){ <br/> if(this.value == "一時停止"){ <br/> clearInterval(T.timer); <br/> this.value = "再開"; <br/> } else { <br/> T.timer = setInterval(function (){ <br/> T.oBlock.move("down"); <br/> }, 1000); <br/> this.value = "一時停止"; <br/> } <br/> } <br/> <br/> } <br/> <br/> 頭> スコア : 0 俄罗斯方块