JS Game Tetris

Source: Internet
Author: User
Tags eval setinterval
Tip: You can modify some of the code before running

<HTML> <SCRIPT> Parent.moveto ((screen.width-775)/2, (screen.height-540)/2); Parent.resizeto (775,540) </SCRIPT> <HEAD> <meta name= "Title" content= "JScript simple Tetris" > < Title>simple tetris</title> <style> Body {margin:0;background:black;font-size:9pt} td {Height:20; WIDTH:20;FONT-SIZE:9PT} #block_div {position:absolute;z-index:1;width:80;font-size:9pt} #table_div {position: ABSOLUTE;Z-INDEX:0;WIDTH:320;FONT-SIZE:9PT} #nblock_div {position:absolute;z-index:2;font:48 system;color:red; FONT-SIZE:9PT} #title_div {position:absolute;left:326;top:7;font-size:22px;color:whitefont-size:9pt} #infobar_div {position:absolute;left:396;top:416;} #infobar2_div {position:absolute;left:396;top:480;font-size:14px;color:whitefont-size:9pt} #infobar2_div A{color : 99ccff;font:system;text-decoration:none;font-size:9pt} </style> <script Id=clienteventhandlersjs Language=javascript> <!--/* Tetris Game by Xie Yong (nsxy@163.net) modifications by DYnamicdrive.com for interface enhancements for this script, visit http://ww w.dynamicdrive.com */if (document.all) {var n_width = $ var n_height = n; 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,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", "#80FFFF", "#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 (0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0); var tt_o2 = new Array (0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0); var Tt_o3 = new Array (0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0); var Tt_o4 = new Array (0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0); var tt_t1 = new Array (0,0,0,0,0,1,0,0,1,1,0,0,0,1,0,0); var tt_t2 = new Array (0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,0); var tt_t3 = new Array (0,0,0,0,0,1,0,0,0,1,1,0,0,1,0,0); var tt_t4 = new Array (0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,0); var tt_z1 = new Array (0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0); var tt_z2 = new Array (0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0); var tt_z3 = new Array (0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0); var tt_z4 = new Array (0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0); var tt_s1 = new Array (0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0); var tt_s2 = new Array (0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0); var tt_s3 = new Array (0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0); var tt_s4 = new Array (0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0); var tt_l1 = new Array (0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0); var tt_l2 = new Array (0,0,0,0,0,0,1,0,1,1,1,0,0,0,0,0); var tt_l3 = new Array (0,0,0,0,0,1,1,0,0,0,1,0,0,0,1,0); var tt_l4 = new Array (0,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0); var tt_j1 = new Array (0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0); var tt_j2 = new Array (0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0); var tt_j3 = NEW Array (0,0,0,0,0,1,1,0,0,1,0,0,0,1,0,0); var tt_j4 = new Array (0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0); var tt_i1 = new Array (0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0); var tt_i2 = new Array (0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1); var tt_i3 = new Array (0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0); var tt_i4 = new Array (0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1); var table = new Array (1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0 , 1, 1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0 , 0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0, 0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,1,1,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 (v) Ar 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; The 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_in cstep+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) {to (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*d*36; The function Lucifer () {for (Var i=2;i<14;i++) {if (table[16+i]==1) is 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 for=document event=onkeydown> <!--if (document.all) Document_onkeydown ()//--> </SCRIPT> </HEAD> <body language=javascript> <div id=block_div > <table id=block_tbl border=0 cellspacing=0 cellpadding=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> </table> </div> <div id=nblock_div> <table id=nblock_tbl border=0 cellspacing=0 Cellpadding=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> </table> </div> <div id=table_div> <table id=table_tbl border=0 cellspacing=0 CE Llpadding=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 * + j; if (table[d2]==1) document.write ("<td bgcolor=navy></td>"); else document.write ("<td style=\" background:black;\ "></td>"); } document.write ("</tr>"); } </script> </table> </div> <div id=title_div nowrap > <font color= "#FFFF00" >input speed (1-9) to begin: <input type= "text" size=8 id= "Speedin" > <button onclick= "Begintet ()" id= "but" >submit! </font></button></div> <div id=infobar_div> <table border=1 bordercolor=navy cellspacing=0 cellpadding=0> <tr align=center > <td >Speed:</td> <td id=spd>1</td> <td >total score:</td> <td ID =sco>0</td> <td >cleared lines:</td> <td id=cll>0</td> </tr> </table& Gt </div> <div Id=infobar2_div>script featured on Dynamic drive</div> <script id=mainsection L Anguage=javascript> <!--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.a Ll.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> </HTML>
Tip: You can modify some of the code before running

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.