JS version-Tetris

Source: Internet
Author: User

<HTML> <Meta http-equiv = Content-Type content = "text/html; charset = gb2312">
<Meta content = "jscript simple RIS ris" name = title>
<Style>
Body {Background: black; margin: 0px}
TD {width: 20px; Height: 20px}
# Block_div {Z-INDEX: 1; width: 80px; position: absolute}
# Table_div {width: 320px; position: absolute}
# Nblock_div {Z-INDEX: 2; Font: 48px system; color: red; position: absolute}
# Title_div {font-size: 22px; left: 326px; color: White; position: absolute; top: 7px}
# Infobar_div {left: pixel PX; position: absolute; top: 416px}
# Infobar2_div {font-size: 14px; left: pixel; color: White; position: absolute; top: 480px}
# Infobar2_div A {color: #99 CCFF; text-Decoration: None}
</Style>
<Script language = JavaScript id = clienteventhandlersjs>
<! --
If (document. All ){
VaR n_width = 800;
VaR n_height = 600;
VaR n_left = math. Round (screen. width/2)-n_width/2;
VaR n_top = math. Round (screen. Height/2)-n_height/2;
VaR n_incstep = 20;
VaR curblcok, nextblock;
VaR arr_curblock = new array (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
VaR curx, Cury;
VaR speed = 1;
VaR maxspeed = 9;
VaR clr_per_line = 18;
VaR pause = 0;
VaR gameover = 0;
VaR colors = new array ("#999999", "# 0000ff", "#80 ffff", "#80ff80", "# FFFF00", "# ff8000 ", "# ff00ff", "# ff0000"
);
VaR CID;
VaR NCID;
VaR blocks = new array ("tt_o1", "tt_t2", "tt_z1", "tt_s1", "tt_l1", "tt_j1", "tt_i2 ");
VaR bid;
VaR killedlines = 0;

VaR tt_o1 = new array );
VaR tt_o2 = new array );
VaR tt_o3 = new array );
VaR tt_o4 = new array );

VaR tt_t1 = new array );
VaR tt_t2 = new array );
VaR tt_t3 = new array );
VaR tt_t4 = new array );

VaR tt_z1 = new array );
VaR tt_z2 = new array );
VaR tt_z3 = new array );
VaR tt_z4 = new array );

VaR tt_s1 = new array (, 0 );
VaR tt_s2 = new array );
VaR tt_s3 = new array );
VaR tt_s4 = new array );

VaR tt_l1 = new array );
VaR tt_l2 = new array );
VaR tt_l3 = new array );
VaR tt_l4 = new array );

VaR tt_j1 = new array );
VaR tt_j2 = new array );
VaR tt_j3 = new array );
VaR tt_j4 = new array );

VaR tt_i1 = new array );
VaR tt_i2 = new array );
VaR tt_i3 = new array );
VaR tt_i4 = new array );

VaR table = new array (
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
}
Else
Alert ("You need ie4 + to play Tetris! ")

Function dimension2 (row, Col, num ){
VaR I = row * num + Col;
Return (this [I]);
}
Array. Prototype. getd = dimension2;

Function showblock (X, Y, block_type, color ){
For (VAR I = 0; I <block_tbl.rows.length; I ++ ){
For (var j = 0; j <block_tbl.rows (I). cells. length; j ++ ){
VaR D2 = I * 4 + J;
If (block_type [D2] = 1 ){
Block_tbl.rows (I). cells (j). style. Background = color;
}
Else {
Block_tbl.rows (I). cells (j). style. Background = "";
}
}
}
Block_div.style.pixelleft = X;
Block_div.style.pixeltop = y;
}

Function showblock1 (X, Y, block_type, color, obj_tabid, obj_divid ){
For (VAR I = 0; I <obj_tabid.rows.length; I ++ ){
For (var j = 0; j <obj_tabid.rows (I). cells. length; j ++ ){
VaR D2 = I * 4 + J;
If (block_type [D2] = 1 ){
Obj_tabid.rows (I). cells (j). style. Background = color;
}
Else {
Obj_tabid.rows (I). cells (j). style. Background = "";
}
}
}
Obj_divid.style.pixelleft = X;
Obj_divid.style.pixeltop = y;
}

Function Change (INC ){
VaR type = curblock. substr (0, 4 );
VaR num = curblock. substr (curblock. Length-1 );
Num = parseint (Num) + Inc;
If (Num> 4 | num <1) num-= 4 * Inc;
Type + = num;
Eval ("arr_tmp =" + Type + ";");
If (canmove (curx, Cury, arr_tmp )){
Curblock = type;
Eval ("arr_curblock =" + curblock + ";");
Eval ("showblock (block_div.style.pixelleft, block_div.style.pixeltop," + curblock + ", colors [" + CID + "]);");
}
}

