下面是俄罗斯方块的截图: 请到这里下载源码: 俄罗斯方块 在线演示 下载地址下面是代码: 复制代码 代码如下: <BR>/********************************************js俄罗斯方块源码***********************************************/ <BR>//作者:高山流水 QQ:21243468 <BR>//创建日期:2009-08-06 <BR>//版权声明:该作品由高山流水创作,转载请注明出处,谢谢合作! <BR>//游戏设计说明: <BR>//1.由于该游戏属于二维游戏,所以布置好网格是写好该游戏的关键,无论是游戏窗口还是预览窗口、 <BR>//方块集虚拟map等都用到了网格的概念,这样做的好处可以避免频繁的获取元素的位置,另外还可以对方块集的移动 <BR>//进行精确的定位和变形。这里比较重要的一点就是提前定义好方块集的map,比如对于L方块集,应定义一个三乘三的正方形 <BR>//网格,然后根据L的每一个形状,确定方块集中的每个方块在正方形网格中的位置,另外还需要保存方块集在map <BR>//(游戏窗口或者预览窗)中的位置,这样任意时刻,通过方块集的位置,和方块在正方形网格中的位置,就可以确定 <BR>//方块集中的每一个方块在map中的位置。 <BR>//2.该游戏主要用到了一些OOP的思想。比如定义一个基类base,方块集类继承自base类,其中还有对象的封装,属性, <BR>//枚举的定义等,当然还有事件,委托,attribute,垃圾收集器等,因为时间关系,就不做代码演示了,各位有兴趣可以自己 <BR>//扩展一下,加深对js OOP的理解。 <BR>//3.js内置对象的扩展:比如Array.prototype.removeAt等 <BR>/********************************************js俄罗斯方块源码**********************************************/ <BR>var Sys = null; <BR>function sys() { } <BR>sys.prototype = { <BR>GameMap: [], <BR>PreviewMap: [], <BR>BlocksObj: [], <BR>Timer: null, <BR>HorizontalNum: 10, //游戏map的水平格数 <BR>VerticalNum: 18, //游戏map的竖直格数 <BR>IsGameOver: false, //判断游戏是否结束 <BR>ScoreStrategy: [100, 300, 500, 800], //得分策略 <BR>LevelScores: [100, 20000, 40000, 60000, 80000, 100000, 120000, 140000, 160000, 200000], //分数等级 <BR>IsPlay: false, //游戏进行中 <BR>IsFirstPlay: true, //是否第一次玩 <BR>SmallGridNum: 6, //预览map的格子数,为正方形 <BR>DirectionEnum: { left: 0, right: 1, up: 2, down: 3 }, //方向的枚举 <BR>Speeds: [1000, 900, 800, 700, 600, 500, 400, 300, 200, 100], //速度,或者说级别 <BR>CurrentSpeed: 1000, //当前级别或者说速度 <BR>TypeEnum: { none: 0, block: 1, blocks: 2 }, //类型 <BR>BlocksEnum: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], //0为LL,1为LR,2为T,3为ZL,4为ZR,5为I,6为F,7为长T <BR>BlocksStateNum: [4, 4, 4, 2, 2, 2, 1, 4, 4, 2], //对应BlocksEnum中每个方块集的变形个数,即有多少种变形 <BR>BlocksShapeMaps: [ //方块形状集的集合 <BR>[ //方块集的虚拟map集合,对应BlocksEnum,比如该map是L的map <BR>[[[2, 1]], [[0, 2], [1, 2], [2, 2]]], //L中的其中一个变形 <BR>[[[1, 0]], [[1, 1]], [[1, 2], [2, 2]]], <BR>[[[0, 1], [1, 1], [2, 1]], [[0, 2]]], <BR>[[[1, 0], [2, 0]], [[2, 1]], [[2, 2]]] <BR>], <BR>[ <BR>[[[2, 0]], [[2, 1]], [[1, 2], [2, 2]]], <BR>[[[0, 1]], [[0, 2], [1, 2]], [[2, 2]]], <BR>[[[0, 0], [1, 0]], [[0, 1]], [[0, 2]]], <BR>[[[0, 1], [1, 1], [2, 1]], [[2, 2]]] <BR>], <BR>[ <BR>[[[0, 0], [1, 0], [2, 0]], [[1, 1]]], <BR>[[[1, 0]], [[0, 1], [1, 1]], [[1, 2]]], <BR>[[[1, 1]], [[0, 2], [1, 2], [2, 2]]], <BR>[[[0, 0]], [[0, 1], [1, 1]], [[0, 2]]] <BR>], <BR>[ <BR>[[[0, 0]], [[0, 1], [1, 1]], [[1, 2]]], <BR>[[[1, 1], [2, 1]], [[0, 2], [1, 2]]] <BR>], <BR>[ <BR>[[[1, 0]], [[0, 1], [1, 1]], [[0, 2]]], <BR>[[[0, 1], [1, 1]], [[1, 2], [2, 2]]] <BR>], <BR>[ <BR>[[[0, 0]], [[0, 1]], [[0, 2]], [[0, 3]]], <BR>[[[0, 3]], [[1, 3]], [[2, 3]], [[3, 3]]] <BR>], <BR>[ <BR>[[[0, 0], [0, 1]], [[1, 0], [1, 1]]] <BR>], <BR>[ <BR>[[[0, 0], [1, 0], [2, 0]], [[1, 1]], [[1, 2]]], <BR>[[[2, 0]], [[0, 1], [1, 1], [2, 1]], [[2, 2]]], <BR>[[[1, 0]], [[1, 1]], [[0, 2], [1, 2], [2, 2]]], <BR>[[[0, 0]], [[0, 1], [1, 1], [2, 1]], [[0, 2]]] <BR>], <BR>[ <BR>[[[0, 1], [1, 1], [2, 1]], [[0, 2]], [[2, 2]]], <BR>[[[1, 0], [2, 0]], [[2, 1]], [[1, 2], [2, 2]]], <BR>[[[0, 1], [2, 1]], [[0, 2], [1, 2], [2, 2]]], <BR>[[[1, 0], [2, 0]], [[1, 1]], [[1, 2], [2, 2]]] <BR>], <BR>[ <BR>[[[0, 0], [1, 0]], [[1, 1]], [[1, 2], [2, 2]]], <BR>[[[2, 0]], [[0, 1], [1, 1], [2, 1]], [[0, 2]]] <BR>], <BR>], <BR>ColorEnum: [[0, 0], [-28, 0], [-56, 0], [-84, 0], [-112, 0], [-140, 0], [-168, 0], [0, 0], [-28, 0], [-56, 0]], //颜色的枚举,对应BlocksEnum <BR>CreateGameMap: function() { //创建游戏map <BR>for (var i = 0; i < this.VerticalNum; i++) { <BR>this.GameMap.push([]); <BR>for (var j = 0; j < this.HorizontalNum; j++) { <BR>this.GameMap[i][j] = {}; <BR>this.GameMap[i][j][Sys.TypeEnum.blocks] = null; <BR>} <BR>} <BR>}, <BR>GetBlocks: function() { //获取GameMap里的方块集 <BR>for (var i = 0; i < this.BlocksObj.length; i++) { <BR>if (this.BlocksObj[i].isInGameMap) { <BR>return this.BlocksObj[i]; <BR>} <BR>} <BR>return null; <BR>}, <BR>AllowBlocksMove: function() { //是否允许方块集移动 <BR>var blocksItem = this.GetBlocks(); <BR>var itemPosArray = this._getBlocksItemPosArray(blocksItem, false); <BR>return this.NoBlocksInthePlace(itemPosArray, blocksItem) && this.CheckBoundary(itemPosArray, blocksItem); <BR>}, <BR>GetMaxAndMinItemPosArray: function(itemPosArray) { //获取最大最小方块位置集合 <BR>itemPosArray.ItemPosXArray.sorts(); <BR>itemPosArray.ItemPosYArray.sorts(); <BR>return { maxItemPosX: itemPosArray.ItemPosXArray[itemPosArray.ItemPosXArray.length - 1], maxItemPosY: itemPosArray.ItemPosYArray[itemPosArray.ItemPosYArray.length - 1], minItemPosX: itemPosArray.ItemPosXArray[0], minItemPosY: itemPosArray.ItemPosYArray[0] }; <BR>}, <BR>NoBlocksInthePlace: function(itemPosArray, blocksItem) { //检测游戏方格中是否已经有方块 <BR>return this._isOverMapChild(itemPosArray, blocksItem) ? false : true; <BR>}, <BR>CheckBoundary: function(itemPosArray, blocksItem) { //是否到达边界了 <BR>var maxAndMinItemPosArray = this.GetMaxAndMinItemPosArray(itemPosArray); <BR>var isNotToBoundary = false; <BR>switch (blocksItem.currentDirectionEnum) { <BR>case this.DirectionEnum.left: <BR>isNotToBoundary = (maxAndMinItemPosArray.minItemPosX > 0) <BR>break; <BR>case this.DirectionEnum.right: <BR>isNotToBoundary = (maxAndMinItemPosArray.maxItemPosX < this.HorizontalNum - 1) <BR>break; <BR>case this.DirectionEnum.down: <BR>isNotToBoundary = (maxAndMinItemPosArray.maxItemPosY < this.VerticalNum - 1); <BR>break; <BR>} <BR>return isNotToBoundary; <BR>}, <BR>_isOverMapChild: function(itemPosArray, blocksItem) { //检测是否会覆盖某个游戏方格中的元素 <BR>var isOverMapChild = false; <BR>for (var i = 0; i < itemPosArray.ItemPosYArray.length; i++) { <BR>var itemX = itemPosArray.ItemPosXArray[i]; <BR>var itemY = itemPosArray.ItemPosYArray[i]; <BR>if (blocksItem.currentDirectionEnum == this.DirectionEnum.left) { <BR>itemX--; <BR>} <BR>else if (blocksItem.currentDirectionEnum == this.DirectionEnum.right) { <BR>itemX++; <BR>} <BR>else if (blocksItem.currentDirectionEnum == this.DirectionEnum.down) { <BR>itemY++; <BR>} <BR>if (this.GameMap[itemY] && this.GameMap[itemY][itemX] && this.GameMap[itemY][itemX][this.TypeEnum.blocks] != null) { <BR>isOverMapChild = true; <BR>break; <BR>} <BR>} <BR>return isOverMapChild; <BR>}, <BR>_getBlocksItemPosArray: function(blocksItem, isRelative) { //获取方块集的位置集合,isRelative=true获取的是方块相对方块集map的位置,否则是方块相对游戏map的位置 <BR>var itemPosXArray = []; <BR>var itemPosYArray = []; <BR>for (var i = 0; i < blocksItem.blocks.length; i++) { <BR>if (isRelative) { <BR>itemPosXArray.push(blocksItem.blocks[i].x); <BR>itemPosYArray.push(blocksItem.blocks[i].y); <BR>} <BR>else { <BR>itemPosXArray.push(blocksItem.blocks[i].x + blocksItem.x); <BR>itemPosYArray.push(blocksItem.blocks[i].y + blocksItem.y); <BR>} <BR>} <BR>return { ItemPosXArray: itemPosXArray, ItemPosYArray: itemPosYArray }; <BR>}, <BR>GetBlocksInitPos: function(blocks) { //获取方块的初始位置 <BR>var blocksItem = null; <BR>if (!blocks) <BR>blocksItem = this.GetBlocks(); <BR>else <BR>blocksItem = blocks; <BR>var itemPosArray = this._getBlocksItemPosArray(blocksItem, true); <BR>itemPosArray.ItemPosXArray = itemPosArray.ItemPosXArray.filter(); <BR>itemPosArray.ItemPosYArray = itemPosArray.ItemPosYArray.filter(); <BR>var childsNumX = itemPosArray.ItemPosXArray.length; <BR>var childsNumY = itemPosArray.ItemPosYArray.length; <BR>var maxAndMinItemPosArray = this.GetMaxAndMinItemPosArray(itemPosArray); <BR>if (blocks) //获取方块集在预览map中的初始位置 <BR>return { x: (this.SmallGridNum - childsNumX - 1) / 2 + 0.5 - maxAndMinItemPosArray.minItemPosX, y: (this.SmallGridNum - childsNumY - 1) / 2 + 0.5 - maxAndMinItemPosArray.minItemPosY }; <BR>else //获取方块集在游戏map中的初始位置 <BR>return { x: parseInt((this.HorizontalNum - childsNumX - 1) / 2) + 1 - maxAndMinItemPosArray.minItemPosX, y: -(childsNumY + maxAndMinItemPosArray.minItemPosY) }; <BR>}, <BR>GetNextActiviteBrocks: function() { //获取下一个活动的方块集 <BR>for (var i = 0; i < this.BlocksObj.length; i++) { <BR>if (this.BlocksObj[i].isInGameMap) { <BR>this.BlocksObj.removeAt(i); <BR>} <BR>} <BR>this.BlocksObj[0].isInGameMap = true; <BR>var itemPos = this.GetBlocksInitPos(); <BR>this.BlocksObj[0].x = itemPos.x; <BR>this.BlocksObj[0].y = itemPos.y; <BR>this.BlocksObj[0].AddToMap(false, false); <BR>this.CreateBlocks(); <BR>}, <BR>PlayGame: function() { //启动游戏 <BR>this.IsPlay = true; <BR>this.NaturalMove(); <BR>if (!this.IsFirstPlay) { <BR>return; <BR>} <BR>this.GetNextActiviteBrocks(); <BR>}, <BR>AddToGameMapGrid: function() { //加入到游戏map网格中 <BR>var blocks = this.GetBlocks(); <BR>blocks.UseGrid(this.GameMap, blocks); <BR>}, <BR>GetScore: function() { //分数处理 <BR>var rowIndexArray = []; <BR>for (var i = 0; i < this.VerticalNum; i++) { //获取满行的行数 <BR>var entireRow = true; <BR>for (var j = 0; j < this.HorizontalNum; j++) { <BR>if (this.GameMap[i][j][this.TypeEnum.blocks] == null) { <BR>entireRow = false; <BR>break; <BR>} <BR>} <BR>if (entireRow) <BR>rowIndexArray.push(i); <BR>} <BR>if (rowIndexArray.length > 0) { <BR>this._FreeMapGrid(rowIndexArray); <BR>document.getElementById("score").innerText = this.ScoreStrategy[rowIndexArray.length - 1] + parseInt(document.getElementById("score").innerText); <BR>this.CheckTheLevel(); <BR>} <BR>}, <BR>CheckTheLevel: function() { //检测是否进入下一个级别 <BR>var currentScore = parseInt(document.getElementById("score").innerText); <BR>var speedList = document.getElementById("speed"); <BR>var currentLevel = parseInt(speedList.options[speedList.selectedIndex].text) - 1; <BR>var levelScore = this.LevelScores[currentLevel]; <BR>if (currentScore >= levelScore) { <BR>if (currentLevel < this.LevelScores.length) { <BR>var element = document.getElementById("gameOver"); <BR>element.innerText = "恭喜你通过第" + (speedList.selectedIndex + 1) + "关"; <BR>element.style.display = "block"; <BR>this.PauseGame(); <BR>document.getElementById("btnStart").disabled = true; <BR>document.getElementById("speed").disabled = true; <BR>this._goToNextLevel.delay(3000); <BR>} <BR>else { <BR>this._finishAllTheLevel(element); <BR>} <BR>} <BR>}, <BR>_goToNextLevel: function() { //进入下一个级别,速度加快 <BR>Sys.IsPlay = true; <BR>document.getElementById("btnStart").disabled = false; <BR>var speedList = document.getElementById("speed"); <BR>speedList.disabled = false; <BR>speedList.options[speedList.selectedIndex + 1].selected = true; <BR>Sys.CurrentSpeed = Sys.Speeds[speedList.selectedIndex + 1]; <BR>Sys.NaturalMove(); <BR>document.getElementById("gameOver").style.display = "none"; <BR>}, <BR>_finishAllTheLevel: function() { //完成所有的游戏级别 <BR>this.PauseGame(); <BR>}, <BR>_FreeMapGrid: function(rowIndexArray) { //从游戏map中释放满行的网格 <BR>var gameMap = this.GameMap; <BR>var startIndex = rowIndexArray[0]; <BR>var len = rowIndexArray.length; <BR>var maxIndex = startIndex + len - 1; <BR>for (var i = startIndex; i <= maxIndex; i++) { <BR>for (var j = 0; j < this.HorizontalNum; j++) { <BR>if (gameMap[i][j][this.TypeEnum.blocks] != null) { <BR>document.getElementById("map").removeChild(gameMap[i][j][this.TypeEnum.blocks].domElement); <BR>gameMap[i][j][this.TypeEnum.blocks] = null; <BR>} <BR>} <BR>} <BR>this.ResetMapGrid(rowIndexArray); <BR>}, <BR>ResetMapGrid: function(rowIndexArray) { //重置游戏网格 <BR>var gameMap = this.GameMap; <BR>var maxIndex = rowIndexArray[0]; <BR>var len = rowIndexArray.length; <BR>for (var i = maxIndex - 1; i >= 0; i--) { <BR>for (var j = 0; j < this.HorizontalNum; j++) { <BR>if (gameMap[i][j][this.TypeEnum.blocks] != null) { <BR>this._resetMapElement(gameMap[i][j][this.TypeEnum.blocks].domElement, len); <BR>gameMap[i + len][j][this.TypeEnum.blocks] = gameMap[i][j][this.TypeEnum.blocks]; <BR>gameMap[i][j][this.TypeEnum.blocks] = null; <BR>} <BR>} <BR>} <BR>}, <BR>_resetMapElement: function(element, len) { //重置dom元素,比如共有满行两行,则之上的元素均需下降两个 <BR>element.style.top = (parseInt(element.style.top) + 28 * len) + "px"; <BR>}, <BR>InitSpeed: function() { //初始化游戏级别 <BR>var speedList = document.getElementById("speed"); <BR>if (speedList.options.length == 0) { <BR>for (var i = 0; i < this.Speeds.length; i++) { <BR>var varItem = new Option(i + 1, this.Speeds[i]); <BR>speedList.options.add(varItem); <BR>} <BR>} <BR>this.SetSpeedSelected(); <BR>}, <BR>SetSpeedSelected: function() { //选中级别 <BR>var speedList = document.getElementById("speed"); <BR>for (var i = 0; i < speedList.options.length; i++) { <BR>if (speedList.options[i].value == this.CurrentSpeed) { <BR>speedList.options[i].selected = true; <BR>break; <BR>} <BR>} <BR>}, <BR>GameOver: function() { //游戏结束 <BR>this.IsGameOver = true; <BR>this.PauseGame(); <BR>var element = document.getElementById("gameOver"); <BR>element.innerText = "Game Over!"; <BR>element.style.display = "block"; <BR>document.getElementById("btnStart").value = "try again"; <BR>}, <BR>PauseGame: function() { //暂停游戏 <BR>this.IsPlay = false; <BR>clearInterval(this.Timer); <BR>}, <BR>CreateBlocks: function() { //创建方块集 <BR>var currentNum = this.BlocksEnum.length.getRandom(); <BR>var blocks = new Blocks(0, 0, this.BlocksStateNum[currentNum], currentNum, this.ColorEnum[currentNum]); <BR>blocks.Init(); <BR>if (this.BlocksObj.length == 3) <BR>Sys.BlocksObj.pop(); <BR>Sys.BlocksObj.push(blocks); <BR>}, <BR>NaturalMove: function() { //自然下落 <BR>this.Timer = setInterval("Moving()", Sys.CurrentSpeed); <BR>} <BR>} <BR>function Base() { } //定义base类 <BR>Base.prototype.AddToMap = function(isAddToPreviewMap, isMoving) { //添加方块集到map中 <BR>for (var i = 0; i < this.blocks.length; i++) { <BR>var element = null; <BR>if (!this.isInGameMap) { //如果方块集是在预览map中 <BR>element = document.createElement("DIV"); <BR>document.getElementById("PreviewMap").appendChild(element); <BR>this.blocksElement.push(element); <BR>this.blocks[i].domElement = element; <BR>} <BR>else <BR>element = this.blocksElement[i]; <BR>if (!isAddToPreviewMap && !isMoving) //由预览map移动到游戏map时 <BR>document.getElementById("map").appendChild(element); <BR>element.style.position = "absolute"; <BR>element.style.left = ((this.x + this.blocks[i].x) * 28) + "px"; //设置元素所在的map的位置 <BR>element.style.top = ((this.y + this.blocks[i].y) * 28) + "px"; <BR>element.style.backgroundPositionX = this.color[0]; <BR>element.style.backgroundPositionY = this.color[1]; <BR>} <BR>} <BR>Base.prototype.UseGrid = function(map, blocksItem) { //方块集加入到游戏map中 <BR>for (var i = 0; i < blocksItem.blocks.length; i++) { <BR>var itemX = blocksItem.x + blocksItem.blocks[i].x; <BR>var itemY = blocksItem.y + blocksItem.blocks[i].y; <BR>if (blocksItem.y < 0) { <BR>Sys.GameOver(); <BR>return; <BR>} <BR>map[itemY][itemX] = {}; <BR>map[itemY][itemX][this.type] = blocksItem.blocks[i]; <BR>} <BR>} <BR>function Block(x, y) { //定义方块结构体 <BR>this.x = x; <BR>this.y = y; <BR>this.type = Sys.TypeEnum.block; <BR>this.domElement = null; <BR>} <BR>function Blocks(x, y, state, blocksEnum, colorEnum) { //方块集类 <BR>this.x = x; <BR>this.y = y; <BR>this.state = state; <BR>this.blocksEnum = blocksEnum; //方块类型(比如L,I,田字形,Z等) <BR>this.color = colorEnum; <BR>this.type = Sys.TypeEnum.blocks; //废弃属性 <BR>this.blocks = []; //方块集下的方块的集合 <BR>this.blocksElement = []; //方块集下的方块的对应dom元素集 <BR>this.currentState = 0; //当前的状态,比如L有四种类型的变形 <BR>this.isInGameMap = false; //是否在游戏map中 <BR>this.currentDirectionEnum = Sys.DirectionEnum.down; //默认方向向下 <BR>} <BR>Blocks.prototype = new Base(); //继承base类 <BR>Blocks.prototype.Init = function() {//初始化blocks <BR>var blocksPoses = Sys.BlocksShapeMaps[this.blocksEnum]; <BR>this.currentState = Sys.BlocksStateNum[this.blocksEnum].getRandom(); //随机获取方块集的状态 <BR>var blocksPos = blocksPoses[this.currentState]; //获取方块集的map <BR>for (var i = 0; i < blocksPos.length; i++) { <BR>for (var j = 0; j < blocksPos[i].length; j++) { <BR>var block = new Block(blocksPos[i][j][0], blocksPos[i][j][1]); <BR>this.blocks.push(block); <BR>} <BR>} <BR>var itemPos = Sys.GetBlocksInitPos(this); //获取初始位置,也就是说在预览map中的位置 <BR>this.x = itemPos.x; <BR>this.y = itemPos.y; <BR>this.AddToMap(true, false); //加入到预览map中 <BR>} <BR>Blocks.prototype.ChangeShape = function() {//方块变换形状 <BR>var gameMap = Sys.GameMap; <BR>var allowChangeShape = true; <BR>var blocksPoses = Sys.BlocksShapeMaps[this.blocksEnum]; <BR>var num = Sys.BlocksStateNum[this.blocksEnum]; <BR>var currentState1 = -1; <BR>this.currentState == num - 1 ? currentState1 = 0 : currentState1 = this.currentState + 1; <BR>var blocksPos = blocksPoses[currentState1]; <BR>var k = 0; <BR>for (var i = 0; i < blocksPos.length; i++) { //主要是检测方块集的下一个变形是否合理 <BR>for (var j = 0; j < blocksPos[i].length; j++) { <BR>var block = this.blocks[k]; <BR>var itemX = this.x + blocksPos[i][j][0]; <BR>var itemY = this.y + blocksPos[i][j][1]; <BR>if ((itemX > Sys.HorizontalNum - 1) || (itemX < 0) || (itemY > Sys.VerticalNum - 1) || itemY >= 0 && gameMap[itemY][itemX] != null && gameMap[itemY][itemX][Sys.TypeEnum.blocks] != null) { <BR>allowChangeShape = false; <BR>break; <BR>} <BR>k++; <BR>} <BR>} <BR>if (allowChangeShape)//如果允许变形 <BR>{ <BR>this.currentState == num - 1 ? this.currentState = 0 : this.currentState++; //设置下一个变形的状态 <BR>k = 0; <BR>for (var i = 0; i < blocksPos.length; i++) { <BR>for (var j = 0; j < blocksPos[i].length; j++) { <BR>var block = this.blocks[k]; <BR>block.x = blocksPos[i][j][0]; <BR>block.y = blocksPos[i][j][1]; <BR>k++; <BR>} <BR>} <BR>this.AddToMap(false, true); //变形后加入到游戏map中 <BR>} <BR>} <BR>Blocks.prototype.BlocksMoveDown = function(isMoving) { //方块集下落 <BR>this.currentDirectionEnum = Sys.DirectionEnum.down; <BR>if (!Sys.AllowBlocksMove()) { //如果不允许移动 <BR>Sys.AddToGameMapGrid(); //固定方块集在游戏map中的位置 <BR>Sys.GetScore(); //得分处理 <BR>Sys.GetNextActiviteBrocks(); //获取下一个方块集 <BR>} <BR>else { //下落一格 <BR>this.y++; <BR>this.AddToMap(false, isMoving); <BR>} <BR>} <BR>Number.prototype.getRandom = function() {//获取0至number之间的随机数 <BR>var num = this; <BR>var i = this + 1; <BR>while (i >= num) { <BR>i = Math.round(Math.random() * 10); <BR>} <BR>return i; <BR>} <BR>Array.prototype.sorts = function() { return this.sort(compare); } //数组排序,按照升序排序 <BR>function compare(a, b) { return a - b; } //定义排序规则 <BR>Array.prototype.removeAt = function(dx) { //清除指定索引的数组元素 <BR>if (isNaN(dx) || dx > this.length) { return false; } <BR>for (var i = 0, n = 0; i < this.length; i++) { <BR>if (this[i] != this[dx]) <BR>this[n++] = this[i]; <BR>} <BR>this.length -= 1; <BR>} <BR>Array.prototype.filter = function() { //清除数组中的重复值 <BR>var arr = []; <BR>for (var i = 0; i < this.length; i++) { <BR>if (!arr.contains(this[i])) <BR>arr.push(this[i]); <BR>} <BR>return arr; <BR>} <BR>Array.prototype.contains = function(item) { //检测数组是否包含某元素 <BR>for (var i = 0; i < this.length; i++) { <BR>if (this[i] == item) <BR>return true; <BR>} <BR>return false; <BR>} <BR>Function.prototype.delay = function(time) { var timer = setTimeout(this, time); } //函数延迟time毫秒执行 <BR>window.onload = InitGame; <BR>function InitGame() {//初始化游戏 <BR>Sys = new sys(); <BR>Sys.BlocksObj = []; <BR>Sys.InitSpeed(); //初始化游戏速度 <BR>Sys.CreateGameMap(); //创建游戏map <BR>Sys.CreateBlocks(); //创建方块集 <BR>} <BR>function GameStart(element) { <BR>if (element.value == "start") { //开始游戏 <BR>element.value = "pause"; <BR>Sys.PlayGame(); <BR>Sys.IsFirstPlay = false; <BR>} <BR>else if (element.value == "pause") { //暂停游戏 <BR>element.value = "start" <BR>Sys.PauseGame(); <BR>} <BR>else { //游戏结束后重新开始 <BR>window.location.reload(); <BR>} <BR>} <BR>function Moving() {//移动 <BR>Sys.GetBlocks().BlocksMoveDown(false); <BR>} <BR>function ChangeSpeed(e) {//切换级别 <BR>var speedlist = document.getElementById("speed"); <BR>Sys.CurrentSpeed = speedlist.options[speedlist.selectedIndex].value; <BR>if (!Sys.IsGameOver) { <BR>clearInterval(Sys.Timer); <BR>this.NaturalMove(); <BR>} <BR>} <BR>function keyDown(e) { //按键操作 <BR>if (Sys.IsGameOver || !Sys.IsPlay) return; <BR>var blocks = Sys.GetBlocks(); <BR>if (e.keyCode == 37) { //向左 <BR>blocks.currentDirectionEnum = Sys.DirectionEnum.left; <BR>if (Sys.AllowBlocksMove()) <BR>blocks.x--; <BR>if (blocks.x != 0) <BR>blocks.AddToMap(false, true); <BR>} <BR>else if (e.keyCode == 38) { //向上 <BR>blocks.currentDirectionEnum = Sys.DirectionEnum.up; <BR>blocks.ChangeShape(); <BR>} <BR>else if (e.keyCode == 39) { //向右 <BR>blocks.currentDirectionEnum = Sys.DirectionEnum.right; <BR>var oldX = blocks.x; <BR>if (Sys.AllowBlocksMove()) <BR>blocks.x++; <BR>if (blocks.x != oldX) <BR>blocks.AddToMap(false, true); <BR>} <BR>else if (e.keyCode == 40) //向下 <BR>{ <BR>blocks.currentDirectionEnum = Sys.DirectionEnum.down; <BR>blocks.BlocksMoveDown(true); <BR>} <BR>} <BR> <BR>body <BR>{ <BR>background-color:#ffffff; <BR>overflow:hidden; <BR>font-size:14px; <BR>} <BR>.gameZone <BR>{ <BR>position:absolute; <BR>left:0px; <BR>top:0px; <BR>width:100%; <BR>height:550px; <BR>background-Color:white; <BR>} <BR>.mask <BR>{ <BR>position:absolute; <BR>left:100px; <BR>top:0px; <BR>width:300px; <BR>height:20px; <BR>background-color:White; <BR>border:solid 0px; <BR>z-index:5; <BR>} <BR>.map <BR>{ <BR>position:absolute; <BR>left:100px; <BR>top:20px; <BR>width:280px; <BR>height:504px; <BR>background-image:url(images/tetris_grid.gif); <BR>border:solid 3px green; <BR>} <BR>.gameOver <BR>{ <BR>position:absolute; <BR>left:100px; <BR>top:20px; <BR>width:280px; <BR>height:504px; <BR>font-weight:800; <BR>font-size:xx-large; <BR>color:Red; <BR>text-align:center; <BR>border:solid 3px; <BR>line-height:420px; <BR>display:none; <BR>filter: Alpha(Opacity=80); <BR>background-color:pink; <BR>} <BR>.map div <BR>{ <BR>BACKGROUND-IMAGE: url(images/tetris.gif); <BR>WIDTH: 28px; <BR>BACKGROUND-REPEAT: no-repeat; <BR>POSITION: absolute; <BR>HEIGHT: 28px <BR>} <BR>.PreviewMap <BR>{ <BR>position:absolute; <BR>left:400px; <BR>top:20px; <BR>width:168px; <BR>height:168px; <BR>background-color:pink; <BR>border:solid 2px green; <BR>} <BR>.PreviewMap div <BR>{ <BR>BACKGROUND-IMAGE: url(images/tetris.gif); <BR>WIDTH: 28px; <BR>BACKGROUND-REPEAT: no-repeat; <BR>POSITION: absolute; <BR>HEIGHT: 28px <BR>} <BR>.start <BR>{ <BR>position:absolute; <BR>left:400px; <BR>top:240px; <BR>width:168px; <BR>height:40px; <BR>} <BR>.scoreSpeed <BR>{ <BR>position:absolute; <BR>left:400px; <BR>top:200px; <BR>width:190px; <BR>height:40px; <BR>} <BR>.score <BR>{ <BR>color:pink; <BR>font-weight:bold; <BR>width:20px; <BR>height:20px; <BR>background-color:blue; <BR>padding-left:10px; <BR>padding-right:10px; <BR>font-size:medium; <BR>} <BR>.speed <BR>{ <BR>color:pink; <BR>font-weight:bold; <BR>width:20px; <BR>height:20px; <BR>background-color:blue; <BR>padding-left:5px; <BR>padding-right:5px; <BR>font-size:medium; <BR>} <BR>.copyright <BR>{ <BR>position:absolute; <BR>left:400px; <BR>top:280px; <BR>word-break:break-all; <BR>width:160px; <BR>height:225px; <BR>border:solid 2px green; <BR>padding:5px; <BR>} <BR> 得分:0 级别: 版权所有 此俄罗斯方块由高山流水开发,欢迎各位使用, 如有bug或者好的意见,请给我留言,谢谢支持! 另如需转载,请注明出处! 顺便,宣传一下我的MVC qq群:45660795,欢迎加入! 作者:高山流水 QQ:21243468