一个javascript脚本写的俄罗斯方块
2007-11-27 17:25:06 来源:WEB开发网Russia.htm
<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.0 Transitional//EN>
<HTML>
<HEAD>
<TITLE> 俄罗斯方块 </TITLE>
<META NAME=Generator CONTENT=EditPlus>
<META NAME=Author CONTENT=dolphin>
<META NAME=Description CONTENT=俄罗斯方块游戏>
<STYLE>
.btnup{
border-left:solid #ffffff 1px;
border-top:solid #ffffff 1px;
border-right:solid #828486 1px;
border-bottom:solid #828486 1px;
font-size: 9pt;
cursor:default
}
tr{
font-family:宋体;
font-size: 9pt;
cursor: default
}
</STYLE>
<SCRIPT LANGUAGE=JavaScript src=Russia.js></SCRIPT>
</HEAD>
<BODY BGCOLOR=#FFFFFF onload=Init() onkeydown=keyDown()>
<TABLE border=0 cellspacing=2 cellpadding=6 align=center width=60%>
<TR valign=top>
<TD width=120>
得分: <SPAN ID=gScore></SPAN> <p>
级别: <SPAN ID=gLevel></SPAN> <p>
行数: <SPAN ID=gLine></SPAN> <p>
</TD>
<TD><SPAN ID=GameBody><!-- 游戏主区域 --></SPAN></TD>
<TD>
<SPAN ID=GameForecast><!-- 游戏预报区域 --></SPAN><p>
<BUTTON ID=start onclick=StartGame()>开始游戏</BUTTON><p>
<BUTTON ID=pause onclick=PauseGame()>暂停游戏</BUTTON><p>
<BUTTON ID=over onclick=OverGame()>结束游戏</BUTTON><p>
<BUTTON>帮 助</BUTTON>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>
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 isRemove = true;
var searchPos = true;
var del = new Array();
for(var i=19; i>=0; i--){
for(var j=0; j<10; j++){
obj = document.all(Main + i + # + j);
if(obj.style.background == Color[0]){
isRemove = false;
}
else{
searchPos = false;
}
}
if(searchPos) {
end = i;
if(del.length>0){delLine=del;return true;}
else{return false;}
}
if(isRemove) {del[m] = i; m++;}
searchPos = true;
isRemove = true;
}
}
function DelLines(){
var deleted = 0;
var obj;
for(var i=0; i<delLine.length; i++){
for(var j=0; j<10; j++){
obj = document.all(Main + delLine[i] + # + j);
obj.style.background = Color[0];
}
deleted = deleted + 1;
if(delLine.length>deleted)
{
if(delLine[i]-delLine[i+1]>1) ReDrawLine(delLine[i+1],delLine[i],deleted);
}
}
ReDrawLine(end,delLine[delLine.length - 1],deleted);
}
function ReDrawLine(endline,startline,moveline){
var obj;
for(var i= startline - 1; i>endline; i--){
for(var j=0; j<10; j++){
obj = document.all(Main + i + # + j);
var oldcolor = obj.style.background;
obj.style.background = Color[0];
var rowid = i + moveline;
obj = document.all(Main + rowid + # + j);
obj.style.background = oldcolor;
}
}
}
function MoveSquare(from,to){
if(isBounds(to) == false) return false;
var obj;
loop:
for(var i=0; i<to.length; i++){
obj = document.all(Main + to[i].rows + # + to[i].cols);
if(obj.style.background != Color[0]){
for(var j=0; j<from.length; j++)
if(to[i].cols==from[j].cols&&to[i].rows==from[j].rows&&to[i].color==from[j].color)
continue loop;
return false
}
}
return true;
}
function MoveCurSq(x,y,isRotate){
NextSq = new Array(CurSq.length);
for(var i=0; i<CurSq.length; i++){
if(isRotate){
var dx = CurSq[i].cols - CurSq[0].cols;
var dy = CurSq[i].rows - CurSq[0].rows;
NextSq[i] = new Square(CurSq[0].cols-dy,CurSq[0].rows+dx,CurSq[i].color);
}
else
NextSq[i] = new Square(CurSq[i].cols+x,CurSq[i].rows+y,CurSq[i].color);
}
if(isRotate) reNextSq();
if(MoveSquare(CurSq,NextSq) == false){
for(var i=0; i<CurSq.length; i++){
if((CurSq[i].rows==0&&CurSq[i].cols==Cols/2)||(CurSq[i].rows==0&&CurSq[i].cols==Cols/2-1))
isOver = true;
}
return false;
}
clearDraw(Main,CurSq);
CurSq = NextSq;
reDraw(Main,CurSq);
return true;
}
function reNextSq(){
var minCols = SortSquare(NextSq,Cols,false);
var minRows = SortSquare(NextSq,Rows,false);
var maxCols = SortSquare(NextSq,Cols,true);
var maxRows = SortSquare(NextSq,Rows,true);
if(minCols<0) changeNextSq(Cols,-minCols);
if(minRows<0) changeNextSq(Rows,-minRows);
if(maxCols>9) changeNextSq(Cols,9-maxCols);
if(maxRows>19) changeNextSq(Rows,19-maxRows);
}
function changeNextSq(name,pos){
for(var i=0; i<NextSq.length; i++){
if(name=Rows)
NextSq[i].rows = NextSq[i].rows + pos;
else
NextSq[i].cols = NextSq[i].cols + pos;
}
}
function keyDown(){
switch(event.keyCode){
case 40:
MoveCurSq(0,1,false);
break;
case 37:
MoveCurSq(-1,0,false);
break;
case 38:
MoveCurSq(0,0,true);
break;
case 39:
MoveCurSq(1,0,false);
break;
}
}
function PauseGame(){
if(pause.innerText == 暂停游戏){
isPause = true;
pause.innerText = 继续游戏;
pause.focus();
}
else{
isPause = false;
pause.innerText = 暂停游戏;
pause.focus();
Run();
}
}
function OverGame(str){
if(typeof(str)==undefined) str = 你的得分: + score +。是否重玩游戏? ;
else str = str + 你的得分: + score +。是否重玩游戏? ;
var isOK = window.confirm(str);
if(!isOK) {
document.location.reload();
window.close();
}
else{
document.location.reload();
}
}
更多精彩
赞助商链接