Function canmove (X, Y, block ){
For (I = 0; I <4; I ++ ){
For (j = 0; j <4; j ++ ){
If (Block. getd (I, j, 4) & table. getd (y/n_incstep + I, X/n_incstep + J, 16) return false;
}
}
Return true;
}

Function newblock (){
Curblock = nextblock;
Cid = NCID;
Bid = math. Round (math. Random () * (blocks. Length-1 ));
NCID = math. Round (math. Random () * (colors. Length-1 ));
Nextblock = blocks [bid];
Eval ("arr_curblock =" + curblock + ";");
Eval ("showblock (120,0," + curblock + ", colors [" + CID + "]);");
Eval ("arr_curblock =" + curblock + ";");
Eval ("showblock1 (466,116," + nextblock + ", colors [" + NCID + "], nblock_tbl, nblock_div );");
}

Function saveblock (){
For (I = 0; I <4; I ++ ){
For (j = 0; j <4; j ++ ){
Table [(Cury/n_incstep + I) * 16 + curx/n_incstep + J] | = arr_curblock [I * 4 + J];
If (arr_curblock [I * 4 + J] = 1)
If (Cury/n_incstep + I <21) & (curx/n_incstep + j> 1) & (curx/n_incstep + j <14 ))
Table_tbl.rows (Cury/n_incstep + I). cells (curx/n_incstep + J). style. Background = colors [CID];
If (Table [(Cury/n_incstep + I) * 16 + curx/n_incstep + J]! = 1)
Table_tbl.rows (Cury/n_incstep + I). cells (curx/n_incstep + J). style. Background = "black ";
}
}
}

Function delline (line ){
For (I = line; I> 0; I --){
For (j = 2; j <14; j ++ ){
Table [I * 16 + J] = table [(I-1) * 16 + J];
}
}
Table_tbl.deleterow (line );
Table_tbl.insertrow (0 );
For (I = 0; I <16; I ++ ){
Table_tbl.rows (0). insertcell ();
If (I <2 | I> 13) table_tbl.rows (0). cells (I). style. Background = "Navy ";
}
Killedlines ++;
CLL. innertext = parseint (CLL. innertext) + 1;
}

Function dellines (){
VaR C, D, I, J;
D = 0;
Cury = block_div.style.pixeltop;
For (I = (Cury/20 + 3); I> Cury/20-1; I --){
C = 0;
For (j = 2; j <14; j ++ ){
If (isnan (Table [I * 16 + J]) | I = 21) break;
C + = table [I * 16 + J];
}
If (C = 12 ){
Delline (I );
I ++;
D ++;
}
}
If (D> 0)
SCO. innertext = parseint (SCO. innertext) + D * 36;
}

Function Lucifer (){
For (VAR I = 2; I <14; I ++ ){
If (Table [16 + I] = 1) return true;
}
Return false;
}

Function gameover (){
Gameover = 1;
Clearinterval (gameinterval );
Block_div.innerhtml = "";
For (I = 0; I <21; I ++ ){
For (j = 2; j <14; j ++ ){
SetTimeout ("table_tbl.rows (" + I + "). cells ("+ J + "). style. background = colors [math. round (math. random () * 7)]; ", 16 * I * j );
}
}
Nblock_div.innerhtml = "game over ";
}

Function document_onkeydown (){
If (gameover = 1) return;
With (block_div.style ){
Curx = pixelleft;
Cury = pixeltop;
Switch (event. keycode ){
Case 37:
If (canmove (curX-n_IncStep, Cury, arr_curblock ))
Pixelleft-= n_incstep;
Break;
Case 38:
Change (1 );
Break;
Case 39:
If (canmove (curx + n_incstep, Cury, arr_curblock ))
Pixelleft + = n_incstep;
Break;
Case 40:
If (canmove (curx, Cury + n_incstep, arr_curblock )){
Pixeltop + = n_incstep;
}
Else {
Saveblock ();
Dellines ();
If (Lucifer ()){
Gameover ();
Return;
}
SCO. innertext = parseint (SCO. innertext) + 2;
Newblock ();
}
Break;
Case 32:
If (pause = 0 ){
Clearinterval (gameinterval );
Pause = 1;
}
Else {
Gameinterval = Window. setinterval ("handle_interval ()", (maxspeed-speed + 1) * 60 );
Pause = 0;
}
Break;
Case 90:
Change (1 );
Break;
Case 88:
Change (-1 );
Break;
Default:
}
}
}

