JavaScript, 實現俄羅斯方塊小遊戲

來源:互聯網
上載者:User
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>俄羅斯方塊</title><style type="text/css">body{ width:530px; background:#F7F7F7; margin:20px auto}table#gameBoard{ border:1px solid black; border-collapse: collapse; float:left;}table#gameBoard td{ width:30px; height:30px; border: 1px dotted #0cc;}div#gameControl{ widows:160; float:right; height: 200px;; line-height: 200px;}.font{ font-family:'微軟雅黑'; font-size:18px; text-align:center;}div input { width: 60px; height:25px; }</style><script type="text/javascript">var T = TETRIS = {aBoardGrids : [],aShapes: [[0xCC00], [0x8888, 0xF00], [0x8C40, 0x6C00], [0x4C80, 0xC600], [0x44C0, 0x8E00, 0xC880, 0xE200], [0x88C0, 0xE800, 0xC440, 0x2E00], [0x4E00, 0x8C80, 0xE400, 0x4C40]],//代表所有方塊的形狀數init : function(){this.oDomBoard = document.getElementById("gameBoard");this.oDomScore = document.getElementById("score");this.aBoardGrids = new Array(18);for (var rows = 0 ; rows < 18 ; rows++){this.aBoardGrids[rows] = new Array(10);var oDomTr = this.oDomBoard.insertRow(-1);for(var cols = 0 ; cols < 10 ; cols++){this.aBoardGrids[rows][cols] = 0;oDomTr.insertCell(cols);}}document.onkeydown = function(keyEvent){keyEvent = keyEvent || window.event;var ikeyNum = keyEvent.which || keyEvent.keyCode;switch(ikeyNum){case 37://←T.oBlock.move("left");break;case 38://↑T.oBlock.rotate((function (){var vShape = T.aShapes[T.iShapeIdx][ (++T.index)%T.aShapes[T.iShapeIdx].length];var sShape = vShape.toString(2);sShape = new Array(17 - sShape.length).join(0) + sShape;T.matrix= sShape.match(/\d{4}/g);return T.matrix;})());//變形break;case 39://→T.oBlock.move("right");break;case 40://↓T.oBlock.move("down");break;}}},next : function (){this.iShapeIdx = parseInt(Math.random() * this.aShapes.length);this.index = 0;var vShape = this.aShapes[this.iShapeIdx][this.index];var sShape = vShape.toString(2);//將16進位轉換為二進位sShape = new Array(17 - sShape.length).join(0) + sShape;//不夠16位,在前面用0補全 this.matrix= sShape.match(/\d{4}/g);//利用Regex匹配this.oBlock = new TETRIS.Block(this.matrix);clearInterval(T.timer);//註冊定時器T.timer = setInterval(function (){T.oBlock.move("down");}, 1000);if( !T.oBlock.checkBlock() ){alert("Game Over~");clearInterval(T.timer);}},updateBoard : function (){//更新面板for(var i = 0 ; i < 4 ; i++){this.aBoardGrids[T.oBlock.shape[i].y][T.oBlock.shape[i].x] = 1;}},eraseLines : function (){var iLines = 0;for(var j = 17 ; j >= 0 ; j--){var num = 0;for(var i = 0 ; i< 10 ; i++){if(this.aBoardGrids[j][i] == 1)num ++;}if(num == 10){iLines ++;for(var m = 0 ; m < i ; m++){for(var n = j ; n > 0 ; n--){this.aBoardGrids[n][m] = this.aBoardGrids[n-1][m];T.oDomBoard.rows[n].cells[m].style.background = T.oDomBoard.rows[n-1].cells[m].style.background;}this.aBoardGrids[0][m] = 0;}j++;}}return iLines;},setScore : function (iLines){var iScore = parseInt(this.oDomScore.innerHTML);if(iLines == 1){iScore += 100;} else if(iLines == 2){iScore += 300;} else if(iLines == 3){iScore += 500;} else if(iLines == 4){iScore += 1000;}this.oDomScore.innerHTML = iScore;}}TETRIS.Block = function (matrix){this.shape = (function(){var aShape = [];for(var i = 0 ; i < matrix.length ; i++){var sValue = matrix[i];for(var j = 0 ; j < sValue.length ; j++){if(sValue.charAt(j) == "1"){aShape.push({ x : j+3 , y : i });}  }}return aShape;})();this.draw();}TETRIS.Block.prototype.move = function (direction){//移動if(this.checkBlock(this.shape,direction)){this.draw("clear");for(var i = 0 ; i < 4 ; i++){switch(direction){case "left"://←this.shape[i].x--;break;case "right":this.shape[i].x++;break;case "down":this.shape[i].y++;break;}}this.draw();} else {if(direction == "down"){this.draw();T.updateBoard();//更新面板var iLines = T.eraseLines();if(iLines > 0){T.setScore(iLines);}T.next();//再產生一個新的方塊}}}TETRIS.Block.prototype.rotate = function (matrix){//變形this.shape = (function(oBlock){var aX = [];var aY = [];for(var i = 0 ; i < 4 ; i++){aX.push(oBlock.shape[i].x);aY.push(oBlock.shape[i].y);}var iMinX = aX.getMin();var iMinY = aY.getMin();var aShape = [];for(var i = 0 ; i < matrix.length ; i++){var sValue = matrix[i];for(var j = 0 ; j < sValue.length ; j++){if(sValue.charAt(j) == "1"){aShape.push({ x : j+iMinX , y : i+iMinY });}  }}if( !( oBlock.checkBlock(aShape)) )return oBlock.shape;oBlock.draw("clear");return aShape;})(this);this.draw();}TETRIS.Block.prototype.draw = function (opt){//繪圖for(var i = 0 ; i < this.shape.length ; i++){var oShape = this.shape[i];T.oDomBoard.rows[oShape.y].cells[oShape.x].style.background = (opt==undefined?"#09F":"");}}TETRIS.Block.prototype.checkBlock = function (shape, direction){shape = shape || this.shape;for(var i = 0 ; i < 4 ; i++){if(direction == "left"){if(shape[i].x == 0 || T.aBoardGrids[shape[i].y][shape[i].x - 1] == 1){return false;}} else if(direction == "right"){if(shape[i].x == 9 || T.aBoardGrids[shape[i].y][shape[i].x + 1] == 1){return false;}} else if(direction == "down"){if(shape[i].y == 17 || T.aBoardGrids[shape[i].y + 1][shape[i].x] ==1){return false;}}if(shape[i].x < 0 || shape[i].x > 9 || shape[i].y < 0 || shape[i].y > 17)return false;if(T.aBoardGrids[shape[i].y][shape[i].x] == 1){return false;}}return true;}Array.prototype.getMin = function (){var iMin = this[0];for(var i = 0 ; i < this.length ; i++){if(this[i] < iMin)iMin = this[i];}return iMin;}window.onload = function(){T.init();var oBtnPlay = document.getElementById("btnPlay");oBtnPlay.onclick = function(){if(this.value == "begin"){T.next();this.value = "over";} else {this.value = "begin";alert("Game Over~");clearInterval(T.timer);} }var oBtnPause = document.getElementById("btnPause");oBtnPause.onclick = function (){if(this.value == "pause"){clearInterval(T.timer);this.value = "resume";} else {T.timer = setInterval(function (){T.oBlock.move("down");}, 1000);this.value = "pause";}}}</script></head><body>    <table id="gameBoard"></table>    <div id="gameControl">        Score : <span id="score">0</span><br />      <input type="button" id="btnPlay" value="begin" />      <input type="button" id="btnPause" value="pause" /><br/>        <span>俄羅斯方塊</span>    </div></body></html>

程式有待最佳化改進,歡迎大家批評指正

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.