The first time to write such a large JS program, code volume first broken 300 of the page, but also is pure JS, no library or frame ...
On and off written four days finally finished, in addition to feel himself through this game, JS improved a lot, but also cultivate their own control of the code. Although not use to object-oriented, but I am strict with myself, more than 90% functions without annotations can not be more than 10 lines, easy to write and easy to update maintenance.
HTML file:
<body>
<div id= ' game ' >
<div id= ' main ' ></div>
<div id= ' count ' >
< h3>sorce:
CSS file
#game {
margin:50px auto;
width:960px;
}
#main {
border:3px solid black;
Float:left;
}
#count {
width:120px;
height:190px;
padding:5px 20px 10px 20px;
Float:right;
BORDER:3PX solid black;
#next {
width:60px;
height:60px;
Text-align:center;
}
#panel {
width:120px;
height:205px;
margin-top:20px;
padding:5px 20px 10px 20px;
BORDER:3PX solid black;
Float:right;
Margin-right: -23px
}
. cell{
Background:white;
Float:left;
width:20px;
height:20px
}
. _cell{
Background:white;
Float:left;
width:15px;
height:15px
}
. button{
position:relative;
margin-top:20px;
width:120px;
height:40px;
border-radius:10px;
Background:black;
Text-align:center;
line-height:40px;
Text-decoration:none;
Cursor:pointer
}
. button:hover{
background:orange
}
. Button a{
text-decoration:none;
Color:white;
}
JS file
var row = Col =, Len = game = document.getElementById (' game '), main = document.getElementById (' main '), Count = document.getElementById (' count '), Next = document.getElementById (' Next '), Sorce = document.getElementById ('
Sorce '), _sorce= 0, NEXTB = 0, Nextn = 0, Width = col*len, height= row*len, map = [], _map = [], bak = [], cur = [], timer = NULL, cells = NULL, _cells= null, Mark = 0, color = ' #0cf ', _color= ' orange ', block = [[0 X3300,0X3300,0X3300,0X3300],[0X3110,0X1700,0X2230,0X7400], [0xf000,0x1111,0xf000,0x1111],[0x3220,0x7100,0x1130,
0X4700], [0x1320,0x6300,0x1320,0x6300],[0x2310,0x3600,0x2310,0x3600], [0x2700,0x2320,0x7200,0x2620]];
Init ();
Newblock ();
function Start (e) {//Event.preventdefault ();
Timer=setinterval (down,400);
function Stop (e) {//Event.preventdefault ();
Clearinterval (timer);
function init () {var tmp;
main.style.width=width+ "px";
main.style.height=height+ "px"; game.style.width=width+200+ "px";
game.style.height=height+ "px"; Cur={x:~~math.random () * (col-8) +4,//to the right distance y:-1, b:~~ (Math.random () *7),//block number n:~~ (Math.random () *4),}//block
State//New Box nextb=~~ (Math.random () *7);
The new state nextn=~~ (Math.random () *4);
Cannot ligatures//document.createelement (' div '). cloneNode var _cell=document.createelement (' div ');
_cell.classname= "_cell";
for (Var i=0;i<4;i++) for (Var j=0;j<4;j++) {Tmp=_cell.clonenode (TRUE) next.appendchild (TMP);
_cells.push (TMP)} _cells=document.queryselectorall ('. _cell ');
Shownext ();
var cell=document.createelement (' div ');
Cell.classname= "Cell";
for (Var i=0;i<row;i++) {for (Var j=0;j<col;j++) {Tmp=cell.clonenode (true);
Tmp.id=i*row+j;
Main.appendchild (TMP);
Cannot call method ' push ' of NULL//Cells.push (TMP);
} cells=document.queryselectorall (". Cell"); Each row is initialized to 100.
001//1 is to determine the boundary//last line initialized to full 1 var tmp= "1" for (var i=0;i<col;i++) tmp+= ' 0 '; tmp+= ' 1 ' mark=tmp=parseint (TMP,2);
for (Var i=0;i<row;i++) {map[i]=tmp;
} tmp= "1";
for (var i=1;i<col+2;i++) tmp+= ' 1 ';
Map[row]=parseint (tmp,2);
Document.onkeydown=function (e) {e=e?e:event; Console.log (e.keycode) switch (e.keycode) {case 37:move (1), break;//Left Case 38:rotate (); break;//upper Case 39:mo ve ( -1); Right case 40:down (); Case 32:down ();
Space}} tmp=document.queryselectorall ('. button ');
Tmp[0].onclick=start;
Tmp[1].onclick=stop; function Newblock () {cur={x:~~ (Math.random () * (col-8) +4), y:0, B:NEXTB,//block number N:nextn//block status} nextb=
~ ~ (Math.random () *7);
Nextn=~~ (Math.random () *4);
Shownext ();
bak={x:cur.x, y:0, B:cur.b, N:CUR.N} update ();
function down () {if (Isover ()) {return} cur.y++;
if (!candown ()) {//Update view for (Var i=0;i<row;i++) map[i]=_map[i];
Show ();
if (Matchline ()) show ();
Newblock ();
} else{update ();
} function Move (dir) {bak={x:cur.x} cur.x+=dir; bak.x=cur.x//ConSole.log (cur.x)//Console.log (bak.x)//bak.x==cur.x if (Candown ()) {update ();
}else{//Console.log (cur.x) cur.x=bak.x;
Console.log (cur.x)}} function rotate () {bak={N:CUR.N} cur.n++;
cur.n%=4;
if (!candown ()) CUR.N=BAK.N;
else update ();
function Update () {var TMP=BLOCK[CUR.B][CUR.N];
for (Var i=0;i<row;i++) _map[i]=map[i];
for (Var i=0;i<4;i++) _map[cur.y+i]|= (tmp>> (12-4*i) &0x000f) <<cur.x);
Show ();
function Candown () {var TMP=BLOCK[CUR.B][CUR.N];
Cur.x<0, the shift result error if (cur.x<0) return false; A square of four lines for (Var i=0;i<4;i++) {//each row with map's next line or OP//Console.log (Map[cur.y+1+i].tostring (2)) if ((tmp>>
(12-4*i) &0x000f) <<cur.x) & Map[cur.y+i]) return false;
return true;
function Matchline () {var upgreade=0; var _mark=map[row]; _mark are all 1 for (Var i=cur.y,j=0;j<4&&i<row;i++,j++) {//if (MAP[I]&0XFFFFFFFFFF==0XFFFFFFFFFF) {if ((Map[i]&_mark) ==_mark) {Map.splicE (i,1);
Map.unshift (Mark);
upgreade=1;
_sorce++;
}} Sorce.innerhtml=_sorce;
return upgreade;
Function Show () {//Console.log (Block[cur.b][cur.n].tostring (2))//Console.log (cur.x) for (Var i=0;i<row;i++) {
for (Var j=0;j<col;j++) if (_map[i]>> (col-j) &0x000000001) {cells[i*col+j].style.background=color;
}else{cells[i*col+j].style.background= ' white '; }//Alert (i+ "+j+" "+cells[i*row+j].id)//Console.log (_map[i].tostring (2))}} function Shownext () {var tmp=block [NEXTB]
[Nextn]; for (Var i=0;i<4;i++) for (Var j=0;j<4;j++) if (tmp>> (15-i*4-j) &0x0001) _cells[i*4+j].style.backg
Round=_color;
else _cells[i*4+j].style.background= ' white ';
The function Isover () {if (map[0]|0x000000000)!=mark) {clearinterval (timer);
Alert ("Game Over");
return true;
return false; }
The operation effect is as follows:
Demo address (point to start run game): Click to open the link