分享
 
 
 

JavaScript 寫遊戲 : 俄羅斯方塊

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

應該看得懂吧?

沒時間寫注釋。。。:(

這個差不多美的啦!只要向Block裏面添加或修改就能改變方塊的形狀。:)

<!--

http://dwin.net

http://dewin.tk

Copyright(c) 1998-2004 dewin all rights reserved

Start 2002-09-20 14:45

Finish 2002-10-31 23:50

Last Edit 2003-08-14 01:25

-->

<body>

<script>

var Rows = 17;

var Cells = 10;

var ObjW = ObjH = 20;

var Scores= 0;

var Marks = [100,400,900,1600];

var BaseBW = 5;

var BaseBGColor = 'white';

var BlockBW = 2;

var BlockBS = 'inset';

var BlockBC = 'orange';

var GlowColor = '#ffff00';

var GlowStrength = 5;

var oSpeed = 151;

//方塊數據

Block = [];

Block[0]=[

' ',

' 1',

'111']

Block[0].change = 4;

Block[0].color = 'red';

Block[1]=[

' ',

'1 ',

'111']

Block[1].change = 4;

Block[1].color = 'green';

Block[2]=[

' ',

' 1 ',

'111']

Block[2].change = 4;

Block[2].color = 'orange';

Block[3]=[

' 11',

'11 ']

Block[3].change = 2;

Block[3].color = 'fuchsia';

Block[4]=[

'11 ',

' 11']

Block[4].change = 2;

Block[4].color = 'yellow';

Block[5]=[

'1111']

Block[5].change = 2;

Block[5].color = 'lime';

Block[6]=[

'11',

'11']

Block[6].change = 1;

Block[6].color = 'blue';

Block[7]=[

'1']

Block[7].change = 1;

Block[7].color = 'gold';

Block[8]=[

' ',

'1 1',

'111']

Block[8].change = 4;

Block[8].color = 'pink';

Block[9]=[

'111',

' 1 ',

' 1 ']

Block[9].change = 4;

Block[9].color = 'black';

//方塊准備下落

function ReadyForDown(obj1,obj2){

obj1.k = parseInt(Math.random()*Block.length);

if(!NewBlock[obj1.k].length){

switch(Block[obj1.k].change){

case 1:Change0(obj1.k);break;

case 2:Change0(obj1.k);Change1(obj1.k);break;

case 4:Change0(obj1.k);Change1(obj1.k);Change2(obj1.k);break;

}

}

obj1.Change = parseInt(Math.random()*Block[obj1.k].change);

obj2.innerHTML = NewBlock[obj1.k][obj1.Change].iHtml;

obj1.GapX = parseInt(NewBlock[obj1.k][obj1.Change][0].length/2);

CheckObjGapY(obj1,NewBlock[obj1.k][obj1.Change].length);

obj1.X = parseInt(Cells/2);

obj1.Y = 0;

}

function CheckObjGapY(obj,k){

if(!NewBlock[obj.k][obj.Change][k-1].join('').match(/\d+/)){

obj.GapY = 1-k;

CheckObjGapY(obj,k-1);

}

else obj.GapY = -k;

}

//改變方塊形狀

function ChangeBlock(){

if(Block[Obj1.k].change > 1){

Obj1.TestChange = (Obj1.Change+1 >= Block[Obj1.k].change)?0:Obj1.Change+1;

Obj1.TestGapX = parseInt(NewBlock[Obj1.k][Obj1.TestChange][0].length/2);

Obj1.TestGapY = -NewBlock[Obj1.k][Obj1.TestChange].length;

Obj1.TestL = Obj1.X-Obj1.TestGapX;

Obj1.TestT = Obj1.Y+Obj1.TestGapY;

var k1 = k2 = Areas;

for(var i=Obj1.TestL;i>=0 && i>Obj1.TestL-NewBlock[Obj1.k][Obj1.TestChange].lg;i--){if((k1=CheckChangeBlock(i))!=Areas)break}

for(var i=Obj1.TestL+1;i<Cells && i<Obj1.TestL+NewBlock[Obj1.k][Obj1.TestChange].lg;i++){if((k2=CheckChangeBlock(i))!=Areas)break}

if(k1 == k2) return;

Obj1.L = (Math.abs(Obj1.TestL-k1)>Math.abs(Obj1.TestL-k2))?k2:k1;

Obj1.T = Obj1.TestT;

Obj1.GapX = Obj1.TestGapX;

Obj1.GapY = Obj1.TestGapY;

Obj1.X = Obj1.L+Obj1.GapX;

Obj1.Y = Obj1.T-Obj1.GapY;

Obj1.Change = Obj1.TestChange;

SkinDiv.style.left = Obj1.L*ObjW;

SkinDiv.style.top = Obj1.T*ObjH;

SkinDiv.innerHTML = NewBlock[Obj1.k][Obj1.Change].iHtml;

}

}

