JS to achieve Tetris Games sharing _javascript Skills

Source: Internet
Author: User
Tags eval pow
Copy Code code as follows:

<!doctype html>Body {background: #000; font:25px/25px;}
#box {float:left;width:252px;border: #999 20px ridge;color: #9f9; text-shadow:2px 3px 1px #0f0;}
#info {float:left;color: #cfc;p adding:24px;}
#next {padding:8px;width:105px;color: #9f9; text-shadow:2px 3px 1px #0f0;}
</style><div id= "box" ></div><div id= "info" >next:<div id= "NEXT" ></div><div "text" > </div></div>
<script type= "Text/javascript" >
var map=eval ("[" +array). Join ("0x801,") + "0xfff]");
var tatris=[[0x6600],[0x2222,0xf00],[0xc600,0x2640],[0x6c00,0x4620],[0x4460,0x2e0,0x6220,0x740],[0x2260,0xe20, 0X6440,0X4700],[0X2620,0X720,0X2320,0X2700]];
var char={x: "\u3000", S: "\u25a0", T: "\U25A1"};
var keycom={"": "Rotate (1)", "the": "Down ()", "Panax Notoginseng": "Move (2,1)", "": "Move (0.5,-1)", "a": "0;pause=!pause"};
var dia, POS, bak, run, Next, Pause=false, info={speed:1,lines:0,score:0};
function Start () {
DIA=NEXT.D;
BAK=POS={FK:[],Y:0,X:4,S:NEXT.S};
Nextdia ();
document.getElementById ("Next"). Innerhtml= (next.d[next.s%next.d.length]|0x10000). toString (2). Slice (-16). Replace (/..../g, "$&<br/>"). Replace (/1/G,CHAR.T). replace (/0/g,char.x);
document.getElementById ("text"). Innerhtml= "SCORE: +info.score+" <br/><br/>lines: "+info.lines+" < Br/><br/>speed: "+info.speed;
Rotate (0);
Run=setinterval ("pause| | Down () ", ~ ~ (Math.pow (1.3,12-info.speed) *30+20));
}
function over () {
Document.onkeydown=null;
Alert ("GAME over");
}
function Nextdia () {
Next={d:tatris[~~ (Math.random () *7)],s:~~ (Math.random () *4)};
}
function Update (t) {
Bak={fk:pos.fk.slice (0), Y:POS.Y,X:POS.X,S:POS.S};
if (t) return;
for (Var i=0,a2= ""; i<22; i++)
A2+=map[i].tostring (2). Slice (1,-1) + "<br/>";
for (var i=0,n; i<4; i++)
if (/([^0]+)/.test (bak.fk[i].tostring (2). Replace (/1/G,CHAR.T)))
A2=a2.substr (0,n= (bak.y+i+1) *15-regexp.$_.length-4) +regexp.$1+a2.slice (n+regexp.$1.length);
document.getElementById ("box"). Innerhtml=a2.replace (/1/G,CHAR.S). replace (/0/g,char.x);
}
The function is () {
for (var i=0; i<4; i++)
if ((Pos.fk[i]&map[pos.y+i])!=0) return pos=bak;
}
function Rotate (r) {
var f=dia[pos.s= (pos.s+r)%dia.length];
for (var i=0; i<4; i++)
Pos.fk[i]= (f>> (12-i*4) &15) <<pos.x;
Update (IS ());
}
function down () {
++pos.y;
if (is ()) {
For (var i=0, r=0; i<4 && pos.y+i<22; i++)
if ((Map[pos.y+i]|=pos.fk[i]) ==0xfff) {
Map.splice (pos.y+i,1), Map.unshift (0x801);
++info.lines%20==0 && info.speed++, r++;
}
Clearinterval (run);
if (map[1]!=0x801) return over ();
Info.score+=~~ (Math.pow (r,1.5) *10) +2;
Start ();
}
Update ();
}
function Move (t,k) {
Pos.x+=k;
for (var i=0; i<4; i++)
pos.fk[i]*=t;
Update (IS ());
}
Document.onkeydown=function (e) {
Eval ("pause| |" +keycom[(e?e:event). KeyCode]);
};
Nextdia ();
Start ();
</script></body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.