一个javascript脚本写的俄罗斯方块 (2)Russia.js


var Cols = 10, Rows = 20, Sqlen = 16;
var Color = new Array(8);
var delLine = new Array();
var CLoc = new Array();
var RLoc = new Array();
var CurSq,NextSq;
var type=-1,oldtype;
var delay = new Array(600,500,400,300,200,100,90,80,70);
var TimerID;
var pos = 0,end,level=0,score=0,lines=0;
var isOver=false,isPause=false;

Color[0] = "#d0d0d0";
Color[1] = "red";
Color[2] = "green"
Color[3] = "cyan";
Color[4] = "yellow";
Color[5] = "orange";
Color[6] = "pink";
Color[7] = "blue";


function GameArea(row,col,name){
var s = "<TABLE BORDER=1 cellspacing=0 cellpadding=1 bgcolor=" + Color[0] + ">";
for(var i=0; i<row; i++){
  s = s + "<TR Height=" + Sqlen + ">";
  for(var j=0; j<col; j++){
   var id = name + i + "#" + j;
   s = s + "<TD Width=" + Sqlen + " class=btnup id=" + id;
   s = s + " style=\"background:" + Color[0] + "\"> </TD>"
  }
  s = s + "</TR>";
}
s = s + "</TABLE>";
return s;
}

function Init(){
GameBody.innerHTML = GameArea(Rows,Cols,'Main');
GameForecast.innerHTML = GameArea(4,4,'Forecast');
}

function Square(cols,rows,color){
this.rows = rows;
this.cols = cols;
this.color = color;
}

function chooseSquare(type,x,y){
var sq = new Array(4);
switch(type){
  case 0:
   sq[0] = new Square(x-1,y,1);
   sq[1] = new Square(x,y,1);
   sq[2] = new Square(x+1,y,1);
   sq[3] = new Square(x+2,y,1);
   break;
  case 1:
   sq[0] = new Square(x,y,5);
   sq[1] = new Square(x,y+1,5);
   sq[2] = new Square(x+1,y+1,5);
   sq[3] = new Square(x,y+2,5);
   break;
  case 2:
   sq[0] = new Square(x,y,2);
   sq[1] = new Square(x,y+1,2);
   sq[2] = new Square(x+1,y+1,2);
   sq[3] = new Square(x+1,y+2,2);
   break;
  case 3:
   sq[0] = new Square(x+1,y,7);
   sq[1] = new Square(x+1,y+1,7);
   sq[2] = new Square(x,y+1,7);
   sq[3] = new Square(x,y+2,7);
   break;
  case 4:
   sq[0] = new Square(x,y,3);
   sq[1] = new Square(x+1,y,3);
   sq[2] = new Square(x,y+1,3);
   sq[3] = new Square(x+1,y+1,3);
   break;
  case 5:
   sq[0] = new Square(x,y,6);
   sq[1] = new Square(x+1,y,6);
   sq[2] = new Square(x+1,y+1,6);
   sq[3] = new Square(x+1,y+2,6);
   break;
  case 6:
   sq[0] = new Square(x+1,y,4);
   sq[1] = new Square(x,y,4);
   sq[2] = new Square(x,y+1,4);
   sq[3] = new Square(x,y+2,4);
   break;
}
return sq;
}

function reDraw(name,sq){
var obj;
for(var i=0; i<sq.length; i++){
  obj = document.all(name + sq[i].rows + "#" + sq[i].cols);
  obj.style.background = Color[sq[i].color];
}
}
function clearDraw(name,sq){
var obj;
for(var i=0; i<sq.length; i++){
  obj = document.all(name + sq[i].rows + "#" + sq[i].cols);
  obj.style.background = Color[0];
}
}

function isBounds(sq){
for(var i=0; i<sq.length; i++){
  if(sq[i].cols<0 || sq[i].cols>9 || sq[i].rows<0 || sq[i].rows>19)
   return false;
}
return true;
}

function SortSquare(sq,name,isMax){
if(isMax){
  var Max = 0;
  if(name=="Rows"){
   for(var i=0; i<sq.length; i++){
    if(sq[i].rows>Max) Max = sq[i].rows;
   }
  }
  else{
   for(var i=0; i<sq.length; i++){
    if(sq[i].cols>Max) Max = sq[i].cols;
   }  
  }
  return Max;
}
else{
  var Min = 20;
  if(name=="Rows"){
   for(var i=0; i<sq.length; i++){
    if(sq[i].rows<Min) Min = sq[i].rows;
   }
  }
  else{
   for(var i=0; i<sq.length; i++){
    if(sq[i].cols<Min) Min = sq[i].cols;
   } 
  }
  return Min;
}
}

function StartGame(){
start.disabled = true;
pause.focus();
type = parseInt(Math.random()*7);
oldtype = type;
type = parseInt(Math.random()*7);
var m = 4/2 -1;
NewSq = chooseSquare(type,m,0);
reDraw('Forecast',NewSq);
Start();
}
function Start(){
if(isOver){
  var s = "本次游戏结束! ";
  OverGame(s);
}
gScore.innerText = score;
gLine.innerText = lines;
gLevel.innerText = level;
oldtype = type;
clearDraw('Forecast',NewSq);
type = parseInt(Math.random()*7);
var m = 4/2 -1;
NewSq = chooseSquare(type,m,0);
reDraw('Forecast',NewSq);
var mm = Cols/2 -1;
CurSq = chooseSquare(oldtype,mm,0);
reDraw('Main',CurSq);
window.clearInterval(TimerID);
TimerID = window.setInterval("Run()",delay[level]);
}
function Run(){
if(isPause) return;
if(MoveCurSq(0,1,false) == false){
  window.clearInterval(TimerID);
  if(RemoveLines()==true) {
   DelLines();
   lines = lines + delLine.length;
   switch(delLine.length){
    case 1:
     score = score + 100;
     break;
    case 2:
     score = score + 300;
     break;
    case 3:
     score = score + 700;
     break;
    case 4:
     score = score + 1000;
     break;
   }

   level = parseInt(score/10000);
  }
  Start();
}
}

function RemoveLines(){
var obj,m=0;
var is

本文作者:



相关阅读:
JavaScript是什么?
css 定位应用实例
解决Windows Server 2008 回收站无法清空故障
使用AJAX技术构建更优秀的Web应用程序
透析PHP的配置文件php.ini
LINUX操作系统的概念和特点
Apache Web 服务器的完全安装指南
MSDOS.SYS文件的参数设置方法
JSP由浅入深(1)—— 熟悉JSP服务器
历史上最详细的Linux DNS配置教程 推荐
javascript操作select参考代码
CSS模块化如何实现?
13个应用HTML5和CSS3实现的Web效果
教你使用MySQL触发器自动更新memcache
快速导航

Copyright © 2016 phpStudy |