檢測改變後,方塊相對於左邊的值。

function CheckChangeBlock(lefts){

for(var y=0;y<NewBlock[Obj1.k][Obj1.TestChange].length;y++){

for(var x=0;x<NewBlock[Obj1.k][Obj1.TestChange][0].length;x++){

if(NewBlock[Obj1.k][Obj1.TestChange][y][x]==1){

try{if(Map[Obj1.TestT+y][lefts+x] != ' ') return Areas}

catch(e){}

}

}

}

return lefts;

}

//方向控制

function Dir(xx,yy){

var special = false

if(NewBlock[Obj1.k][Obj1.Change].length==1 && NewBlock[Obj1.k][Obj1.Change][0]==1)

for(var y=Obj1.T+1;y<Rows-1;y++)

if(Map[y+yy][Obj1.L]==" "){

special = true

break;

}

for(var y=NewBlock[Obj1.k][Obj1.Change].length-1;y>=0;y--){

for(var x=NewBlock[Obj1.k][Obj1.Change][0].length-1;x>=0;x--){

if(NewBlock[Obj1.k][Obj1.Change][y][x]==1){

try{

if(yy==0 && (Obj1.L+x+xx<0 || Obj1.L+x+xx>=Cells || Map[Obj1.T+y][Obj1.L+x+xx]==1)) return;//橫

if(Obj1.T+y+yy>=Rows || (!special && Map[Obj1.T+y+yy][Obj1.L+x+xx]==1)) return CreateBlockInMap();//豎

}

catch(e){}

}

}

}

SkinDiv.style.left = (Obj1.L=(Obj1.X+=xx)-Obj1.GapX)*ObjW;

SkinDiv.style.top = (Obj1.T=(Obj1.Y+=yy)+Obj1.GapY)*ObjH;

}

//開始創造地圖啦。

function CreateBlockInMap(){

for(var y=0;y<NewBlock[Obj1.k][Obj1.Change].length;y++){

for(var x=0;x<NewBlock[Obj1.k][Obj1.Change][0].length;x++){

try{

if(NewBlock[Obj1.k][Obj1.Change][y][x]==1){

Map[Obj1.T+y][Obj1.L+x] = NewBlock[Obj1.k][Obj1.Change][y][x];

BaseDiv.childNodes[Areas-(Obj1.T+y)*Cells+Obj1.L+x].style.background = Block[Obj1.k].color;

BaseDiv.childNodes[Areas-(Obj1.T+y)*Cells+Obj1.L+x].style.visibility = 'visible';

}

}

catch(e){return GameOver()}

}

}

CheckBlockAppear();

ReadyForDown(Obj2,ReadyBlockDiv);

}