Function handle_interval (){
Curx = block_div.style.pixelleft;
Cury = block_div.style.pixeltop;
If (canmove (curx, Cury + n_incstep, arr_curblock )){
Block_div.style.pixeltop + = n_incstep;
}
Else {
Saveblock ();
Dellines ();
If (Lucifer ()){
Gameover ();
Return;
}
SCO. innertext = parseint (SCO. innertext) + 2
Newblock ();
}
If (killedlines> = clr_per_line ){
Killedlines-= clr_per_line;
If (speed <maxspeed)
Speed ++;
Else
Speed = maxspeed;
SPD. innertext = speed;
Clearinterval (gameinterval );
Gameinterval = Window. setinterval ("handle_interval ()", (maxspeed-speed + 1) * 60 );
}
}
// -->
</SCRIPT>

<Script language = JavaScript event = onkeydown for = document>
<! --
If (document. All)
Document_onkeydown ()
// -->
</SCRIPT>

<Meta content = "mshtml 6.00.5730.11" name = generator> <Body Language = JavaScript>
<Div id = block_div style = "Left: 60px; top: 0px">
<Table id = block_tbl cellspacing = 0 cellpadding = 0 border = 0>
<SCRIPT>
If (document. All ){
For (VAR I = 0; I <4; I ++ ){
Document. Write ("<tr> ");
For (var j = 0; j <4; j ++ ){
Document. Write ("<TD style =/" border: 1 solid black;/"> </TD> ");
}
Document. Write ("</tr> ");
}
}
</SCRIPT>

<Tbody> </table> </div>
<Div id = nblock_div>
<Table id = nblock_tbl cellspacing = 0 cellpadding = 0 border = 0>
<SCRIPT>
If (document. All ){
For (VAR I = 0; I <4; I ++ ){
Document. Write ("<tr> ");
For (var j = 0; j <4; j ++ ){
Document. Write ("<TD style =/" height: 40; width: 40; Border: 1 outset black;/"> </TD> ");
}
Document. Write ("</tr> ");
}
}
</SCRIPT>

<Tbody> </table> </div>
<Div id = table_div>
<Table id = table_tbl cellspacing = 0 cellpadding = 0 border = 0>
<SCRIPT>
If (document. All ){
For (VAR I = 0; I <22; I ++ ){
Document. Write ("<tr> ");
For (var j = 0; j <16; j ++ ){
VaR D2 = I * 16 + J;
If (Table [D2] = 1)
Document. Write ("<TD bgcolor = Navy> </TD> ");
Else
Document. Write ("<TD style =/" Background: black;/"> </TD> ");
}
Document. Write ("</tr> ");
}
}
</SCRIPT>

<Tbody> </table> </div>
<Div id = title_div nowrap> Please input speed (1-9) to begin: <input id = speedin
Size = 8> & nbsp; <button id = But onclick = begintet ()> submit! </Button> </div>
<Div id = infobar_div>
<Table bordercolor = Navy cellspacing = 0 cellpadding = 0 border = 1>
<Tbody>
<Tr align = middle>
<TD style = "Font: 12px system; width: 56px; color: #99 CCFF"> speed: </TD>
<TD id = SPD style = "Font: 12px system; color: Red"> 1 </TD>
<TD style = "Font: 12px system; width: 86px; color: #99 CCFF"> total score: </TD>
<TD id = SCO style = "Font: 12px system; color: Red"> 0 </TD>
<TD style = "Font: 12px system; width: 96px; color: #99 CCFF"> cleared
Lines: </TD>
<TD id = CLL
Style = "Font: 12px system; color: Red"> 0 </TD> </tr> </tbody> </table> </div>
<Script language = JavaScript id = mainsection>
<! --
If (document. All ){
NCID = math. Round (math. Random () * (colors. Length-1 ));
Bid = math. Round (math. Random () * (blocks. Length-1 ));
Nextblock = blocks [bid];
Newblock ();
}
Function begintet (){
Document. All. speedin. Disabled = true
Document. all. But. Disabled = true
Speed = parseint (document. All. speedin. value );
If (isnan (speed) | speed = NULL | speed> maxspeed | speed <1) speed = 1;
SPD. innertext = speed;
Gameinterval = Window. setinterval ("handle_interval ()", (maxspeed-speed + 1) * 60 );
}
// -->
</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.