Let's take a look at the effect chart first:
The number above is a score, the game does not consider compatibility, only tested on chrome, but most modern browsers can still play.
First on HTML code
<! doctype html> <html> <head> <meta charset= "UTF-8" >
<title> Tetris </title> <style type= "Text/css" > body {
padding: 0;
margin-top: 40px;
text-align: center; } .tetris { border: 4px solid
Black &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} </style> </head> <body > <div id= "Score" ></div> <Canvas id= "Tetris" width= " height=" "420" ></canvas> < Script type= "Text/javascript" src= "js/tetris2.js" ></script> </body> </html>
HTML code does not explain AH, focus on the way JS implementation.
is the first function to load a picture:
Function dlimg (IMG) { // return an IMG object var oimg = new
image ();
oimg.src = ' images/' + img;
return oimg; /** * @param img img Object array * @param sw screen adaptation of a ratio * @param fun Program Entry function */function loadallimg (img, sw, fun) {
Var l = img.length, i,h = 0; for (i = 0; i < l; i ++) {
oimgarr[img[i]] = dlimg (Img[i]); oimgarr[img[i]].onload = function () { // Loading is asynchronous this.width =
this.width * sw; this.height = this.height
* sw;
h ++; h >= l && fun (); // call fun function   after all pictures have been loaded successfully; &NBSP}}
The
Oimgarr is a global variable that temporarily saves the IMG object used by the game. The
is then the code for the main function, which contains the main logical features of Tetris. The
Main function has 2 objects, the Block object is a Shape object, and the parameter type is the type of the shape (top, L, l, field, shape), blocks refers to the entire Russian disk (do not know what words to describe ...). Object
Below is the implementation of the Block object:
Function block (type) { this.type = type;
this.i = -1;
this.j = 6;
this.speed = 100;
this.defer = 0; switch (this.type) { case 1: // l character this.outline &NBSP;=&NBSP;[{I:&NBSP;THIS.I,&NBSP;J:&NBSP;THIS.J}, &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{I:&NBSP;THIS.I&NBSP;-&NBSP;1,&NBSP;J:&NBSP;THIS.J}, {i: this.i - &NBSP;2,&NBSP;J:&NBSP;THIS.J},
{i: this.i - 3, j: this.j}]; break; case 2: // on Word this.outline = [{i: this.i, j: this.j - 1}, {i: &NBSP;THIS.I&NBSP;-&NBSP;1,&NBSP;J:&NBSP;THIS.J}, &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{I:&NBSP;THIS.I,&NBSP;J:&NBSP;THIS.J}, {i: this.i, j: this.j + 1
}];
break; case 3: // l Word this.outline = [{i: this.i - 2, j: this.j - 1}, {i: this.i - 1, j: this.j - 1}, {i: this.i, j: this.j - 1},
{i: this.i, j: this.j}];
break; case 4: // Field Letter this.outline = [{i: this.i - 1, j: this.j - 1}, {i: this.i, j: this.j - 1}, &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{I:&NBSP;THIS.I,&NBSP;J:&NBSP;THIS.J} , {i:
this.i - 1, j: this.j}];
break; case 5: // Turn Word this.outline = [{i: this.i - 1, j: this.j - 1}, {i: this.i, j: this.j - 1}, &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{I:&NBSP;THIS.I,&NBSP;J:&NBSP;THIS.J}, {i: this.i + 1, j: this.j}];
break; &NBSP;&NBSP;&NBSP;&NBSP} this.dropBlock = function () { //
Drop Box var that = this; if (this.defer == this.speed) { this.outline.map (function (o) { o.i = o.i
+ 1;
});
this.defer = 0; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} else this.defer ++;
}; this.setSpeed = function () {
this.speed = 2;
this.defer = 0;
}; this.isReady = function () {
return this.speed == this.defer; &NBSP;&NBSP;&NBSP;&NBSP}}
Below is how the blocks object is implemented:
var blocks = { nullimg: imga[' Null.png '], cellimg: imga[' Cell.png '], pause: false, // whether the game is in a pause matrix: New array, // Matrix,-1 for null, 0 for moving, 1 for existing block: new block (1), // default the first occurrence of the box type is 1 score: 0, // Score Cumulative init: function () { var that = this, code =
null; &Nbsp; for (var i = 0; i < 21; i ++) { // initialization matrix Array this.matrix[i]
= new array (12); for (var j = 0; j < 12; j ++) {
this.matrix[i][j] = -1; ctx.drawimage (this.nullimg, j * cell, i * Cell, this.nullimg.width, this.nullImg.height);
} } document.onkeydown = function (e) { // key events code = e.keycode | |
e.which; switch (code) { case 37: // ← &Nbsp; that.setsite (-1);
break; case 38: // ↑
that.rotateblock ();
break; case 39: // →
that.setsite (1);
break; case 40: // ↓ the accelerated decline of the long if (That.block.speed == config. SPEED) that.block.speedup () ; // Acceleration
break; case 32: // Pause
!that.pause ? that.suspend () : that.start ();
break; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;DEFAULT&NBSP: return false; } }
; document.onkeyup = function (e) { if (e.keycode == 40) { // Release ↓ Recovery Speed that.block.speed = config.
SPEED;
} } &nBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}, start: function () { // start Game
var that = this; time = setinterval (function () {
console.time (' all '); that.block.dropblock (); // drop Box that.refreshmat (); // Refresh matrix &nbsP; that.reachbottom (); // detects whether it reaches the bottom or encounters an existing square
console.timeend (' all '); }, config .
Time); this.pause
= false; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}, suspend: function () { // pause
this.pause = true; clearinterval (
Time); }, refreshmat: function () { // perform a matrix refresh
var img = null, that = this; That.block.outline.forEach (function (o) { // Place all positions before moving to-1 if (o.i > 0 && that.matrix[o.i - 1][o.j] != 1 )
that.matrix[o.i - 1][o.j] = -1;
}); That.block.outline.forEach (function (o) { // refresh position after move
if (o.i >= 0)
that.matrix[o.i][o.j] = 0;
}); This.matrix.forEach (function (l, i) { // Redraw matrix l.foreach (function (M , j) { img = (m == -1 ?
&NBSP;THAT.NULLIMG&NBSP;:&NBSP;THAT.CELLIMG); ctx.drawimage (img, j * cell, i * cell,
Img.width, img.height);
});
}); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}, rotatePoint: function (c, p) { // c point for rotation Center, P is the rotation point, rotates 90 degrees clockwise. Returns the rotated coordinates return {j: p.i - c.i + c.j, i: -p.j + c.i + c.j}; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}, rotateBlock: function () { var that = this, i, o = null, ctr = that.block.outline[1], l =
That.block.outline.length; if ( that.block.type != 4) { // tin glyphs cannot rotate for (i = 0; i < l; i++) { o = that.rotatepoint (Ctr, that.block.outline[i]); if (o.j < 0 | | o.j > 11 | | &NBSP;O.I&NBSP;>&NBSP;20) { // rotation can not touch the boundary
break; } else if (o.i > 0 && o.j >= 0 && o.j <= 20 && Blocks.matrix[o.i][o.j] == 1) { // rotation can not have the point of the square
break; } } if (i == 4) { that.block.outline.foreach (function (o, i) { if (o.i >= 0) that.matrix[o.i][o.j] = -1; // clear the position before the change
that.block.outline[i] = that.rotatepoint (Ctr, o);
});
} } }, setsite: function (dir) { // set position after move left var i,
o, l = this.block.outline.length; for (i = 0; i < l; i ++) {
o = this.block.outline[i]; // whether it encounters an existing square, whether it touches the left and right edges if (o.i >= 0 && ( blocks.matrix[o.i][o.j + dir] == 1) | | (O.J&NBSP;+&NBSP;DIR&NBSP;==&NBsp;-1 | | o.j + dir == 12)) { break; // once a collision occurs , you exit the loop and do not perform a move operation } } if (i == l) { // when Count=l, indicates that the move operation did not occur a collision this.block.outline.foreach (function (o) { if (o.i >= 0) { Blocks.matrix[o.i][o.j] = -1; // Place current position to-1 o.j = (o.j + dir == -1 | | &NBSP;O.J&NBSP;+&NBSP;DIR&NBSP;==&NBSP;12) ? o.j : o.j + dir; // Whether to allow movement, allow the O.j+dir value to be given to O.J &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BLOCKS.MATRIX[O.I][O.J] = 0; // Refresh Latest Value } else { // Less than 0 o'clock (outside the matrix), also need to move around o.j = (o.j + dir == -1 | |
&NBSP;O.J&NBSP;+&NBSP;DIR&NBSP;==&NBSP;12) ? o.j : o.j + dir; }
}); } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}, reachBottom: function () { var
that = this, i, j, o, l = that.block.outline.length; if ( That.block.isReady ()) { // at the end of the current box drop frame, then check to see if the bottom is reached for (j = 0; j < l; j ++) { o = that.block.outline
[j]; if (o.i >= 0 && (o.i == 20 | | that.matrix[o.i + 1][o.j] == 1) { // collision when moving down break; // box to reach the bottom or fall on the other squares, the box stops falling, creating new squares }
} if (j < l) { // when the box falls on the bottom or other squares, check it out for (i = 0; i < l; i ++) {
o = that.block.outline[i]; if (o.i >= 0) { that.matrix[o.i][o.j] = 1; // After the box stops, modify the matrix data } else { that.gameover (); // game End
return; } } that.ruinmat (); // detect the need for blasting lines, if any, perform blasting operations &nBsp; that.block = new block (ParseInt (Math.random () *
&NBSP;5) + 1);
} } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP},
detectMat: function () { // detection matrix, to determine whether there is a row, return an array var count = 0, s, detecta = []; // explosion-Required line number this.matrix.foreach (FUNCTION&NBSP; (l, i) {
for (s = 0; s < l.length; s ++) {
if (l[s] == 1) count ++; else break; }
count == 12 && detecta.push (i);
count = 0;
}); return detecta.length == 0 ?
false : detecta; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}, ruinMat: function () { // blasting continuous line var dmat = This.detectmat (); // returns a collection of line numbers with squares for the entire row if (Dmat) { this.score = this.score + ( dmat.length == 1 ? 100 : dmat.length == 2 ? 250 :
dmat.length == 3 ? 450 : 700); score.innerhtml =
This.score.toString (); dmat.foreach (function (d) { blocks.matrix.splice (D, 1); // Delete all rows with squares blocks.matrix.unshift ([ -1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1]; // make up for the deleted line
}); } dmat = null; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}, gameOver: function () {
clearinterval (time);
alert (' You hang up '); } };
Block This object can be understood according to the coordinates of the following figure:
As you can see, the upper-left corner of the chessboard is the coordinate (0,0), each small square represents a coordinate point, the value of the sitting punctuation corresponds to the state value, that is, 1 indicates that the position is idle, 0 indicates the position is being refreshed, and 1 indicates that the position is occupied.
Of course there is still a module is not implemented, this module is the hint of the next box shape, this interested students can add ha.
Source in GitHub address: Https://github.com/zquancai/tetrisc
Welcome to download and learn from each other
HTML5 Canvas to achieve simple Tetris
Recently learned the next HTML5, take time to use Canvas wrote a Tetris game, write a relatively simple and rough, but also a bit of bugs, algorithms are not considered in depth, just to learn to use the canvas.
Main ways to achieve:
One, draw the canvas
Canvas width 200px, high 400px, small square width and height of 10px.
Two, draw 7 kinds of shapes of squares
On the canvas, the 4 small squares of each shape are drawn with the coordinates relative to the upper-left corner of the canvas, and the upper-left corner of the HTML5 's canvas coordinates is x=0,y=0.
For example in the first shape, the coordinates of the four squares are:
4 coordinate locations stored in array [0,0,1,0,1,1,1,2]
The relative coordinates of each shape's deformation are also stored in the array, in order to achieve a simple algorithm for shape deformation.
When drawing on the canvas, you need to draw four squares with the actual coordinates on the canvas, with a square width and a height of 10px;
FillRect (0,0,10,10)
FillRect (10,0,10,10)
FillRect (10,10,10,10)
FillRect (10,20,10,10)
three, Move Down
Every move down, y-coordinates +1, for example, the coordinates of the above figure change to: 0,1,1,1,1,2,1,3
Four, move around
Move once, x coordinates +1 or 1, for example, if the above figure moves 1 coordinates to the right, the coordinates are: 1,0,2,0,2,1,2,2.
You need to determine whether the current coordinates are to the left or right margin of the canvas, that is, the rightmost square in each shape or shape's warp to the right edge of the canvas, and you cannot move to the right, instead, the leftmost square is to the left edge and cannot be moved to the left.
V, deformation
Reads the initial coordinates of the current shape's deformation, plus the offset coordinates before being deformed.
vi. elimination of full grid rows
As described in drawing canvas, the canvas is divided into 40*20 areas, that is, 40 rows, 20 columns, each region can place a square (10*10), the 40*20 area of the canvas corresponds to a two-dimensional array, storing each region whether there are squares, there are 1, not 0
When each shape falls to the bottom, it is possible to determine whether each area of the row in which the shape is located has a square, that is, whether the value in the corresponding array is 1, or 1, and that the row is full and can be eliminated.
Seven, Operation
Add key Event
Up ARROW: Warp
DOWN ARROW: accelerating Move Down
Left ARROW: Move left
Right ARROW: Moving Right
Eight, Code
<! Doctype html> <!-- author:kinglau (Liushouqian) date : 2012-07-17 description: Some content is currently fixed, such as canvas size. There are also bugs blog:http://www.cnblogs.com/kinglau/ http:// weibo.com/u/1712849017 --> <html> <head> <title></
title> <meta charset= "UTF-8" /> <style> #canvas {
border: black 1px solid; } </style> <script type= "Text/javascript" > //7 Basic Shapes, and the coordinate var shap1=[[0,0,1,0,1,1,1,2],[0,1,1,1,2 after deformation,1,2,0],[0,0,0,1,0,2,1,2],[0,0,1,0,2,0,0,1]]; var shap2=[[0,0,0,1,0,2,1,0],[0,0,1,0,2,0,2,1],[
0,2,1,0,1,1,1,2],[0,0,0,1,1,1,2,1]]; var shap3=[[0,0,1,0,1,1,2,1],[1,0,1,1,0,1,0,2],[ 0,0,1,0,1,1,2,1],[1,0,1,1,0,1,0,2]] var shap4=[[
0,1,1,0,1,1,2,0],[0,0,0,1,1,1,1,2],[0,1,1,0,1,1,2,0],[0,0,0,1,1,1,1,2]]; var shap5=[[0,0,1,0,0,1,1,1],[0,0,1,0,0,1,1,1],[
0,0,1,0,0,1,1,1],[0,0,1,0,0,1,1,1]]; var shap6=[[0,1,1,1,2,1,1,0],[0,0,0,1,0,2,1,1],[
0,0,1,0,2,0,1,1],[1,0,1,1,0,1,1,2]]; var shap7=[[0,0,1,0,2,0,3,0],[0,0,0,1,0,2,0,3],[
0,0,1,0,2,0,3,0],[0,0,0,1,0,2,0,3]];
var shaps=[shap1,shap2,shap3,shap4,shap5,shap6,shap7]; //Unit Length var unitLen=10; //Canvas Context var
ctx; //Timing var
Tid //the corresponding array of canvas areas var
resultarray=new array (40); function init () { //Get the canvas context
ctx=document.getelementbyid ("Canvas"). GetContext (' 2d '); //Add key Events document.addeventlistener (' KeydoWN ', moveshape,false); //Initialize Canvas area array for (var i=0;i<40;i++) {
var row=new array (); for (var j =0;j<20;j++) {
row[j]=0; }
resultArray[i]=row; }
// startRun=true;
drawline ();
topTrue=true;
tid=setinterval ("DrawTetris ();", 200); } //
The coordinate offset of each shape on the canvas relative to the initial position var rectX=0;
var rectY=0; //shape Deformation var
rotate=0; //the initial coordinates of the current shape or deformation
var t; //Current Shape var shape;
var startRun=true; var shapeheight=0;//the height of the current shape, the maximum deviation of the y-coordinate of the four squares //used to produce random shapes var
randmshape=1; //the coordinates of each square of the current shape
Var shapexy=new array (4); //draw shapes according to coordinates
Function draw () { var i=0;
var tempY=0;
shapexy=new array (4); &Nbsp; for (i=0;i<4;i++) {
drawrect ((T[i*2]+rectx) *unitlen, (t[i*2+1]+recty) *unitLen); var row=
New array (2); row[0]= (t[i*2+
1]+RECTY); row[1]=t[i*2]+
Rectx; shapexy[i]=row
; //shapexy[i,0]
= (t[i*2+1]+recty); //shapexy[i,1]
=T[I*2]; &nbSp; if (toptrue==true) { if (tempy< (t[i*2+1]+recty)) {
tempY=t[i*2+1]+rectY;
shapeHeight=tempY+1;
} } &nbsP; else {
//tempY=rectY; } }
//rectY=tempY;
rectY+=1; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} //According to the captured key, To determine the specific key function getdirection (event) { var keycode = event.which | |
event.keyCode; &Nbsp; switch (keycode) { case 1: &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CASE&NBSP;38: case 269: //up return
' Up ';
break;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CASE&NBSP;2:
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CASE&NBSP;40: case 270:
return ' down ';
break;
case 3:
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CASE&NBSP;37:
case 271:
return ' left ';
break;
case 4: &nbsP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CASE&NBSP;39: case 272:
return ' right ';
break; case 339: //exit Case 240: //back
return ' Back ';
break; } } //according to the key to determine the action performed Function moveshape (event) { if ( Getdirection (Event) = = ' right ') { for (var i=0;i <4;i++) { if (t[2*i]+rectx+1>=20 | | resultArray[2*i+rectX+1]==1 ) {
return; } }
Rectx+=1; } if (Getdirection (event) = ' left ') { for (var i=0;i<4;i++) { if (t[2*i]+rectx-1<0 | | resultarray[2*i+rectx-1]==1) { return;
} }
rectX-=1; } if (Getdirection (event) = = ' up ') {
var mleft=0; for (var i =0;i<4;i++) { if (t[i*2]+rectx>mleft) { mleft=rectx;
} }
if (rotate==3) {
rotate=0; } else{ rotate+=
1; } t=shape[rotate]; for (var i =0;i<4;i++) {
//t[2*i]=t[2*i]+mleft;
rectX=mleft; } } if (Getdirection (event) = ' down ') {
clearinterval (TID); tid= SetInterval ("Drawtetris (); ", 50); } &NBSP;&NBSP} //method of timed execution function drawtetris () {
if (Checkbottom () ==true) return;
//t=shape[rotate]; if (startrun==false) { ctx.clearrect (shapeXY[0][1]*
UNITLEN-1,SHAPEXY[0][0]*UNITLEN-1,UNITLEN+2,UNITLEN+2); ctx.clearrect (
SHAPEXY[1][1]*UNITLEN-1,SHAPEXY[1][0]*UNITLEN-1,UNITLEN+2,UNITLEN+2); &nbSp; ctx.clearrect (shapexy[2][1]*unitlen-1,shapexy[2][0]*unitlen-1,unitlen+2,unitlen+2); ctx.clearrect (
SHAPEXY[3][1]*UNITLEN-1,SHAPEXY[3][0]*UNITLEN-1,UNITLEN+2,UNITLEN+2); }
startRun=false; //ctx.clearrect (0,0,200,400-(
Shapeheight) *10);
drawline ();
var sp=randmShape;
shape=shaps[sp-1];
t=shape[rotate]; &nBsp;draw (); } var
Toptrue=false; //Check if the current shape is at the bottom of the canvas function checkbottom () { if (toptrue==true) {
startRun=true;
topTrue=false;
rectX=9;
rectY=0; randmshape=
Math.floor (Math.random () *7+1); return true; } if (recty+shapeheight-1>=40 | | recty==0) {
//clearinterval (TID); //
Ctx.clearrect (0,0,200,300);
if (rectY==0) { Return false &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; }
currentshapeonbottom (); return
True } else { // The four squares in the shape have one to the bottom, and can no longer move down the if (shapexy[0][0]==39 | | shapexy[1][0]==39 | | shapexy[2][0]==39 | | shapexy[3][0]==39) { &Nbsp; currentshapeonbottom ();
return true; } //the next line in the row for each square in the shape, if the box already exists , you cannot move the if further down ( RESULTARRAY[SHAPEXY[0][0]+1][SHAPEXY[0][1]]+RESULTARRAY[SHAPEXY[1][0]+1][SHAPEXY[1][1]]
&NBSP;&NBSP;&NBSP;+RESULTARRAY[SHAPEXY[2][0]+1][SHAPEXY[2][1]]+RESULTARRAY[SHAPEXY[3][0]+1][SHAPEXY[3][1]] >=1) ) {
currentshapeonbottom ();
return true; }
topTrue=false; return
False } &NBSP;&NBSP} //the current shape after it reaches the bottom of the canvas function currentShapeonbottom () { resultarray[shapexy[0] [0]]
[Shapexy[0][1]]=1; resultarray[shapexy[1][0]][shapexy[1][
1]]=1; resultarray[shapexy[2][0]][shapexy[2][
1]]=1; resultarray[shapexy[3][0]][shapexy[3][
1]]=1; if (Clearrow () ==false) {
return; }
rectY=0;
rectX=9; randmshaPe=math.floor (Math.random () *7+1)
Startrun=true;
topTrue=true;
clearinterval (TID);
tid=setinterval ("DrawTetris ();", 200); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} //scoring
var vpoint=0; //clear the full grid row and score Function clearrow () { var row=
New array ();
var spacerow=new array (); var&nBsp;spacerows=new array (); for (var i=0;i<20;i++) {
spaceRow[i]=0; }
row[0]=shapeXY[0][0]; for (var i=1;i<4;i++) { for (var j=0;j <row.length;j++) { if (Row[j]!=shapexy[i][0]) { if (Row[j]<shapeXY I [0]) {
row.push (Shapexy[i][0]); } else {
row.unshift (Shapexy[i][0]); }
break; } } } var
isNeedRedraw=false;
for (var i=0;i<row.length;i++) { var
rowstate=0; for (var j =0;j<20;j++) {
rowState+=resultArray[row[i]][j]; } &nbSp; if (rowstate==20) { resultarray.splice (Row[i],
1);
resultarray.unshift (Spacerow);
vpoint+=10;
document.getelementbyid ("Txtpoint"). Value=vpoint;
isNeedRedraw=true; } } if (isneedredraw==true) {
ctx.clearrect (0,0,200,400); redrawcanvas ()
; } else { if ( shapexy[0][0]==0 | | shapexy[1][0]==0 | | shapexy[2][0]==0 | | shapexy[3][0]==0) {
document.getelementbyid ("Idstate"). innertext= "Game Over"; &nbSp; clearinterval (TID);
return false; } }
return true; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} //clears the full grid row, Redraw Canvas function redrawcanvas () { for (var i=0;i<40;i++) { for (var j=0;j<20;j++) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp; if (resultarray[i][j]==1) {
drawrect (J*unitlen,i*unitlen);
} } } &NBSP;&NBSP} //draw a small square in a shape function drawrect (x,y) {
ctx.fillstyle= "Red";
ctx.strokestyle= "BLACK"; ctx.linewidth=1;
ctx.fillrect (x,y,10,10);
ctx.strokerect (x,y,10,10); }
function drawline () { /*
var i=1; for (i=1;i<40;i++) {
ctx.beginpath (); Ctx.strokestyle= "BLACK"; ctx.linewidth=
1; ctx.moveto (0,i
*10); ctx.lineto (200
, i*10);
ctx.stroke (); } */ </script> </head> <body onload= "init ()" > <canvas id= "Canvas" width= " height=" > Browser does not support Html5 canvas </canvas> &Nbsp; <br> <form id= "F" name= "F" onsubmit= "init ()" > <br> < Input id= "Btnstart" type= "Submit" value= "Start" /> <label id= "Idstate" ></label> </ Div> <br> <label> score:</label> <input id= " Txtpoint " readonly=" true " value=" 0 " /> </form> </body > </html>