分享
 
 
 

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

王朝html/css/js·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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

得分: &nbsp;&nbsp;<SPAN ID="gScore"></SPAN>&nbsp;&nbsp;<p>

级别: &nbsp;&nbsp;<SPAN ID="gLevel"></SPAN>&nbsp;&nbsp;<p>

行数: &nbsp;&nbsp;<SPAN ID="gLine"></SPAN>&nbsp;&nbsp;<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>帮&nbsp;&nbsp;&nbsp;&nbsp;助</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] + "\">&nbsp;</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();

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有