function CheckBlockAppear(){

clearInterval(GoTime2);

GoTime2=0

var NewMap = [];

var iHtml = '';

var k = Rows-1;

var kk = -1;

for(var y=Rows-1;y>=0;y--){

if(Map[y].join('') != FullString){NewMap[k--] = Map[y]}

else{

kk++;

for(var x=0;x<Cells;x++){

BaseDiv.childNodes[Areas-(y+kk)*Cells].removeNode(true);

iHtml += "<span style='width:"+ObjW+";height:"+ObjH+";border:"+BlockBW+" "+BlockBS+" "+BlockBC+";overflow:hidden;visibility:hidden;'></span>";

}

}

}

for(;k>=0;k--){

NewMap[k] = [];

for(var x=0;x<Cells;x++){

NewMap[k][x] = ' ';

}

}

if(kk!=-1) ScoreDiv.innerHTML = (Scores+=Marks[kk]);//Cal the Scores

for(var i=0;i<Rows;i++){Map[i]=NewMap[i]}

BaseDiv.innerHTML += iHtml;

for(i in Obj2){Obj1[i] = Obj2[i]}//Obj2 => Obj1

SkinDiv.innerHTML = NewBlock[Obj1.k][Obj1.Change].iHtml;

SkinDiv.style.left = (Obj1.L = Obj1.X-Obj1.GapX)*ObjW;

SkinDiv.style.top = (Obj1.T = Obj1.GapY)*ObjH;

}

function GameOver(){

if(confirm('Game Over , ReStart ?')) window.location.reload();

else window.close();

}

//初始化方塊的變形數組

function Change0(k){

NewBlock[k] = [];

NewBlock[k][0] = [];

NewBlock[k][0].iHtml = '';

for(var y=0;y<Block[k].length;y++){

NewBlock[k][0][y] = [];

for(var x=0;x<Block[k][0].length;x++){

NewBlock[k][0].iHtml += ((NewBlock[k][0][y][x]=Block[k][y].charAt(x)) == 1)?BlockHtml(x,y,k):"";

}

}

CheckBlockLong(k,0);

}

function Change1(k){

NewBlock[k][1] = [];

NewBlock[k][1].iHtml = '';

for(var y=0;y<Block[k][0].length;y++){

NewBlock[k][1][y] = [];

for(var x=0;x<Block[k].length;x++){

NewBlock[k][1].iHtml += ((NewBlock[k][1][y][x]=Block[k][Block[k].length-x-1].charAt(y)) == 1)?BlockHtml(x,y,k):"";

}

}

CheckBlockLong(k,1);

}

function Change2(k){

NewBlock[k][2] = [];

NewBlock[k][3] = [];

NewBlock[k][2].iHtml = '';

NewBlock[k][3].iHtml = '';

for(var y=0;y<Block[k].length;y++){

NewBlock[k][2][y] = [];

for(var x=0;x<Block[k][0].length;x++){

NewBlock[k][2].iHtml += ((NewBlock[k][2][y][x]=Block[k][Block[k].length-y-1].charAt(Block[k][0].length-x-1)) == 1)?BlockHtml(x,y,k):"";

}

}

for(var y=0;y<Block[k][0].length;y++){

NewBlock[k][3][y] = [];

for(var x=0;x<Block[k].length;x++){

NewBlock[k][3].iHtml += ((NewBlock[k][3][y][x]=Block[k][x].charAt(Block[k][0].length-y-1)) == 1)?BlockHtml(x,y,k):"";

}

}

CheckBlockLong(k,2);

CheckBlockLong(k,3);

}

function BlockHtml(x,y,k){

return "<span style='position:absolute;left:"+x*ObjW+";top:"+y*ObjH+";width:"+ObjW+";height:"+ObjH+";background:"+Block[k].color+";border:"+BlockBW+" "+BlockBS+" "+BlockBC+";overflow:hidden;'></span>"

}

function CheckBlockLong(k,i){

for(var y=0;y<NewBlock[k][i].length;y++){

NewBlock[k][i][y].lg = (NewBlock[k][i][y].toString().match(/\d+/)==null)?0:NewBlock[k][i][y].join('').match(/\d+/).toString().length;

}

NewBlock[k][i].lg = 1;

for(var y=0;y<NewBlock[k][i].length;y++){

NewBlock[k][i].lg = Math.max(NewBlock[k][i][y].lg,NewBlock[k][i].lg);

}

}

