Tetris based on jquery

Source: Internet
Author: User
Tags hasownproperty

Code is second, there may be a lot of shortcomings, and there is a function is not implemented, that is the deformation of the box, did not think of a very perfect solution. Here's my idea, because when I did it, I didn't look at the others.

Idea, so do not know whether they have taken a detour, first made four basic small box model, all the building blocks are composed of these four blocks. The difficulty of feeling the problem is that the judgment movement terminates, here I set a baseline,

The value of the first baseline is the height of the entire frame, when the top value of the model equals the entire height, the motion stops, and the condition of the left and right motion is the same, when the movement falls to the bottom, the baseline is updated, the coordinates of the four model are inserted into the baseline, but the elimination function is taken into account. , insert an object with the top value as the property name, depending on the top value. Its logical relationship as a whole is as follows, when each movement of the block is predetermined, whether the next coordinate is in the baseline, if it is, stops the motion, and inserts the coordinate into the baseline to achieve the purpose of updating the baseline, whereas the other continues to move. When the block movement is stopped, it is necessary to determine whether each property value of the object that holds the baseline is a complete digital chain of the city, that is, in the same row, each column has a block exists, if the existence is to eliminate the operation, after elimination, the need to update the baseline operation.

Understanding, the feeling JS is a single-threaded language, it is obvious that when I move to both sides, and touch the sides of the frame, always press the right or LEFT arrow keys, the block can not fall, will always perform the program to both sides of the motion, do not know is not my plan is wrong.

The following code, only provide ideas:

