This article mainly introduces 60 lines of js code to implement Tetris, which may have some minor bugs, but it is quite good in general. Let's take a look at the code and give comments. This is what I saw on the internet before. It was written by a great God and has been worshipped.
Script var map = eval ("[" + Array (23 ). join ("0x801,") + "0 xfff]"); var tatris = [[0x6600], [0x2222, 0xf00], [0xc600, 0x2640], [0x6c00, 0x4620], [0x4460, 0x2e0, 0x6220,0x740], [0x2260, 0xe20, 0x6440,0x4700], [0x2620,0x2700, 0x2320,0 x]; var keycom = {"38": "rotate (1)", "40 ": "down ()", "37": "move (0.5)", "39": "move (,-1)"}; var dia, pos, bak, run; function start () {dia = tatris [~~ (Math. random () * 7)]; bak = pos = {fk: [], y: 0, x: 4, s :~~ (Math. random () * 4)}; rotate (0);} function over () {document. onkeydown = null; clearInterval (run); alert ("game over");} function update (t) {bak = {fk: pos. fk. slice (0), y: pos. y, x: pos. x, s: pos. s}; if (t) return; for (var I = 0, a2 = ""; I <22; I ++) a2 + = map [I]. toString (2 ). slice (1,-1) +"
"; For (var I = 0, n; I <4; I ++) if (/([^ 0] + )/. test (bak. fk [I]. toString (2 ). replace (/1/g, "\ u25a1") a2 = a2.substr (0, n = (bak. y + I + 1) * 15-RegExp. $ _. length-4) + RegExp. $1 + a2.slice (n + RegExp. $1. length); document. getElementById ("box "). innerHTML = a2.replace (/1/g, "\ u25a0 "). replace (/0/g, "\ u3000");} function is () {for (var I = 0; I <4; I ++) if (pos. fk [I] & map [pos. y + I])! = 0) return pos = bak;} function rotate (r) {var f = dia [pos. s = (pos. s + r) % dia. length]; for (var I = 0; I <4; I ++) pos. fk [I] = (f> (12-i * 4) & 15) <
The above is all the content of this article. I hope you will like it.