文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>一个javascript脚本写的俄罗斯方块

一个javascript脚本写的俄罗斯方块

时间:2007-02-17  来源:PHP爱好者

<!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>



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

function isBounds(sq){
for(var i=0; i<sq.length; i++){
if(sq.cols<0 || sq.cols>9 || sq.rows<0 || sq.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.rows>Max) Max = sq.rows;
}
}
else{
for(var i=0; i<sq.length; i++){
if(sq.cols>Max) Max = sq.cols;
}
}
return Max;
}
else{
var Min = 20;
if(name=="Rows"){
for(var i=0; i<sq.length; i++){
if(sq.rows<Min) Min = sq.rows;
}
}
else{
for(var i=0; i<sq.length; i++){
if(sq.cols<Min) Min = sq.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 + "#" + j);
obj.style.background = Color[0];
}
deleted = deleted + 1;
if(delLine.length>deleted)
{
if(delLine-delLine[i+1]>1) ReDrawLine(delLine[i+1],delLine,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.rows + "#" + to.cols);
if(obj.style.background != Color[0]){
for(var j=0; j<from.length; j++)
if(to.cols==from[j].cols&&to.rows==from[j].rows&&to.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.cols - CurSq[0].cols;
var dy = CurSq.rows - CurSq[0].rows;
NextSq = new Square(CurSq[0].cols-dy,CurSq[0].rows+dx,CurSq.color);
}
else
NextSq = new Square(CurSq.cols+x,CurSq.rows+y,CurSq.color);
}
if(isRotate) reNextSq();
if(MoveSquare(CurSq,NextSq) == false){
for(var i=0; i<CurSq.length; i++){
if((CurSq.rows==0&&CurSq.cols==Cols/2)||(CurSq.rows==0&&CurSq.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.rows = NextSq.rows + pos;
else
NextSq.cols = NextSq.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();
}
}


php爱好者站 http://www.phpfans.net c/vc/c++/java.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载