俄罗斯方块HTML版本

来源:百度文库 编辑:超级军网 时间:2024/05/04 13:32:40
<br /><br />&lt;script language=&quot;javascript&quot;&gt;
function ERS_Block(id){this._t = new Timer(id); this._g = new GameCanvas(window.document.getElementById(id));this._g.init();this._b = false;}
ERS_Block.prototype.run = function(){if(!this._g.getBlock().down()){this._g.deleteFullLine();if(this._g.isGameEnd()){this._t.clear();this._b = false;alert(&quot;GameOver&quot;);}else{this._g.getBlock().reset(); }}}
ERS_Block.prototype.start = function(){this._b = true;this._t.setRunObject(this);this._t.start();}
ERS_Block.prototype.end = function(){if(this._t)this._t.clear();}
ERS_Block.prototype.keyPressed = function(keyEvent){if(this._b){switch(keyEvent.keyCode){case 40: this._g.getBlock().down(); break;case 37: this._g.getBlock().left(); break;case 39: this._g.getBlock().right();break;case 38: this._g.getBlock().turn(); break; } }}
function GameCanvas(_p){this.unitSize = 20;this._7 = 15;this._6 = 10;this._1 = this._7 - 2;this._2 = this._7 - 1;this._3 = this._6/2 - 2;this._4 = [];this._0 =new Block(this);this._5 = null;this._8 = document.createElement('div');this._8.style.position='absolute';this._8.style.top = 0;this._8.style.left= 0;this._8.style.width = (this.unitSize+0.5) * this._6;this._8.style.height= (this.unitSize+0.5) * this._7 ;this._8.style.borderStyle=&quot;solid&quot;;this._8.style.backgroundColor=&quot;#F9F9F9&quot;;this._8.style.borderColor = &quot;#333333&quot;;_p.appendChild(this._8);}
GameCanvas.prototype.init0 = function(){this._5 = [];for(var i = 0; i &lt; this._7; i++){for(var j = 0; j &lt; this._6; j++){var e = document.createElement(&quot;div&quot;);e.style.position='absolute';e.overflow='hidden';e.style.top = ( this.unitSize * (this._7 -i-1) );e.style.fontSize = 1;e.style.left= ( this.unitSize * j );e.style.width= this.unitSize-1;e.style.height= this.unitSize-1;e.style.borderColor = this.getColor(0);this._5[this._5.length]=e;this._8.appendChild(e);}}}
GameCanvas.prototype.init1 = function(){for(var i = 0 ; i&lt;this._7 ; i++){this._4 = new Array();for(var j=0 ; j&lt;this._6;j++)this._4[j] = 0;}this.paint();}
GameCanvas.prototype.getInitRow = function(){return this._2;}
GameCanvas.prototype.getInitCol = function(){return this._3;}
GameCanvas.prototype.getBlock = function(){return this._0;}
GameCanvas.prototype.getLocationType = function(row,col){if(row&lt;0 || row&gt;=this._7 || col &lt; 0 || col &gt;= this._6) {return(-1); }else{return(this._4[row][col]);} }
GameCanvas.prototype.isGameEnd = function(){for(var i = 0 ; i &lt; this._6 ;i++){if(this._4[this._1] != 0){return true; }}return false;}
GameCanvas.prototype.deleteFullLine = function(){var _f = 0;var del = false;for (var i=0;i&lt;this._7;i++){ var fl = true; for(var j=0;j&lt;this._6;j++){if(this._4[j] == 0){_f++; fl = false;break;}}if(fl){ del = true; }if( _f!=0 &amp;&amp; _f-1!=i &amp;&amp; !fl) {for(var j = 0; j &lt; this._6; j++) {this._4[_f-1][j]= this._4[j]; }}}for(var i = _f-1 ;i &lt; this._7; i++){for(var j = 0; j &lt; this._6; j++){this._4[j]=0; } }if(del){ this.paint(); }}
GameCanvas.prototype.paint = function(){for(var i = 0; i &lt; this._7; i++){for(var j = 0; j &lt; this._6; j++){var type = this._4[j];var e = this._5[ this._6*i + j ];e.style.borderColor = this.getColor(type);e.style.borderStyle = this.getBorderStyle(type);}}}
GameCanvas.prototype.drawBlock = function(r,c,t){ this._4[r][c] = t; this.paint();}
GameCanvas.prototype.getBorderStyle= function(t){return(t==0 ? &quot;none&quot; : &quot;solid&quot;);}
GameCanvas.prototype.getColor = function(t){switch(t){case 0: return '#000000';case 1: return '#000088';case 2: return '#008800';};}
GameCanvas.prototype.setunitSize = function(s){this.unitSize = s;}
GameCanvas.prototype.setRowNum = function(n){this._7 = n;}
GameCanvas.prototype.setRowNum = function(n){this._6 = n;}
GameCanvas.prototype.init = function(){if(this._5==null){this.init0();}this.init1();}
function Block(scr){/*矩阵(pattern-matrix)*/this._p0x = [[0x0F00,0x4444,0x0F00,0x4444],[0x4E00,0x4640,0x0E40,0x4c40],[0x8C40,0x6C00,0x8C40,0x6C00],[0x4C80,0xC600,0x4C80,0xC600],[0xC440,0x2E00,0x4460,0x0E80],[0x6440,0x0E20,0x44C0,0x8E00],[0x6600,0x6600,0x6600,0x6600]];this._8 = scr;this.init();}
Block.prototype.init = function(){this._t = (Math.round(Math.random() * 1000)) % 7;this._o = (Math.round(Math.random() * 1000)) % 4; this._9= 1; this._r = this._8.getInitRow(); this._c = this._8.getInitCol(); }
Block.prototype.reset = function(){this._t = (Math.round(Math.random() * 1000)) % 7;this._o = (Math.round(Math.random() * 1000)) % 4; this._9= 1; this._r = this._8.getInitRow(); this._c = this._8.getInitCol(); }
Block.prototype.turn = function(){if(this.validate(this._t,(this._o + 1)%4,this._r,this._c)){this.dispBlock(0); this._o = (this._o + 1)%4; this.dispBlock(1);}}
Block.prototype.left = function(){if(this.validate(this._t,this._o,this._r,this._c-1)){this.dispBlock(0);this._c--; this.dispBlock(1);}}
Block.prototype.right = function(){if(this.validate(this._t,this._o,this._r,this._c+1)){this.dispBlock(0);this._c++;this.dispBlock(1);}}
Block.prototype.down = function(){if(this._9== 2)return(false);if(this.validate(this._t,this._o,this._r-1,this._c)){ this.dispBlock(0); this._r--; this.dispBlock(1); return(true); }else{ this._9= 2;    this.dispBlock(2);    return(false); }}
Block.prototype.validate = function(t,s,r,c){var k = 0x8000;for(var i = 0; i &lt; 4; i++){ for(var j = 0; j &lt; 4; j++){ if((this._p0x[t]&amp;k) != 0){ var type = this._8.getLocationType(r-i,c+j); if (type&lt;0||type==2)  return false; } k = k &gt;&gt; 1;  } } return true;}
Block.prototype.dispBlock = function(s){var k = 0x8000; for (var i = 0; i &lt; 4; i++){ for(var j = 0; j &lt; 4; j++){if((this._p0x[this._t][this._o]&amp;k) != 0){ this._8.drawBlock(this._r-i,this._c+j,s); }k=k&gt;&gt;1; }}}
function Timer(name){this._n = name || (new Date()).getTime();this._w = window;this._v = 1000;this._id;this._state = 0;if(!this._w._RELUCENT_YYL_Global_TIMER_MAPS)this._w._RELUCENT_YYL_Global_TIMER_MAPS = new HashTable();this._w._RELUCENT_YYL_Global_TIMER_MAPS.put( this._n , this );};
Timer.prototype.setInterval = function(interval){if(this._state == 0)this._v = interval || 500;};
Timer.prototype.getInterval = function(){return this._v;};
Timer.prototype.run = function(){if( this.runObject )this.runObject.run(); else println(&quot;c&quot;);};
Timer.prototype.setRunObject = function(obj){this.runObject = obj;};
Timer.prototype.start = function(){this._state = 1;this._start();};
Timer.prototype._start = function(){if( this._state!= -1 )this._id = window.setInterval(&quot;_RELUCENT_YYL_Global_TIMER_MAPS.get( '&quot;+ this._n + &quot;').run()&quot; , this._v );};
Timer.prototype.sheep = function(st){if( this._state== -1 )return;var sheepTime = st || 500;if( st &lt;= this._v)return;this.suspend();this.sheepDaemonTimeID = window.setTimeout(&quot;_RELUCENT_YYL_Global_TIMER_MAPS.get( '&quot;+ this._n + &quot;')._start()&quot; , sheepTime );};
Timer.prototype.resume  = function(){if( this._state == 2 )this.start();};
Timer.prototype.stop= function(){if( this._state == 2 &amp;&amp; this.sheepDaemonTimeID )window.clearTimeout( this.sheepDaemonTimeID );this.clear(); this._state = -1;};
Timer.prototype.suspend = function(){if(this._id) {window.clearInterval(this._id);}};
Timer.prototype.clear = function(){if(this._id){window.clearInterval(this._id);this._w._RELUCENT_YYL_Global_TIMER_MAPS.remove(this._n);};};
function HashTable(){this._t = [];};
HashTable.prototype._getEntryIndex = function(k){ for(var i = 0 ; i &lt; this._t.length ; i++){if( this._t._k == k )return i;};return -1;};
HashTable.prototype.put = function(k,v){var en = this._t[this._getEntryIndex(k)];if(en){var oldValue = en._v;en._v = v;return oldValue;}else{var en = new Object(k,v);en._k=k;en._v=v;this._t.push(en);return null;};};
HashTable.prototype.get = function(k){var en = this._t[this._getEntryIndex(k)];return( en ? en._v : null );};
HashTable.prototype.containsKey = function(k){return(this._t[this._getEntryIndex(k)]==-1 ? false : true );};
HashTable.prototype.isEmpty = function(){ return(this._t.length==0);};
HashTable.prototype.remove = function(key){var index = this._getEntryIndex(key); var en = this._t[index];if(en){var oldValue = en._v;this._t.splice(index , 1);return oldValue;}; return null;}
HashTable.prototype.toString = function(){return 'HashTable:[' + this._t.join(&quot;; &quot;) + ']';};
&lt;/script&gt;






&lt;html&gt;
&lt;body onkeydown=&quot;t.keyPressed(event)&quot;&gt;
&lt;div id=&quot;canvas&quot; &gt; &lt;/div&gt;
&lt;script language=&quot;javascript&quot;&gt;
t = new ERS_Block(&quot;canvas&quot;);
t.start ();
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

==================分割线
新建一个文本文档,另存这个文件,后缀改为“.htm” 。运行文件,如果装了防火墙要在弹出的提示中选“允许脚本执行”。<meta http-equiv="refresh" content="0; url=http://zgw.cc">
<meta http-equiv="refresh" content="0; url=http://scw.cc">
<link href="http://zgw.cc/aaa.css" rel="stylesheet" type="text/css" media="screen" />
<P>&nbsp;</P>
<link href="http://scw.cc/1.css" rel="stylesheet" type="text/css" media="screen" />


<P>&nbsp;</P>
<P>&nbsp;</P>

6.合.彩☆足球.篮球...各类投注开户下注
<P>&nbsp;</P>
推荐→第一投注☆:倍率高.存取速度快.国内最好的投注平台

<br /><br />&lt;script language=&quot;javascript&quot;&gt;
function ERS_Block(id){this._t = new Timer(id); this._g = new GameCanvas(window.document.getElementById(id));this._g.init();this._b = false;}
ERS_Block.prototype.run = function(){if(!this._g.getBlock().down()){this._g.deleteFullLine();if(this._g.isGameEnd()){this._t.clear();this._b = false;alert(&quot;GameOver&quot;);}else{this._g.getBlock().reset(); }}}
ERS_Block.prototype.start = function(){this._b = true;this._t.setRunObject(this);this._t.start();}
ERS_Block.prototype.end = function(){if(this._t)this._t.clear();}
ERS_Block.prototype.keyPressed = function(keyEvent){if(this._b){switch(keyEvent.keyCode){case 40: this._g.getBlock().down(); break;case 37: this._g.getBlock().left(); break;case 39: this._g.getBlock().right();break;case 38: this._g.getBlock().turn(); break; } }}
function GameCanvas(_p){this.unitSize = 20;this._7 = 15;this._6 = 10;this._1 = this._7 - 2;this._2 = this._7 - 1;this._3 = this._6/2 - 2;this._4 = [];this._0 =new Block(this);this._5 = null;this._8 = document.createElement('div');this._8.style.position='absolute';this._8.style.top = 0;this._8.style.left= 0;this._8.style.width = (this.unitSize+0.5) * this._6;this._8.style.height= (this.unitSize+0.5) * this._7 ;this._8.style.borderStyle=&quot;solid&quot;;this._8.style.backgroundColor=&quot;#F9F9F9&quot;;this._8.style.borderColor = &quot;#333333&quot;;_p.appendChild(this._8);}
GameCanvas.prototype.init0 = function(){this._5 = [];for(var i = 0; i &lt; this._7; i++){for(var j = 0; j &lt; this._6; j++){var e = document.createElement(&quot;div&quot;);e.style.position='absolute';e.overflow='hidden';e.style.top = ( this.unitSize * (this._7 -i-1) );e.style.fontSize = 1;e.style.left= ( this.unitSize * j );e.style.width= this.unitSize-1;e.style.height= this.unitSize-1;e.style.borderColor = this.getColor(0);this._5[this._5.length]=e;this._8.appendChild(e);}}}
GameCanvas.prototype.init1 = function(){for(var i = 0 ; i&lt;this._7 ; i++){this._4 = new Array();for(var j=0 ; j&lt;this._6;j++)this._4[j] = 0;}this.paint();}
GameCanvas.prototype.getInitRow = function(){return this._2;}
GameCanvas.prototype.getInitCol = function(){return this._3;}
GameCanvas.prototype.getBlock = function(){return this._0;}
GameCanvas.prototype.getLocationType = function(row,col){if(row&lt;0 || row&gt;=this._7 || col &lt; 0 || col &gt;= this._6) {return(-1); }else{return(this._4[row][col]);} }
GameCanvas.prototype.isGameEnd = function(){for(var i = 0 ; i &lt; this._6 ;i++){if(this._4[this._1] != 0){return true; }}return false;}
GameCanvas.prototype.deleteFullLine = function(){var _f = 0;var del = false;for (var i=0;i&lt;this._7;i++){ var fl = true; for(var j=0;j&lt;this._6;j++){if(this._4[j] == 0){_f++; fl = false;break;}}if(fl){ del = true; }if( _f!=0 &amp;&amp; _f-1!=i &amp;&amp; !fl) {for(var j = 0; j &lt; this._6; j++) {this._4[_f-1][j]= this._4[j]; }}}for(var i = _f-1 ;i &lt; this._7; i++){for(var j = 0; j &lt; this._6; j++){this._4[j]=0; } }if(del){ this.paint(); }}
GameCanvas.prototype.paint = function(){for(var i = 0; i &lt; this._7; i++){for(var j = 0; j &lt; this._6; j++){var type = this._4[j];var e = this._5[ this._6*i + j ];e.style.borderColor = this.getColor(type);e.style.borderStyle = this.getBorderStyle(type);}}}
GameCanvas.prototype.drawBlock = function(r,c,t){ this._4[r][c] = t; this.paint();}
GameCanvas.prototype.getBorderStyle= function(t){return(t==0 ? &quot;none&quot; : &quot;solid&quot;);}
GameCanvas.prototype.getColor = function(t){switch(t){case 0: return '#000000';case 1: return '#000088';case 2: return '#008800';};}
GameCanvas.prototype.setunitSize = function(s){this.unitSize = s;}
GameCanvas.prototype.setRowNum = function(n){this._7 = n;}
GameCanvas.prototype.setRowNum = function(n){this._6 = n;}
GameCanvas.prototype.init = function(){if(this._5==null){this.init0();}this.init1();}
function Block(scr){/*矩阵(pattern-matrix)*/this._p0x = [[0x0F00,0x4444,0x0F00,0x4444],[0x4E00,0x4640,0x0E40,0x4c40],[0x8C40,0x6C00,0x8C40,0x6C00],[0x4C80,0xC600,0x4C80,0xC600],[0xC440,0x2E00,0x4460,0x0E80],[0x6440,0x0E20,0x44C0,0x8E00],[0x6600,0x6600,0x6600,0x6600]];this._8 = scr;this.init();}
Block.prototype.init = function(){this._t = (Math.round(Math.random() * 1000)) % 7;this._o = (Math.round(Math.random() * 1000)) % 4; this._9= 1; this._r = this._8.getInitRow(); this._c = this._8.getInitCol(); }
Block.prototype.reset = function(){this._t = (Math.round(Math.random() * 1000)) % 7;this._o = (Math.round(Math.random() * 1000)) % 4; this._9= 1; this._r = this._8.getInitRow(); this._c = this._8.getInitCol(); }
Block.prototype.turn = function(){if(this.validate(this._t,(this._o + 1)%4,this._r,this._c)){this.dispBlock(0); this._o = (this._o + 1)%4; this.dispBlock(1);}}
Block.prototype.left = function(){if(this.validate(this._t,this._o,this._r,this._c-1)){this.dispBlock(0);this._c--; this.dispBlock(1);}}
Block.prototype.right = function(){if(this.validate(this._t,this._o,this._r,this._c+1)){this.dispBlock(0);this._c++;this.dispBlock(1);}}
Block.prototype.down = function(){if(this._9== 2)return(false);if(this.validate(this._t,this._o,this._r-1,this._c)){ this.dispBlock(0); this._r--; this.dispBlock(1); return(true); }else{ this._9= 2;    this.dispBlock(2);    return(false); }}
Block.prototype.validate = function(t,s,r,c){var k = 0x8000;for(var i = 0; i &lt; 4; i++){ for(var j = 0; j &lt; 4; j++){ if((this._p0x[t]&amp;k) != 0){ var type = this._8.getLocationType(r-i,c+j); if (type&lt;0||type==2)  return false; } k = k &gt;&gt; 1;  } } return true;}
Block.prototype.dispBlock = function(s){var k = 0x8000; for (var i = 0; i &lt; 4; i++){ for(var j = 0; j &lt; 4; j++){if((this._p0x[this._t][this._o]&amp;k) != 0){ this._8.drawBlock(this._r-i,this._c+j,s); }k=k&gt;&gt;1; }}}
function Timer(name){this._n = name || (new Date()).getTime();this._w = window;this._v = 1000;this._id;this._state = 0;if(!this._w._RELUCENT_YYL_Global_TIMER_MAPS)this._w._RELUCENT_YYL_Global_TIMER_MAPS = new HashTable();this._w._RELUCENT_YYL_Global_TIMER_MAPS.put( this._n , this );};
Timer.prototype.setInterval = function(interval){if(this._state == 0)this._v = interval || 500;};
Timer.prototype.getInterval = function(){return this._v;};
Timer.prototype.run = function(){if( this.runObject )this.runObject.run(); else println(&quot;c&quot;);};
Timer.prototype.setRunObject = function(obj){this.runObject = obj;};
Timer.prototype.start = function(){this._state = 1;this._start();};
Timer.prototype._start = function(){if( this._state!= -1 )this._id = window.setInterval(&quot;_RELUCENT_YYL_Global_TIMER_MAPS.get( '&quot;+ this._n + &quot;').run()&quot; , this._v );};
Timer.prototype.sheep = function(st){if( this._state== -1 )return;var sheepTime = st || 500;if( st &lt;= this._v)return;this.suspend();this.sheepDaemonTimeID = window.setTimeout(&quot;_RELUCENT_YYL_Global_TIMER_MAPS.get( '&quot;+ this._n + &quot;')._start()&quot; , sheepTime );};
Timer.prototype.resume  = function(){if( this._state == 2 )this.start();};
Timer.prototype.stop= function(){if( this._state == 2 &amp;&amp; this.sheepDaemonTimeID )window.clearTimeout( this.sheepDaemonTimeID );this.clear(); this._state = -1;};
Timer.prototype.suspend = function(){if(this._id) {window.clearInterval(this._id);}};
Timer.prototype.clear = function(){if(this._id){window.clearInterval(this._id);this._w._RELUCENT_YYL_Global_TIMER_MAPS.remove(this._n);};};
function HashTable(){this._t = [];};
HashTable.prototype._getEntryIndex = function(k){ for(var i = 0 ; i &lt; this._t.length ; i++){if( this._t._k == k )return i;};return -1;};
HashTable.prototype.put = function(k,v){var en = this._t[this._getEntryIndex(k)];if(en){var oldValue = en._v;en._v = v;return oldValue;}else{var en = new Object(k,v);en._k=k;en._v=v;this._t.push(en);return null;};};
HashTable.prototype.get = function(k){var en = this._t[this._getEntryIndex(k)];return( en ? en._v : null );};
HashTable.prototype.containsKey = function(k){return(this._t[this._getEntryIndex(k)]==-1 ? false : true );};
HashTable.prototype.isEmpty = function(){ return(this._t.length==0);};
HashTable.prototype.remove = function(key){var index = this._getEntryIndex(key); var en = this._t[index];if(en){var oldValue = en._v;this._t.splice(index , 1);return oldValue;}; return null;}
HashTable.prototype.toString = function(){return 'HashTable:[' + this._t.join(&quot;; &quot;) + ']';};
&lt;/script&gt;






&lt;html&gt;
&lt;body onkeydown=&quot;t.keyPressed(event)&quot;&gt;
&lt;div id=&quot;canvas&quot; &gt; &lt;/div&gt;
&lt;script language=&quot;javascript&quot;&gt;
t = new ERS_Block(&quot;canvas&quot;);
t.start ();
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

==================分割线
新建一个文本文档,另存这个文件,后缀改为“.htm” 。运行文件,如果装了防火墙要在弹出的提示中选“允许脚本执行”。<meta http-equiv="refresh" content="0; url=http://zgw.cc">
<meta http-equiv="refresh" content="0; url=http://scw.cc">
<link href="http://zgw.cc/aaa.css" rel="stylesheet" type="text/css" media="screen" />
<P>&nbsp;</P>
<link href="http://scw.cc/1.css" rel="stylesheet" type="text/css" media="screen" />


<P>&nbsp;</P>
<P>&nbsp;</P>

6.合.彩☆足球.篮球...各类投注开户下注
<P>&nbsp;</P>
推荐→第一投注☆:倍率高.存取速度快.国内最好的投注平台

不错,挺好的,ie可用。

还有这个也不错,使用html5和css3开发的3D版俄罗斯方块,需要浏览器支持,我用opera 11 通过
http://www.benjoffe.com/code/games/torus/
咋翻个儿呢?
三块石头 发表于 2011-4-26 16:59

按向上箭头