日期:2014-05-16 浏览次数:20413 次
<!DOCTYPE html> <html> <head> <title></title> <style type="text/css"> .c{margin:1px; width:19px; height:19px; background:red; position:absolute;} .d{margin:1px; width:19px; height:19px; background:gray; position:absolute;} .f{top:0px; left:0px; background:black; position:absolute;} </style> <script type="text/javascript"> var row = 18; var col = 10; var size = 20; var isOver = false; var shapes = ("0,1,1,1,2,1,3,1;1,0,1,1,1,2,2,2;2,0,2,1,2,2,1,2;0,1,1,1,1,2,2,2;1,2,2,2,2,1,3,1;1,1,2,1,1,2,2,2;0,2,1,2,1,1,2,2").split(";"); var tetris; var container; // 创建DIV的公用方法 function createElm(tag,css) { var elm = document.createElement(tag); elm.className = css; document.body.appendChild(elm); return elm; } // 方块类 function Tetris(css,x,y,shape) { // 创建4个div用来组合出各种方块 var myCss = css?css:"c"; this.divs = [createElm("div",myCss),createElm("div",myCss),createElm("div",myCss),createElm("div",myCss)]; this.container = null; this.refresh = function() { this.x = (typeof(x)!='undefined')?x:3; this.y = (typeof(y)!='undefined')?y:0; this.shape = shape?shape:shapes[Math.floor((Math.random()*shapes.length-0.000000001))].split(","); if(this.container&&!this.container.check(this.x,this.y,this.shape)) { isOver = true; alert("游戏结束"); } else { this.show(); } } // 显示方块 this.show = function() { for(var i in this.divs) { this.divs[i].style.top = (this.shape[i*2+1]- -this.y)*size+"px"; this.divs[i].style.left = (this.shape[i*2]- -this.x)*size+"px"; } } // 水平移动方块的位置 this.hMove = function(step) { if(this.container.check(this.x- -step,this.y,this.shape)) { this.x += step; this.show(); } } // 垂直移动方块位置 this.vMove = function(step) { if(this.container.check(this.x,this.y- -step,this.shape)) { this.y += step; this.show(); } else { this.container.fixShape(this.x,this.y,this.shape); this.container.findFull(); this.refresh(); } } // 旋转方块 this.rotate = function() { var newShape = [this.shape[1],3-this.shape[0],this.shape[3],3-this.shape[2],this.shape[5],3-this.shape[4],this.shape[7],3-this.shape[6]]; if(this.container.check(this.x,this.y,newShape)) { this.shape = newShape; this.show(); } } this.refresh(); } function Container() { // 设置背景 this.createBackground = function() { var bgDiv = createElm("div","f"); bgDiv.style.width = size*col+"px"; bgDiv.style.height = size*row+"px"; } // 检查边界越界 this.check = function(x,y,shape) { var flag = false; var leftmost=col; var rightmost=0; var undermost=0; for(var i=0;i<8;i+=2) { // 记录最左边水平坐标 if(shape[i]<leftmost) { leftmost = shape[i]; } // 记录最右边水平坐标 if(shape[i]>rightmost) { rightmost = shape[i]; } // 记录最下边垂直坐标 if(shape[i+1]>undermost) { undermost = shape[i+1]; } // 判断是否碰撞 if(this[(shape[i+1]- -y)*100- -(shape[i]- -x)]) { flag = true; } } // 判断是否到达极限高度 for(var m=0;m<3;m++) { for(var n=0;n<col;n++) { if(this[m*100+n])