function document.onkeydown(){

switch(event.keyCode){

case 34:clearInterval(GoTime);oSpeed+=10;if(GoTime>0){Dir(0,1);GoTime=setInterval('Dir(0,1)',oSpeed)};break;//page up

case 33:if(oSpeed-10>0){clearInterval(GoTime);oSpeed-=5};if(GoTime>0){Dir(0,1);GoTime=setInterval('Dir(0,1)',oSpeed);};break;//page down

case 38:if(GoTime>0)GoTime2=setInterval("Dir(0,1)",1);break;//up

case 37:CheckKeyDown(-1,0,150,20);break;//left

case 39:CheckKeyDown(1,0,150,20);break;//right

case 40:CheckKeyDown(0,1,150,20);break;//down

case 96:if(GoTime>0)ChangeBlock();break;//0

case 83:CheckStart();break;

}

}

function document.onkeyup(){

DoKeyDown=false

}

function CheckKeyDown(x,y,t1,t2){

if(GoTime>0 && !DoKeyDown){

Dir(x,y)

DoKeyDown=true

setTimeout("if(DoKeyDown)KeyDownAgain("+x+","+y+","+t2+");",t1)

}

}

function KeyDownAgain(x,y,t2){

if(GoTime>0 && DoKeyDown){

Dir(x,y)

setTimeout("KeyDownAgain("+x+","+y+","+t2+")",t2)

}

}

function CheckStart(){

if(GoTime == -1){

CheckBlockAppear();

GoTime = 0;

ReadyForDown(Obj2,ReadyBlockDiv);

}

if(!GoTime){

Dir(0,1);

GoTime = setInterval('Dir(0,1)',oSpeed);

}

else{

clearInterval(GoTime);

GoTime = 0;

}

}

var Map = [];

var NewBlock = [];

var Areas = 0;

var GoTime = -1, GoTime2;

var FullString = '';

var Obj1 = new Object();

var Obj2 = new Object();

var DoKeyDown=false

window.focus();

var iHtml = '';

var BaseWidth = Cells*ObjW;

var BaseHeight = Rows*ObjH;

var MainMapWidth = 2*BaseBW+Cells*ObjW;

var MainMapHeight = 2*BaseBW+Rows*ObjH;

document.body.innerHTML += "<font color=red>press \"s\" button to Star or Stop<br>press \"0\" button to Change Quadrel into Another Shape<br>press \"Page Up\" to Speed up<br>press \"Page Down\" to Speed down</font><span style='position:absolute;left:"+(document.body.clientWidth-MainMapWidth)/2+";top:"+(document.body.clientHeight-MainMapHeight)/2+";width:"+MainMapWidth+";height:"+MainMapHeight+";overflow:visible;border:"+BaseBW+" inset #0000CC;'><span style='position:absolute;left:0;top:0;width:"+BaseWidth+";height:"+BaseHeight+";filter:glow(color="+GlowColor+",strength="+GlowStrength+");'><span id='BaseDiv' style='position:absolute;left:0;top:0;width:"+BaseWidth+";height:"+BaseHeight+";filter:flipv;'></span></span><span id=SkinDiv style='position:absolute;'></span><span id='ReadyBlockDiv' style='position:absolute;left:"+MainMapWidth+";top:0;'></span><span id=ScoreDiv style='position:absolute;left:"+(MainMapWidth+50)+";top:"+(MainMapHeight-20)+";'>"+Scores+"</span></span>";

for(var i=0,k=Cells*Rows;i<k;i++){iHtml += "<span style='width:"+ObjW+";height:"+ObjH+";border:"+BlockBW+" "+BlockBS+" "+BlockBC+";overflow:hidden;visibility:hidden;'></span>"}

for(var i=0;i<Block.length;i++) NewBlock[i]=[]

FullString = new Array(Cells+1).join("1")

for(var y=0;y<Rows;y++){

Map[y] = [];

for(var x=0;x<Cells;x++) Map[y][x]=' ';

}

Areas = Cells*(Rows-1);

BaseDiv.innerHTML = iHtml;

ReadyForDown(Obj2,ReadyBlockDiv);

</script>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
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- 王朝網路 版權所有