Create your game empire with pure JS Tetris.

Source: Internet
Author: User

Create your game empire with pure JS Tetris.

Tetris, Russian: Т р и с) is a television and handheld game machine. It was invented by Russian Alexei pakitov.

The basic rule of the Russian square is to move, rotate, and place various blocks automatically output by the game, so that they are arranged into one or more complete rows and the score is eliminated. It is easy to use and suitable for all ages, so it is widely used in the world.

So our question is, which of the following is the best excavator technology?

Tetris can be said to be a game that is popular all over the world and is well known to the world. Can we use code to simply implement this game as a web developer?

Html code:

1 <! Doctype html> 2

3 4 5 <body>
6

Css section: 

 
 
  1. body { 
  2.         background: blue; 
  3.         font: 25px / 25px ËÎÌå; 
  4.     } 
  5.  
  6.     #box { 
  7.         float: left; 
  8.         width: 252px; 
  9.         border: #999 20px ridge; 
  10.         color: #9f9; 
  11.         text-shadow: 2px 3px 1px #0f0; 
  12.     } 
  13.  
  14.     #info { 
  15.         float: left; 
  16.         color: #cfc; 
  17.         padding: 24px; 
  18.     } 
  19.  
  20.     #next { 
  21.         padding: 8px; 
  22.         width: 105px; 
  23.         color: #9f9; 
  24.         text-shadow: 2px 3px 1px #0f0; 
  25.     } 

Js section:

1 var map = eval ("[" + Array (23). join ("0x801,") + "0 xfff]");
2 var tatris = [[0x6600], [0x2222, 0xf00], [0xc600, 0x2640], [0x6c00, 0x4620], [0x4460, 0x2e0, 0x6220, 0x740], [0x2260, 0xe20, 0x6440, 0x4700], [0x2620, 0x720, 0x2320, 0x2700];
3 var char = {x: "\ u3000", s: "\ u25a0", t: "\ u25a1 "};
4 var keycom = {"38": "rotate (1)", "40": "down ()", "37": "move (2, 1 )", "39": "move (0.5,-1)", "32": "0; pause =! Pause "};
5 var dia, pos, bak, run, next, pause = false, info = {speed: 1, lines: 0, score: 0 };
6
7 // start time
8 function start (){
9 dia = next. d;
10 bak = pos = {
11 fk: [],
12 y: 0,
13 x: 4,
14 s: next. s
15 };
16 nextdia ();
17 document. getElementById ("next "). innerHTML = (next. d [next. s % next. d. length] | 0x10000 ). toString (2 ). slice (-16 ). replace (/.... /g, "$ & <br/> "). replace (/1/g, char. t ). replace (/0/g, char. x );
18 document. getElementById ("text "). innerHTML = "SCORE:" + info. score + "<br/> LINES:" + info. lines + "<br/> SPEED:" + info. speed;
19 rotate (0 );
20 run = setInterval ("pause | down ()",~ ~ (Math. pow (1.3, 12-info. speed) * 30 + 20 ));
21}
22
23 // game end event
24 function over (){
25 document. onkeydown = null;
26
27 // confirm, whether to enable another round
28 var end = confirm ("the game is over, next game ");
29 if (end ){
30 window. location. href = window. location. href;
31} else {
32 alert ("Cool, good self-control! ");
33}
34}
35
36 function nextdia (){
37 next = {d: tatris [~ ~ (Math. random () * 7)], s :~ ~ (Math. random () * 4 )};
38}
39
40 function update (t ){
41 bak = {fk: pos. fk. slice (0), y: pos. y, x: pos. x, s: pos. s };
42 if (t) return;
43 for (var I = 0, a2 = ""; I <22; I ++) a2 + = map [I]. toString (2 ). slice (1,-1) + "<br/> ";
44 for (var I = 0, n; I <4; I ++)
45 if (/([^ 0] +)/. test (bak. fk [I]. toString (2). replace (/1/g, char. t )))
46 a2 = a2.substr (0, n = (bak. y + I + 1) * 15-RegExp. $ _. length-4) + RegExp. $1 + a2.slice (n + RegExp. $1. length );
47 document. getElementById ("box"). innerHTML = a2.replace (/1/g, char. s). replace (/0/g, char. x );
48}
49
50 function is (){
51 for (var I = 0; I <4; I ++)
52 if (pos. fk [I] & map [pos. y + I])! = 0)
53 return pos = bak;
54}
55
56 function rotate (r ){
57 var f = dia [pos. s = (pos. s + r) % dia. length];
58 for (var I = 0; I <4; I ++)
59 pos. fk [I] = (f> (12-I * 4) & 15) <pos. x;
60 update (is ());
61}
62
63 function down (){
64 + + pos. y;
65 if (is ()){
66 for (var I = 0, r = 0; I <4 & pos. y + I <22; I ++)
67 if (map [pos. y + I] | = pos. fk [I]) = 0 xfff ){
68 map. splice (pos. y + I, 1), map. unshift (0x801 );
69 + + info. lines % 20 = 0 & info. speed ++, r ++;
70}
71 clearInterval (run );
72 if (map [1]! = 0x801)
73 return over ();
74 info. score + = ~ ~ (Math. pow (r, 1.5) * 10) + 2;
75 start ();
76}
77 update ();
78}
79
80 function move (t, k ){
81 pos. x + = k;
82 for (var I = 0; I <4; I ++)
83 pos. fk [I] * = t;
84 update (is ());
85}
86
87 document. onkeydown = function (e ){
88 eval ("pause |" + keycom [(e? E: event). keyCode]);
89 };
90 nextdia ();
91 start ();

Implementation:

Download the source code.

We hereby declare that all comments and private messages will be replied immediately. You are also welcome to correct the mistakes and make progress together.

Author's blog http://www.cnblogs.com/toutou

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.