Define (function (Require,exports,module) {
Require ("jquery");
var fly={
Time:null,//Timer
State:1,//control state, determine whether to continue movement
moving:0,//whether in the process of movement
Movingxy:[],//motion coordinates
Mode:[],//four motion modules
stopaxis:{
},//baseline coordinates
xyz:{
1:[[60,0],[80,0],[100,0],[120,0]],
2:[[100,0],[100,20],[100,40],[100,60]],
3:[[80,0],[100,0],[80,20],[100,20]],//Tian |
4:[[100,0],[80,20],[100,20],[120,20]],//Earth
5:[[120,0],[80,20],[100,20],[120,20]],
6:[[80,0],[80,20],[100,20],[100,40]],
7:[[100,0],[80,20],[100,20],[80,40]]
},
Creat:function (x) {
var md= "<a class= ' mode ' ></a>";
This.mode[0]= ($ (MD). css ({"Left": x[0][0]+ "px", "Top": x[0][1]+ "px"});
This.mode[1]= ($ (MD). css ({"Left": x[1][0]+ "px", "Top": x[1][1]+ "px"});
This.mode[2]= ($ (MD). css ({"Left": x[2][0]+ "px", "Top": x[2][1]+ "px"});
This.mode[3]= ($ (MD). css ({"Left": x[3][0]+ "px", "Top": x[3][1]+ "px"});

},
Start:function (X,Y,WH) {
This.movingxy[0]= ([This.mode[0].css ("left"). Replace ("px", ""), This.mode[0].css ("Top"). Replace ("px", "")])
This.movingxy[1]= ([This.mode[1].css ("left"). Replace ("px", ""), This.mode[1].css ("Top"). Replace ("px", "")])
This.movingxy[2]= ([This.mode[2].css ("left"). Replace ("px", ""), This.mode[2].css ("Top"). Replace ("px", "")])
This.movingxy[3]= ([This.mode[3].css ("left"). Replace ("px", ""), This.mode[3].css ("Top"). Replace ("px", "")])
for (Var i=0;i<4;i++) {
Fly.ifmove (Fly.movingxy[i],x,y)
if (this.state==0) {
Break
}
}
Console.log (Fly.state)
if (fly.state = = 1) {
$ (". Mode"). CSS ({
"Left": "+ =" + x,
"Top": "+ =" + Y
});
}
else {
Clearinterval (Fly.time);
Fly.state = 1;
if (wh = = False) {
$ (". Mode"). attr ("Class", "mode_stop"); Remove mode, change to normal block
Fly.insert (FLY.MOVINGXY);
if (Fly.delet ()) {
Alert ("GAME over");
return false;
}
this.moving=0;
if (this.moving==0) {
Fly.creat (Fly.xyz[math.floor (Math.random () *7+1));
$ ("#fy"). Append (Fly.mode[0],fly.mode[1],fly.mode[2],fly.mode[3]);
Fly.time=setinterval (function () {
Fly.start (0,20,false)
},700)
fly.moving=1;
}
}else{
Fly.time=setinterval (function () {
Fly.start (0,20,false)
},700)
fly.moving=1;
}

}
},
Control:function () {
$ (document). Bind ("KeyDown", function (event) {
Switch (event.keycode) {
Case 37:
Fly.start ( -20,0,true);
Case 39:
Fly.start (20,0,true);
Case 40:
Fly.start (0,20,false);
Case 17:

}
})
},
Ifmove:function (axis,x,y) {
Rexy=[parseint (Axis[0]) +x,parseint (axis[1]) +y];
if (rexy[0]==-20| | rexy[0]==200| | rexy[1]==300) {
fly.state=0;
return false;
}
if (Fly.stopAxis.hasOwnProperty (rexy[1])) {
for (Var n=0;n<fly.stopaxis[rexy[1]].length;n++) {
if (Rexy.join ("") ==fly.stopaxis[rexy[1]][n].join ("")) {
fly.state=0;
return false;
}
}
}
return false;
/*for (var i in Fly.stopaxis) {
if (Rexy.join ("") ==fly.stopaxis[i].join ("")) {
fly.state=0;
return false;
}
}*/
},
Insert:function (array) {//Insert Baseline
for (Var i=0;i<4;i++) {
Console.log (array[i][0]+ "| |" +ARRAY[I][1])
if (!fly.stopaxis.hasownproperty (array[i][1])) {
Fly.stopaxis[array[i][1]]=new Array ();
}
Fly.stopaxis[array[i][1]].push (Array[i]);
Fly.mode[i].attr ("Topname", fly.movingxy[i][1]);
Console.log (Fly.stopaxis)
}
},
Delet:function () {//elimination module
var kzy = {};
for (var i in Fly.stopaxis) {
if (i==0) {
return true;
}
if (fly.stopaxis[i].length = = 10) {
Fly.stopaxis[i] = [];
$ (". Mode_stop[topname=" + i + "]"). Remove (); Eliminate baselines
var score=parseint ($ ("#oo"). Text ()) +10;
$ ("#oo"). Text (score);
for (var x in Fly.stopaxis) {//Overall descent
if (x < i) {
$ (". Mode_stop[topname=" + x + "]"). CSS ("Top", "+=20");
for (var k = 0; k < fly.stopaxis[x].length; k++) {
FLY.STOPAXIS[X][K][1] = parseint (fly.stopaxis[x][k][1]) + 20;
}
Console.log (x)
Kzy[parseint (x) + [] = fly.stopaxis[x];
Fly.stopaxis[x]=[];
}
}
}
}
For (var m in Fly.stopaxis) {//refactoring baseline
if (Kzy.hasownproperty (m)) {
FLY.STOPAXIS[M]=KZY[M];
}
}
},
};
$ (function () {
$ ("#start"). Bind ("click", Function () {
if (fly.moving==0) {
This.mode=[];
Fly.creat (Fly.xyz[math.floor (Math.random () *7+1));
Clearinterval (Fly.time);
$ ("#fy"). Append (Fly.mode[0],fly.mode[1],fly.mode[2],fly.mode[3]);
Fly.time=setinterval (function () {
Fly.start (0,+20,false);
},700);
fly.moving=1;
}
})
Fly.control ()
$ ("#stop"). Bind ("click", Function () {
Clearinterval (Fly.time);
})
})
})

Tetris based on jquery

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.